-
Notifications
You must be signed in to change notification settings - Fork 6
/
FactMetaData.cpp
194 lines (171 loc) · 5.67 KB
/
FactMetaData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "FactMetaData.h"
#include <limits>
#include <QDebug>
FactMetaData::FactMetaData(ValueType_t type,QObject *parent) : QObject(parent)
, _type(type)
, _rawDefaultValue(0)
, _defaultValueAvailable(false)
, _rawMin(_minForType())
, _rawMax(_maxForType())
{
}
FactMetaData::~FactMetaData()
{
}
QVariant FactMetaData::_minForType() const
{
switch(_type) {
case typeUint8:
return QVariant(std::numeric_limits<unsigned char>::min());
case typeInt8:
return QVariant(std::numeric_limits<char>::min());
case typeUint16:
return QVariant(std::numeric_limits<unsigned short int>::min());
case typeInt16:
return QVariant(std::numeric_limits<short int>::min());
case typeUint32:
return QVariant(std::numeric_limits<unsigned int>::min());
case typeInt32:
return QVariant(std::numeric_limits<int>::min());
case typeFloat:
return QVariant(-std::numeric_limits<float>::max());
case typeDouble:
return QVariant(-std::numeric_limits<double>::max());
}
return QVariant();
}
QVariant FactMetaData::_maxForType() const
{
switch(_type) {
case typeUint8:
return QVariant(std::numeric_limits<unsigned char>::max());
case typeInt8:
return QVariant(std::numeric_limits<char>::max());
case typeUint16:
return QVariant(std::numeric_limits<unsigned short int>::max());
case typeInt16:
return QVariant(std::numeric_limits<short int>::max());
case typeUint32:
return QVariant(std::numeric_limits<unsigned int>::max());
case typeInt32:
return QVariant(std::numeric_limits<int>::max());
case typeFloat:
return QVariant(std::numeric_limits<float>::max());
case typeDouble:
return QVariant(std::numeric_limits<double>::max());
}
return QVariant();
}
void FactMetaData::setRawDefaultValue(const QVariant &rawDefaultValue)
{
if(_rawMin < rawDefaultValue && rawDefaultValue < _rawMax) {
_rawDefaultValue = rawDefaultValue;
_defaultValueAvailable = true;
}
else {
qWarning() << "Attempt to set default value which is outside min/max range";
}
}
void FactMetaData::setRawMin(const QVariant &rawMin)
{
if(rawMin >= _minForType()) {
_rawMin = rawMin;
}
else {
qWarning()<<"Attempt to set min below allowable value.";
_rawMin = _minForType();
}
}
void FactMetaData::setRawMax(const QVariant &rawMax)
{
if(rawMax < _maxForType()) {
_rawMax = rawMax;
}
else {
qWarning() << "Attempt to set max above allowable value";
_rawMax = _maxForType();
}
}
FactMetaData::ValueType_t FactMetaData::stringToType(const QString &typeString,bool &unknownType)
{
QStringList typeStringList;
QList<ValueType_t> knownTypes;
unknownType = false;
typeStringList<< QStringLiteral("Uint8")
<< QStringLiteral("Int8")
<< QStringLiteral("Uint16")
<< QStringLiteral("Int16")
<< QStringLiteral("Uint32")
<< QStringLiteral("Int32")
<< QStringLiteral("Float")
<< QStringLiteral("Double");
knownTypes<< typeUint8
<< typeInt8
<< typeUint16
<< typeInt16
<< typeUint32
<< typeInt32
<< typeFloat
<< typeDouble;
for(int i=0;i<typeStringList.count();i++) {
if(typeStringList.at(i).compare(typeString,Qt::CaseInsensitive) == 0) {
return knownTypes.at(i);
}
}
unknownType = true;
return FactMetaData::typeDouble;
}
bool FactMetaData::convertAndValidateRaw(const QVariant &rawValue, bool convertOnly, QVariant &convertValue, QString &errorString)
{
bool convertOk = false;
errorString.clear();
switch(type()) {
case typeUint8:
case typeUint16:
case typeUint32:
convertValue = rawValue.toUInt(&convertOk);
if(!convertOnly && convertOk) {
if(convertValue>rawMax() || convertValue<rawMin()) {
errorString = QString("Value must be within %1 and %2.").arg(rawMin().toUInt()).arg(rawMax().toUInt());
}
}
break;
case typeInt8:
case typeInt16:
case typeInt32:
convertValue = rawValue.toInt(&convertOk);
if(!convertOnly && convertOk) {
if(convertValue>rawMax() || convertValue<rawMin()) {
errorString = QString("Value must be within %1 and %2.").arg(rawMin().toInt()).arg(rawMax().toInt());
}
}
break;
case typeFloat:
convertValue = rawValue.toFloat(&convertOk);
if(!convertOnly && convertOk) {
if(convertValue>rawMax() || convertValue<rawMin()) {
errorString = QString("Value must be within %1 and %2.").arg(rawMin().toFloat()).arg(rawMax().toFloat());
}
}
break;
case typeDouble:
convertValue = rawValue.toDouble(&convertOk);
if(!convertOnly && convertOk) {
if(convertValue>rawMax() || convertValue<rawMin()) {
errorString = QString("Value must be within %1 and %2.").arg(rawMin().toDouble()).arg(rawMax().toDouble());
}
}
break;
}
return convertOk && errorString.isEmpty();
}
QVariant FactMetaData::rawDefaultValue() const
{
if(_defaultValueAvailable) {
return _rawDefaultValue;
}
else {
qWarning()<<"Attempt to access unavailable default value";
return QVariant(0);
}
}