| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef TDATAENTRY_H |
| 21 |
#define TDATAENTRY_H |
| 22 |
|
| 23 |
#include "tNamedEntry.h" |
| 24 |
#include "BiPointer.h" |
| 25 |
#include "tLabeller.h" |
| 26 |
#include "DataTypeComparator.h" |
| 27 |
|
| 28 |
|
| 29 |
#undef min |
| 30 |
#undef max |
| 31 |
|
| 32 |
|
| 33 |
class TDataInterval |
| 34 |
{ |
| 35 |
public: |
| 36 |
|
| 37 |
ComparableDT min; |
| 38 |
|
| 39 |
ComparableDT max; |
| 40 |
|
| 41 |
bool minExcl; |
| 42 |
|
| 43 |
bool maxExcl; |
| 44 |
|
| 45 |
public: |
| 46 |
|
| 47 |
|
| 48 |
void clear ( void ) { min = max = ComparableDT(); } |
| 49 |
|
| 50 |
|
| 51 |
bool hasMin ( void ) const { return min.inited(); } |
| 52 |
|
| 53 |
bool hasMax ( void ) const { return max.inited(); } |
| 54 |
|
| 55 |
bool empty ( void ) const { return !hasMin() && !hasMax(); } |
| 56 |
|
| 57 |
bool closed ( void ) const { return hasMin() && hasMax(); } |
| 58 |
|
| 59 |
|
| 60 |
bool updateMin ( bool excl, const ComparableDT& value ) |
| 61 |
{ |
| 62 |
if ( hasMin() ) |
| 63 |
{ |
| 64 |
|
| 65 |
if ( min > value ) |
| 66 |
return false; |
| 67 |
if ( min == value && |
| 68 |
minExcl && !excl ) |
| 69 |
return false; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
min = value; |
| 74 |
minExcl = min.correctMin(excl); |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
bool updateMax ( bool excl, const ComparableDT& value ) |
| 79 |
{ |
| 80 |
if ( hasMax() ) |
| 81 |
{ |
| 82 |
|
| 83 |
if ( max < value ) |
| 84 |
return false; |
| 85 |
if ( max == value && |
| 86 |
maxExcl && !excl ) |
| 87 |
return false; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
max = value; |
| 92 |
maxExcl = max.correctMax(excl); |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
bool update ( bool min, bool excl, const ComparableDT& value ) |
| 97 |
{ return min ? updateMin ( excl, value ) : updateMax ( excl, value ); } |
| 98 |
|
| 99 |
bool update ( const TDataInterval& Int ) |
| 100 |
{ |
| 101 |
bool ret = false; |
| 102 |
if ( Int.hasMin() ) |
| 103 |
ret |= updateMin ( Int.minExcl, Int.min ); |
| 104 |
if ( Int.hasMax() ) |
| 105 |
ret |= updateMax ( Int.maxExcl, Int.max ); |
| 106 |
return ret; |
| 107 |
} |
| 108 |
|
| 109 |
bool consistent ( const ComparableDT& dtype ) const |
| 110 |
{ |
| 111 |
if ( hasMin() && !min.compatible(dtype) ) |
| 112 |
return false; |
| 113 |
if ( hasMax() && !max.compatible(dtype) ) |
| 114 |
return false; |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
void Print ( std::ostream& o ) const |
| 120 |
{ |
| 121 |
if ( hasMin() ) |
| 122 |
o << (minExcl ? '(' : '[') << min; |
| 123 |
else |
| 124 |
o << '{'; |
| 125 |
o << ','; |
| 126 |
if ( hasMax() ) |
| 127 |
o << max << (maxExcl ? ')' : ']'); |
| 128 |
else |
| 129 |
o << '}'; |
| 130 |
} |
| 131 |
|
| 132 |
void printLISP ( std::ostream& o, const char* type ) const |
| 133 |
{ |
| 134 |
if ( hasMin() && hasMax() ) |
| 135 |
o << "(and "; |
| 136 |
if ( hasMin() ) |
| 137 |
{ |
| 138 |
o << "(g" << (minExcl ? 't' : 'e') << " (" << type; |
| 139 |
min.printValue(o); |
| 140 |
o << "))"; |
| 141 |
} |
| 142 |
if ( hasMax() ) |
| 143 |
{ |
| 144 |
o << "(l" << (maxExcl ? 't' : 'e') << " (" << type; |
| 145 |
max.printValue(o); |
| 146 |
o << "))"; |
| 147 |
} |
| 148 |
if ( hasMin() && hasMax() ) |
| 149 |
o << ")"; |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
class TDataEntry: public TNamedEntry |
| 155 |
{ |
| 156 |
private: |
| 157 |
|
| 158 |
TLabeller::LabelType rel; |
| 159 |
|
| 160 |
protected: |
| 161 |
|
| 162 |
const TDataEntry* Type; |
| 163 |
|
| 164 |
BipolarPointer pName; |
| 165 |
|
| 166 |
ComparableDT comp; |
| 167 |
|
| 168 |
TDataInterval Constraints; |
| 169 |
|
| 170 |
protected: |
| 171 |
|
| 172 |
void setComp ( const std::string& typeName ) |
| 173 |
{ |
| 174 |
|
| 175 |
if ( typeName == "string" ) |
| 176 |
comp = ComparableDT(getName()); |
| 177 |
else if ( typeName == "number" ) |
| 178 |
comp = ComparableDT(atol(getName())); |
| 179 |
else if ( typeName == "real" ) |
| 180 |
comp = ComparableDT((float)atof(getName())); |
| 181 |
else if ( typeName == "bool" ) |
| 182 |
comp = ComparableDT(getName()); |
| 183 |
else if ( typeName == "time" ) |
| 184 |
comp = ComparableDT ( atol(getName()), 0 ); |
| 185 |
else |
| 186 |
fpp_unreachable(); |
| 187 |
} |
| 188 |
|
| 189 |
public: |
| 190 |
|
| 191 |
explicit TDataEntry ( const std::string& name ) |
| 192 |
: TNamedEntry(name) |
| 193 |
, Type(nullptr) |
| 194 |
, pName(bpINVALID) |
| 195 |
, comp() |
| 196 |
{} |
| 197 |
|
| 198 |
TDataEntry ( const TDataEntry& ) = delete; |
| 199 |
|
| 200 |
TDataEntry& operator = ( const TDataEntry& ) = delete; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
bool isBasicDataType ( void ) const { return Type == nullptr && Constraints.empty(); } |
| 206 |
|
| 207 |
bool isRestrictedDataType ( void ) const { return !Constraints.empty(); } |
| 208 |
|
| 209 |
bool isDataValue ( void ) const { return Type != nullptr && Constraints.empty(); } |
| 210 |
|
| 211 |
|
| 212 |
void setHostType ( const TDataEntry* type ) { Type = type; setComp(type->getName()); } |
| 213 |
|
| 214 |
const TDataEntry* getType ( void ) const { return Type; } |
| 215 |
|
| 216 |
|
| 217 |
const ComparableDT& getComp ( void ) const { return comp; } |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
TDataInterval* getFacet ( void ) { return &Constraints; } |
| 223 |
|
| 224 |
const TDataInterval* getFacet ( void ) const { return &Constraints; } |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
bool isRelevant ( const TLabeller& lab ) const { return lab.isLabelled(rel); } |
| 230 |
|
| 231 |
void setRelevant ( const TLabeller& lab ) { lab.set(rel); } |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
BipolarPointer getBP ( void ) const { return pName; } |
| 237 |
|
| 238 |
void setBP ( BipolarPointer p ) { pName = p; } |
| 239 |
|
| 240 |
|
| 241 |
void printLISP ( std::ostream& o ) const |
| 242 |
{ |
| 243 |
o << ' '; |
| 244 |
if ( isBasicDataType() ) |
| 245 |
o << "(" << getName() << ")"; |
| 246 |
else if ( isDataValue() ) |
| 247 |
{ |
| 248 |
o << "(" << getType()->getName(); |
| 249 |
comp.printValue(o); |
| 250 |
o << ")"; |
| 251 |
} |
| 252 |
else if ( isRestrictedDataType() ) |
| 253 |
Constraints.printLISP ( o, getType()->getName() ); |
| 254 |
else |
| 255 |
fpp_unreachable(); |
| 256 |
} |
| 257 |
}; |
| 258 |
|
| 259 |
inline std::ostream& |
| 260 |
operator << ( std::ostream& o, const TDataInterval& c ) |
| 261 |
{ |
| 262 |
c.Print(o); |
| 263 |
return o; |
| 264 |
} |
| 265 |
|
| 266 |
#endif |