| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef DATATYPECOMPARATOR_H |
| 21 |
#define DATATYPECOMPARATOR_H |
| 22 |
|
| 23 |
#include <ctime> |
| 24 |
#include <string> |
| 25 |
#include <ostream> |
| 26 |
|
| 27 |
#include "fpp_assert.h" |
| 28 |
#include "globaldef.h" |
| 29 |
|
| 30 |
|
| 31 |
class ComparableDT |
| 32 |
{ |
| 33 |
private: |
| 34 |
|
| 35 |
std::string strValue; |
| 36 |
|
| 37 |
long longIntValue = 0; |
| 38 |
|
| 39 |
float floatValue = 0.0; |
| 40 |
|
| 41 |
long timeValue = 0; |
| 42 |
|
| 43 |
enum ValueType { UNUSED = 0, INT, STR, FLOAT, TIME } vType = UNUSED; |
| 44 |
|
| 45 |
public: |
| 46 |
|
| 47 |
ComparableDT() = default; |
| 48 |
|
| 49 |
explicit ComparableDT ( long int value ) |
| 50 |
: longIntValue(value) |
| 51 |
, vType(INT) |
| 52 |
{} |
| 53 |
|
| 54 |
explicit ComparableDT ( const char* value ) |
| 55 |
: strValue(value) |
| 56 |
, vType(STR) |
| 57 |
{} |
| 58 |
|
| 59 |
explicit ComparableDT ( float value ) |
| 60 |
: floatValue(value) |
| 61 |
, vType(FLOAT) |
| 62 |
{} |
| 63 |
|
| 64 |
explicit ComparableDT ( long value, int ) |
| 65 |
: timeValue(value) |
| 66 |
, vType(TIME) |
| 67 |
{} |
| 68 |
|
| 69 |
ComparableDT ( const ComparableDT& copy ) = default; |
| 70 |
|
| 71 |
ComparableDT& operator= ( const ComparableDT& copy ) = default; |
| 72 |
|
| 73 |
|
| 74 |
long int getLongIntValue ( void ) const { return longIntValue; } |
| 75 |
|
| 76 |
const std::string& getStringValue ( void ) const { return strValue; } |
| 77 |
|
| 78 |
float getFloatValue ( void ) const { return floatValue; } |
| 79 |
|
| 80 |
long getTimeValue ( void ) const { return timeValue; } |
| 81 |
|
| 82 |
bool hasDiscreteType ( void ) const { return vType == INT || vType == TIME; } |
| 83 |
|
| 84 |
bool inited ( void ) const { return vType != UNUSED; } |
| 85 |
|
| 86 |
bool compatible ( const ComparableDT& other ) const { return !inited() || !other.inited() || vType == other.vType; } |
| 87 |
|
| 88 |
bool correctMin ( bool excl ) |
| 89 |
{ |
| 90 |
if ( vType == INT && excl ) |
| 91 |
{ |
| 92 |
longIntValue++; |
| 93 |
return false; |
| 94 |
} |
| 95 |
if ( vType == TIME && excl ) |
| 96 |
{ |
| 97 |
timeValue++; |
| 98 |
return false; |
| 99 |
} |
| 100 |
return excl; |
| 101 |
} |
| 102 |
|
| 103 |
bool correctMax ( bool excl ) |
| 104 |
{ |
| 105 |
if ( vType == INT && excl ) |
| 106 |
{ |
| 107 |
longIntValue--; |
| 108 |
return false; |
| 109 |
} |
| 110 |
if ( vType == TIME && excl ) |
| 111 |
{ |
| 112 |
timeValue--; |
| 113 |
return false; |
| 114 |
} |
| 115 |
return excl; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
bool operator == ( const ComparableDT& other ) const |
| 122 |
{ |
| 123 |
fpp_assert ( vType == other.vType ); |
| 124 |
switch ( vType ) |
| 125 |
{ |
| 126 |
case INT: return getLongIntValue() == other.getLongIntValue(); |
| 127 |
case STR: return getStringValue() == other.getStringValue(); |
| 128 |
case FLOAT: return getFloatValue() == other.getFloatValue(); |
| 129 |
case TIME: return getTimeValue() == other.getTimeValue(); |
| 130 |
default: fpp_unreachable(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
bool operator != ( const ComparableDT& other ) const |
| 135 |
{ return !(*this == other); } |
| 136 |
|
| 137 |
bool operator < ( const ComparableDT& other ) const |
| 138 |
{ |
| 139 |
fpp_assert ( vType == other.vType ); |
| 140 |
switch ( vType ) |
| 141 |
{ |
| 142 |
case INT: return getLongIntValue() < other.getLongIntValue(); |
| 143 |
case STR: return getStringValue() < other.getStringValue(); |
| 144 |
case FLOAT: return getFloatValue() < other.getFloatValue(); |
| 145 |
case TIME: return getTimeValue() < other.getTimeValue(); |
| 146 |
default: fpp_unreachable(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
bool operator > ( const ComparableDT& other ) const { return other < *this; } |
| 151 |
|
| 152 |
|
| 153 |
std::ostream& printValue ( std::ostream& o ) const |
| 154 |
{ |
| 155 |
o << ' '; |
| 156 |
switch ( vType ) |
| 157 |
{ |
| 158 |
case INT: o << getLongIntValue(); break; |
| 159 |
case FLOAT: o << getFloatValue(); break; |
| 160 |
case STR: o << '"' << getStringValue() << '"'; break; |
| 161 |
case TIME: o << getTimeValue(); break; |
| 162 |
default: fpp_unreachable(); |
| 163 |
} |
| 164 |
return o; |
| 165 |
} |
| 166 |
friend std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt ); |
| 167 |
}; |
| 168 |
|
| 169 |
inline |
| 170 |
std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt ) |
| 171 |
{ |
| 172 |
switch ( cdt.vType ) |
| 173 |
{ |
| 174 |
case ComparableDT::INT: o << cdt.getLongIntValue(); break; |
| 175 |
case ComparableDT::STR: o << cdt.getStringValue(); break; |
| 176 |
case ComparableDT::FLOAT: o << cdt.getFloatValue(); break; |
| 177 |
case ComparableDT::TIME: o << cdt.getTimeValue(); break; |
| 178 |
default: fpp_unreachable(); |
| 179 |
} |
| 180 |
return o; |
| 181 |
} |
| 182 |
|
| 183 |
#endif |