From 7825e172513d39be0a7e949db5491112585bc8ce Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 25 Jun 2016 07:16:14 +0200 Subject: [PATCH] added helper method --- src/lib/ebus/datatype.cpp | 16 ++++++++-------- src/lib/ebus/datatype.h | 40 ++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index 34565e8d..403b6839 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -491,16 +491,16 @@ result_t DateTimeDataType::writeSymbols(istringstream& input, } -NumberDataType::NumberDataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, - const unsigned int minValue, const unsigned int maxValue, const int divisor) - : DataType(id, bitCount, flags, replacement), m_minValue(minValue), m_maxValue(maxValue), m_divisor(divisor), m_firstBit(0), m_baseType(NULL) -{ +unsigned char NumberDataType::calcPrecision(const int divisor) { unsigned char precision = 0; - if (divisor > 1) - for (unsigned int exp = 1; exp < MAX_DIVISOR; exp *= 10, precision++) - if (exp >= (unsigned int)divisor) + if (divisor > 1) { + for (unsigned int exp = 1; exp < MAX_DIVISOR; exp *= 10, precision++) { + if (exp >= (unsigned int)divisor) { break; - m_precision = precision; + } + } + } + return precision; } bool NumberDataType::dump(ostream& output, unsigned char length) const diff --git a/src/lib/ebus/datatype.h b/src/lib/ebus/datatype.h index c29a0f96..1f34baa0 100644 --- a/src/lib/ebus/datatype.h +++ b/src/lib/ebus/datatype.h @@ -40,9 +40,6 @@ * using one of these base data types with certain flags, such as #BCD, #FIX, * #REQ, see @a DataType. * - * The list of available base data types is stored in #baseTypes and can easily - * be extended if necessary. - * * Each @a DataType can be converted from a @a SymbolString to an * @a ostringstream (see @a DataType#readSymbols() methods) or vice versa from * an @a istringstream to a @a SymbolString (see @a DataType#writeSymbols()). @@ -97,7 +94,7 @@ static const unsigned int REV = 0x04; //!< reverted binary representation (most static const unsigned int SIG = 0x08; //!< signed value static const unsigned int IGN = 0x10; //!< ignore value during read and write static const unsigned int FIX = 0x20; //!< fixed width formatting -static const unsigned int REQ = 0x40;//!< value may not be NULL +static const unsigned int REQ = 0x40; //!< value may not be NULL static const unsigned int HCD = 0x80; //!< binary representation is hex converted to decimal and interpreted as 2 digits (also requires #BCD) static const unsigned int EXP = 0x100; //!< exponential numeric representation static const unsigned int LAST_DATATYPE_FLAG = EXP; //!< the last flag value used by @a DataType and children @@ -139,6 +136,7 @@ int parseSignedInt(const char* str, int base, const int minValue, const int maxV */ void printErrorPos(ostream& out, vector::iterator begin, const vector::iterator end, vector::iterator pos, string filename, size_t lineNo, result_t result); + /** * Base class for all kinds of data types. */ @@ -256,6 +254,7 @@ protected: }; + /** * A string based @a DataType. */ @@ -302,6 +301,7 @@ private: }; + /** * A date/time based @a DataType. */ @@ -364,7 +364,7 @@ private: /** - * Base class for numeric @a DataType. + * A number based @a DataType. */ class NumberDataType : public DataType { @@ -375,19 +375,20 @@ public: * @param id the type identifier. * @param bitCount the number of bits (maximum length if #ADJ flag is set, must be multiple of 8 with flag #BCD). * @param flags the combination of flags (like #BCD). - * @param replacement the replacement value (no replacement if equal to #minValue). + * @param replacement the replacement value (no replacement if equal to minValue). * @param minValue the minimum raw value. * @param maxValue the maximum raw value. * @param divisor the divisor (negative for reciprocal). */ NumberDataType(const char* id, const unsigned char bitCount, const unsigned short flags, const unsigned int replacement, - const unsigned int minValue, const unsigned int maxValue, const int divisor); + const unsigned int minValue, const unsigned int maxValue, const int divisor) + : DataType(id, bitCount, flags, replacement), m_minValue(minValue), m_maxValue(maxValue), m_divisor(divisor), m_precision(calcPrecision(divisor)), m_firstBit(0), m_baseType(NULL) {} /** * Constructs a new instance for less than 8 bits. * @param id the type identifier. * @param bitCount the number of bits (maximum length if #ADJ flag is set). - * @param flags the combination of flags (like #ADJ, must not include flag #BCD). + * @param flags the combination of flags (like #ADJ, may not include flag #BCD). * @param replacement the replacement value (no replacement if zero). * @param firstBit the offset to the first bit. * @param divisor the divisor (negative for reciprocal). @@ -401,13 +402,25 @@ public: */ virtual ~NumberDataType() {} - // @copydoc + /** + * Calculate the precision from the divisor. + * + * @param divisor the divisor (negative for reciprocal). + * @return the precision for formatting the value. + */ + static unsigned char calcPrecision(const int divisor); + + // @copydoc virtual bool dump(ostream& output, const unsigned char length) const; /** * Derive a new @a NumberDataType from this. - * @param divisor the extra divisor (negative for reciprocal) to apply, or 1 for none (if applicable). - * @param derived the derived @a NumberDataType, or this if derivation is not necessary. + * @param divisor the extra divisor (negative for reciprocal) to apply, or + * 1 for none (if applicable), or 0 to keep the current value. + * @param bitCount the number of bits (maximum length if #ADJ flag is set, + * must be multiple of 8 with flag #BCD), or 0 to keep the current value. + * @param derived the derived @a NumberDataType, or this if derivation is + * not necessary. * @return @a RESULT_OK on success, or an error code. */ result_t derive(int divisor, unsigned char bitCount, NumberDataType* &derived); @@ -453,7 +466,8 @@ public: * @param offset the offset in the @a SymbolString. * @param length the number of symbols to write, or @a REMAIN_LEN. * @param output the unescaped @a SymbolString to write the binary value to. - * @param length the variable in which to store the used length in bytes, or NULL. + * @param usedLength the variable in which to store the used length in bytes, + * or NULL. * @return @a RESULT_OK on success, or an error code. */ virtual result_t writeRawValue(unsigned int value, @@ -477,7 +491,7 @@ private: const int m_divisor; /** the precision for formatting the value. */ - unsigned char m_precision; + const unsigned char m_precision; /** the offset to the first bit. */ const short m_firstBit;