solved most TODOs, introduced reference types and chains, use result_t where possible, switched to divisor instead of factor, formatting, introduced VALUE_SEPARATOR for possible switch to real CSV (later), added value range check, small optimizations
This commit is contained in:
+45
-37
@@ -21,6 +21,7 @@
|
||||
#define LIBEBUS_DATA_H_
|
||||
|
||||
#include "symbol.h"
|
||||
#include "result.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@@ -39,10 +40,9 @@ enum PartType {
|
||||
enum BaseType {
|
||||
bt_str, // text string in a StringDataField
|
||||
bt_hexstr, // hex digit string in a StringDataField
|
||||
bt_date, // date in a StringDataField
|
||||
bt_time, // time in a StringDataField
|
||||
bt_list, // numeric list value in a ValueListDataField
|
||||
bt_number // number value in a NumberDataField
|
||||
bt_dat, // date in a StringDataField
|
||||
bt_tim, // time in a StringDataField
|
||||
bt_num, // numeric value in a NumericDataField
|
||||
};
|
||||
|
||||
/** flags for dataType_t. */
|
||||
@@ -50,7 +50,7 @@ const unsigned int ADJ = 0x01; // adjustable length, numBytes is maximum length
|
||||
const unsigned int BCD = 0x02; // binary representation is BCD
|
||||
const unsigned int REV = 0x04; // reverted binary representation (most significant byte first)
|
||||
const unsigned int SIG = 0x08; // signed value
|
||||
const unsigned int LST = 0x10; // value list is possible (without applied factor)
|
||||
const unsigned int LST = 0x10; // value list is possible (without applied divisor)
|
||||
const unsigned int DAY = 0x20; // default value list is week days
|
||||
|
||||
/** the structure for defining field types with their properties. */
|
||||
@@ -97,43 +97,55 @@ public:
|
||||
/**
|
||||
* @brief Factory method for creating a new instance.
|
||||
* @param dstAddress the destination bus address.
|
||||
* @param isSetMessage whther the field is part of a set message.
|
||||
* @param isSetMessage whether the field is part of a set message.
|
||||
* @param it the iterator to traverse for the definition parts.
|
||||
* @param end the iterator pointing to the end of the definition parts.
|
||||
* @param predefined a map of predefined DataFields to be referenced by name.
|
||||
* @param fields the vector to which created instances are added.
|
||||
* @param nextPos the variable holding the next subsequent position.
|
||||
* @return RESULT_OK on success, RESULT_ERR_EOF if the iterator is empty, or an error code.
|
||||
* Note: the caller needs to cleanup created instances.
|
||||
*/
|
||||
static DataField* create(const unsigned char dstAddress, const bool isSetMessage,
|
||||
std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end);
|
||||
static result_t create(const unsigned char dstAddress, const bool isSetMessage,
|
||||
std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end,
|
||||
const std::map<std::string, DataField*> predefined, std::vector<DataField*>& fields,
|
||||
unsigned char& nextPos);
|
||||
|
||||
/**
|
||||
* @brief Reads the value from the master or slave @a SymbolString.
|
||||
* @param masterData the unescaped master data @a SymbolString for reading binary data.
|
||||
* @param slaveData the unescaped slave data @a SymbolString for reading binary data.
|
||||
* @return the formatted value as string.
|
||||
* @param output the ostringstream to append the formatted value to.
|
||||
* @param vervose whether to prepend the name, append the unit (if present), and append
|
||||
* the comment in square brackets (if present).
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
const std::string read(SymbolString& masterData, SymbolString& slaveData, bool verbose=false);
|
||||
result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output,
|
||||
bool verbose=false);
|
||||
/**
|
||||
* @brief Writes the value to the master or slave @a SymbolString.
|
||||
* @param masterData the unescaped master data @a SymbolString for writing binary data.
|
||||
* @param slaveData the unescaped slave data @a SymbolString for writing binary data.
|
||||
* @param value the formatted value as string.
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
bool write(const std::string& value, SymbolString& masterData, SymbolString& slaveData);
|
||||
result_t write(const std::string& value, SymbolString& masterData, SymbolString& slaveData);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Internal method for reading the field from a @a SymbolString.
|
||||
* @param input the unescaped @a SymbolString to read the binary value from.
|
||||
* @param output the ostringstream to append the formatted value to.
|
||||
* @return true if the value was parsed successfully.
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
virtual bool readSymbols(SymbolString& input, std::ostringstream& output) = 0;
|
||||
virtual result_t readSymbols(SymbolString& input, std::ostringstream& output) = 0;
|
||||
/**
|
||||
* @brief Internal method for writing the field to a @a SymbolString.
|
||||
* @param input the istringstream to parse the formatted value from.
|
||||
* @param output the unescaped @a SymbolString to write the binary value to.
|
||||
* @return true if the value was formatted successfully.
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
virtual bool writeSymbols(std::istringstream& input, SymbolString& output) = 0;
|
||||
virtual result_t writeSymbols(std::istringstream& input, SymbolString& output) = 0;
|
||||
|
||||
/** the field name. */
|
||||
const std::string m_name;
|
||||
@@ -179,8 +191,8 @@ public:
|
||||
virtual ~StringDataField() {}
|
||||
|
||||
protected:
|
||||
virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
virtual result_t readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual result_t writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
|
||||
};
|
||||
|
||||
@@ -198,9 +210,8 @@ public:
|
||||
* @param offset the offset to the first symbol in the message part in which the field is stored.
|
||||
* @param length the number of symbols in the message part in which the field is stored.
|
||||
* @param dataType the data type definition.
|
||||
* @param comment the field comment.
|
||||
* @param unit the value unit.
|
||||
* @param replacement the (binary) replacement value to use if the value is not set.
|
||||
* @param comment the field comment.
|
||||
*/
|
||||
NumericDataField(const std::string name, const PartType partType,
|
||||
const unsigned char offset, const unsigned char length,
|
||||
@@ -217,16 +228,16 @@ protected:
|
||||
* @brief Internal method for reading the raw value from a @a SymbolString.
|
||||
* @param input the unescaped @a SymbolString to read the binary value from.
|
||||
* @param value the variable in which to store the raw value.
|
||||
* @return true if the value was read successfully.
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
bool readRawValue(SymbolString& input, unsigned int& value);
|
||||
result_t readRawValue(SymbolString& input, unsigned int& value);
|
||||
/**
|
||||
* @brief Internal method for writing the raw value to a @a SymbolString.
|
||||
* @param value the raw value to write.
|
||||
* @param output the unescaped @a SymbolString to write the binary value to.
|
||||
* @return true if the value was written successfully.
|
||||
* @return RESULT_OK on success, or an error code.
|
||||
*/
|
||||
bool writeRawValue(unsigned int value, SymbolString& output);
|
||||
result_t writeRawValue(unsigned int value, SymbolString& output);
|
||||
|
||||
};
|
||||
|
||||
@@ -243,28 +254,27 @@ public:
|
||||
* @param offset the offset to the first symbol in the message part in which the field is stored.
|
||||
* @param length the number of symbols in the message part in which the field is stored.
|
||||
* @param dataType the data type definition.
|
||||
* @param comment the field comment.
|
||||
* @param unit the value unit.
|
||||
* @param replacement the (binary) replacement value to use if the value is not set.
|
||||
* @param factor the factor to apply on the value.
|
||||
* @param comment the field comment.
|
||||
* @param divisor the extra divisor to apply on the value, or 1 for none.
|
||||
*/
|
||||
NumberDataField(const std::string name, const PartType partType,
|
||||
const unsigned char offset, const unsigned char length,
|
||||
const dataType_t dataType, const std::string unit,
|
||||
const std::string comment, const float factor)
|
||||
const std::string comment, const unsigned int divisor)
|
||||
: NumericDataField(name, partType, offset, length, dataType, unit, comment),
|
||||
m_factor(factor / dataType.divisor) {}
|
||||
m_divisor(divisor * dataType.divisor) {}
|
||||
/**
|
||||
* @brief Destructor.
|
||||
*/
|
||||
virtual ~NumberDataField() {}
|
||||
|
||||
protected:
|
||||
virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
virtual result_t readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual result_t writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
|
||||
/** the factor to apply on the value. */
|
||||
const float m_factor;
|
||||
/** the combined divisor to apply on the value, or 1 for none. */
|
||||
const unsigned int m_divisor;
|
||||
|
||||
};
|
||||
|
||||
@@ -281,10 +291,8 @@ public:
|
||||
* @param offset the offset to the first symbol in the message part in which the field is stored.
|
||||
* @param length the number of symbols in the message part in which the field is stored.
|
||||
* @param dataType the data type definition.
|
||||
* @param comment the field comment.
|
||||
* @param unit the value unit.
|
||||
* @param factor the factor to apply on the value.
|
||||
* @param replacement the (binary) replacement value to use if the value is not set.
|
||||
* @param comment the field comment.
|
||||
* @param values the value=text assignments.
|
||||
*/
|
||||
ValueListDataField(const std::string name, const PartType partType,
|
||||
@@ -299,8 +307,8 @@ public:
|
||||
virtual ~ValueListDataField() {}
|
||||
|
||||
protected:
|
||||
virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
virtual result_t readSymbols(SymbolString& input, std::ostringstream& output);
|
||||
virtual result_t writeSymbols(std::istringstream& input, SymbolString& output);
|
||||
|
||||
/** the value=text assignments. */
|
||||
std::map<unsigned int, std::string> m_values;
|
||||
|
||||
Reference in New Issue
Block a user