1 #ifndef FIXPRECDEC_H 2 #define FIXPRECDEC_H 3 4 #include "attribute_.h" 5 #include "types.h" 6 7 /** 8 * @brief A class for fixed precision decimals encoded in the type int. 9 * The unit is globally fixed to FixPrecDec::unit_; 10 */ 11 class FixPrecDec 12 { 13 public: 14 int value_; 15 static const int unit_; //! the number one encoded as a FixPrecDec 16 17 std::string str() const; 18 19 bool operator<(double other); 20 bool operator>(double other); 21 22 bool operator<(FixPrecDec other) { 23 return value_ < other.value_; 24 } 25 26 bool operator>(FixPrecDec other) { 27 return value_ > other.value_; 28 } 29 fromInteger(int integer)30 static FixPrecDec fromInteger(int integer) { 31 return integer * unit_; 32 } 33 34 FixPrecDec operator+(FixPrecDec other) { 35 return value_ + other.value_; 36 } 37 FixPrecDec operator-(FixPrecDec other) { 38 return value_ - other.value_; 39 } 40 FixPrecDec operator/(int divisor) { 41 return value_ / divisor; 42 } raw(int value)43 static FixPrecDec raw(int value) { return value; } 44 static FixPrecDec approxFrac(int nominator, int denominator); 45 private: FixPrecDec(int value)46 FixPrecDec(int value) : value_(value) {} 47 }; 48 49 template<> FixPrecDec Converter<FixPrecDec>::parse(const std::string& source); 50 template<> std::string Converter<FixPrecDec>::str(FixPrecDec payload); 51 52 template<> staticType()53inline Type Attribute_<FixPrecDec>::staticType() { return Type::DECIMAL; } 54 55 56 #endif // FIXPRECDEC_H 57