From 8461c66103cc11a7c8a1c999e9ce266eb1d1c6ee Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 22 Nov 2014 09:41:11 +0100 Subject: [PATCH] fixed getNextOffset() and added optional PartType argument, renamed pt_template to pt_any, added getPartType() --- src/lib/ebus/data.cpp | 29 ++++++++++++++++++++--------- src/lib/ebus/data.h | 20 +++++++++++++------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 413b9edc..b993330e 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -165,7 +165,7 @@ result_t DataField::create(std::vector::iterator& it, posStr++; } else if (isTemplate) { - partType = pt_template; + partType = pt_any; } else { result = RESULT_ERR_INVALID_ARG; @@ -174,7 +174,7 @@ result_t DataField::create(std::vector::iterator& it, if (posStr[0] == 0) { if (fields.empty() == false) - offset = fields.back()->getNextOffset(); + offset = fields.back()->getNextOffset(partType); else offset = 0; length = 0; @@ -274,7 +274,7 @@ result_t DataField::create(std::vector::iterator& it, result = ref->second->derive(name, comment, unit, partType, offset, divisor, values, fields); if (result != RESULT_OK) break; - offset = fields.back()->getNextOffset(); + offset = fields.back()->getNextOffset(partType); } if (offset > MAX_POS) { result = RESULT_ERR_INVALID_ARG; // invalid pos definition @@ -362,8 +362,11 @@ result_t DataField::create(std::vector::iterator& it, } -unsigned char SingleDataField::getNextOffset() +unsigned char SingleDataField::getNextOffset(PartType partType) { + if (partType != pt_any && partType != m_partType) + return 0; + unsigned char offset = m_offset + m_length; if ((m_dataType.numBits % 8) != 0 && m_dataType.precisionOrFirstBit + (m_dataType.numBits % 8) < 8) @@ -429,7 +432,7 @@ result_t StringDataField::derive(std::string name, std::string comment, unsigned int divisor, std::map values, std::vector& fields) { - if (m_partType != pt_template && partType == pt_template) + if (m_partType != pt_any && partType == pt_any) return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance if (divisor != 0 || values.empty() == false) return RESULT_ERR_INVALID_ARG; // cannot set divisor or values for string field @@ -706,7 +709,7 @@ result_t NumberDataField::derive(std::string name, std::string comment, unsigned int divisor, std::map values, std::vector& fields) { - if (m_partType != pt_template && partType == pt_template) + if (m_partType != pt_any && partType == pt_any) return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance if (name.empty() == true) name = m_name; @@ -840,7 +843,7 @@ result_t ValueListDataField::derive(std::string name, std::string comment, unsigned int divisor, std::map values, std::vector& fields) { - if (m_partType != pt_template && partType == pt_template) + if (m_partType != pt_any && partType == pt_any) return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance if (name.empty() == true) name = m_name; @@ -911,9 +914,17 @@ DataFieldSet::~DataFieldSet() } } -unsigned char DataFieldSet::getNextOffset() +unsigned char DataFieldSet::getNextOffset(PartType partType) { - return m_fields.back()->getNextOffset(); + return 0; + + for (std::vector::reverse_iterator it = m_fields.rbegin(); it < m_fields.rend(); it++) { + SingleDataField* field = *it; + if (partType == pt_any || partType == field->getPartType()) + return field->getNextOffset(partType); + } + + return 0; } result_t DataFieldSet::derive(std::string name, std::string comment, diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index 747f9a8b..c2fa4ec4 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -28,7 +28,7 @@ /** the message part in which a data field is stored. */ enum PartType { - pt_template, // special part type for templates (relative offset) + pt_any, // stored in any data (master or slave, relative offset) pt_masterData, // stored in master data pt_slaveData, // stored in slave data }; @@ -112,9 +112,10 @@ public: const bool isSetMessage=false, const unsigned char dstAddress=SYN); /** * @brief Returns the offset to the first symbol in the message part for a field following this field. + * @param partType the message part for which to get the offset, or @a pt_any for any. * @return the offset to the first symbol in the message part for a field following this field. */ - virtual unsigned char getNextOffset() = 0; + virtual unsigned char getNextOffset(PartType partType=pt_any) = 0; /** * @brief Derives a new DataField from this field. * @param name the field name. @@ -134,12 +135,12 @@ public: * @brief Get the field name. * @return the field name. */ - const std::string getName() { return m_name; } + std::string getName() const { return m_name; } /** * @brief Get the field comment. * @return the field comment. */ - const std::string getComment() { return m_comment; } + std::string getComment() const { return m_comment; } /** * @brief Reads the value from the master or slave @a SymbolString. * @param masterData the unescaped master data @a SymbolString for reading binary data. @@ -204,9 +205,14 @@ public: * @brief Get the value unit. * @return the value unit. */ - const std::string getUnit() { return m_unit; } + std::string getUnit() const { return m_unit; } + /** + * @brief Get the message part in which the field is stored. + * @return the message part in which the field is stored. + */ + PartType getPartType() const { return m_partType; } // @copydoc - virtual unsigned char getNextOffset(); + virtual unsigned char getNextOffset(PartType partType=pt_any); /** * @brief Reads the value from the master or slave @a SymbolString. * @param masterData the unescaped master data @a SymbolString for reading binary data. @@ -470,7 +476,7 @@ public: */ virtual ~DataFieldSet(); // @copydoc - virtual unsigned char getNextOffset(); + virtual unsigned char getNextOffset(PartType partType=pt_any); // @copydoc virtual result_t derive(std::string name, std::string comment, std::string unit, const PartType partType, unsigned char offset,