fixed getNextOffset() and added optional PartType argument, renamed pt_template to pt_any, added getPartType()

This commit is contained in:
john30
2014-11-22 09:41:11 +01:00
parent 54de8319c9
commit 8461c66103
2 changed files with 33 additions and 16 deletions
+20 -9
View File
@@ -165,7 +165,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
posStr++; posStr++;
} }
else if (isTemplate) { else if (isTemplate) {
partType = pt_template; partType = pt_any;
} }
else { else {
result = RESULT_ERR_INVALID_ARG; result = RESULT_ERR_INVALID_ARG;
@@ -174,7 +174,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
if (posStr[0] == 0) { if (posStr[0] == 0) {
if (fields.empty() == false) if (fields.empty() == false)
offset = fields.back()->getNextOffset(); offset = fields.back()->getNextOffset(partType);
else else
offset = 0; offset = 0;
length = 0; length = 0;
@@ -274,7 +274,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
result = ref->second->derive(name, comment, unit, partType, offset, divisor, values, fields); result = ref->second->derive(name, comment, unit, partType, offset, divisor, values, fields);
if (result != RESULT_OK) if (result != RESULT_OK)
break; break;
offset = fields.back()->getNextOffset(); offset = fields.back()->getNextOffset(partType);
} }
if (offset > MAX_POS) { if (offset > MAX_POS) {
result = RESULT_ERR_INVALID_ARG; // invalid pos definition result = RESULT_ERR_INVALID_ARG; // invalid pos definition
@@ -362,8 +362,11 @@ result_t DataField::create(std::vector<std::string>::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; unsigned char offset = m_offset + m_length;
if ((m_dataType.numBits % 8) != 0 if ((m_dataType.numBits % 8) != 0
&& m_dataType.precisionOrFirstBit + (m_dataType.numBits % 8) < 8) && 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<unsigned int, std::string> values, unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields) std::vector<SingleDataField*>& 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 return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
if (divisor != 0 || values.empty() == false) if (divisor != 0 || values.empty() == false)
return RESULT_ERR_INVALID_ARG; // cannot set divisor or values for string field 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<unsigned int, std::string> values, unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields) std::vector<SingleDataField*>& 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 return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
if (name.empty() == true) if (name.empty() == true)
name = m_name; name = m_name;
@@ -840,7 +843,7 @@ result_t ValueListDataField::derive(std::string name, std::string comment,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields) std::vector<SingleDataField*>& 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 return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
if (name.empty() == true) if (name.empty() == true)
name = m_name; 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<SingleDataField*>::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, result_t DataFieldSet::derive(std::string name, std::string comment,
+13 -7
View File
@@ -28,7 +28,7 @@
/** the message part in which a data field is stored. */ /** the message part in which a data field is stored. */
enum PartType { 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_masterData, // stored in master data
pt_slaveData, // stored in slave data pt_slaveData, // stored in slave data
}; };
@@ -112,9 +112,10 @@ public:
const bool isSetMessage=false, const unsigned char dstAddress=SYN); 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. * @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. * @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. * @brief Derives a new DataField from this field.
* @param name the field name. * @param name the field name.
@@ -134,12 +135,12 @@ public:
* @brief Get the field name. * @brief Get the field name.
* @return 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. * @brief Get the field comment.
* @return 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. * @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data. * @param masterData the unescaped master data @a SymbolString for reading binary data.
@@ -204,9 +205,14 @@ public:
* @brief Get the value unit. * @brief Get the value unit.
* @return 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 // @copydoc
virtual unsigned char getNextOffset(); virtual unsigned char getNextOffset(PartType partType=pt_any);
/** /**
* @brief Reads the value from the master or slave @a SymbolString. * @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data. * @param masterData the unescaped master data @a SymbolString for reading binary data.
@@ -470,7 +476,7 @@ public:
*/ */
virtual ~DataFieldSet(); virtual ~DataFieldSet();
// @copydoc // @copydoc
virtual unsigned char getNextOffset(); virtual unsigned char getNextOffset(PartType partType=pt_any);
// @copydoc // @copydoc
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType, unsigned char offset, std::string unit, const PartType partType, unsigned char offset,