diff --git a/include/ki/dml/Field.h b/include/ki/dml/Field.h index b288909..6facc9a 100644 --- a/include/ki/dml/Field.h +++ b/include/ki/dml/Field.h @@ -116,6 +116,9 @@ namespace dml if (!value.empty()) set_value_from_string(value); } + + std::string get_value_string() const override final; + void set_value_from_string(std::string value) override final; private: ValueT m_value; @@ -126,7 +129,7 @@ namespace dml Field *clone() const override final { auto *clone = new Field(m_name); - clone->m_transferable = true; + clone->m_transferable = m_transferable; clone->m_value = m_value; return clone; } @@ -151,9 +154,6 @@ namespace dml throw value_error(oss.str()); } } - - std::string get_value_string() const; - void set_value_from_string(std::string value); }; typedef Field BytField; @@ -183,6 +183,18 @@ namespace dml iss >> m_value; } + template <> + std::string BytField::get_value_string() const; + + template <> + void BytField::set_value_from_string(std::string value); + + template <> + std::string UBytField::get_value_string() const; + + template <> + void UBytField::set_value_from_string(std::string value); + template <> std::string StrField::get_value_string() const; diff --git a/include/ki/dml/FieldBase.h b/include/ki/dml/FieldBase.h index 6f7cc6d..509bebe 100644 --- a/include/ki/dml/FieldBase.h +++ b/include/ki/dml/FieldBase.h @@ -53,6 +53,9 @@ namespace dml * Creates a new Field from XML data. */ static FieldBase *create_from_xml(const rapidxml::xml_node<> *node); + + virtual std::string get_value_string() const = 0; + virtual void set_value_from_string(std::string value) = 0; protected: std::string m_name; bool m_transferable; diff --git a/include/ki/dml/Record.h b/include/ki/dml/Record.h index cc70dd7..f1e1079 100644 --- a/include/ki/dml/Record.h +++ b/include/ki/dml/Record.h @@ -34,6 +34,8 @@ namespace dml return m_field_map.at(name)->is_type(); } + FieldBase *get_field(std::string name) const; + /** * Returns a previously added field with the specified name * and type. diff --git a/src/dml/Record.cpp b/src/dml/Record.cpp index 9389a48..6cb7c0e 100644 --- a/src/dml/Record.cpp +++ b/src/dml/Record.cpp @@ -30,6 +30,13 @@ namespace dml return m_field_map.count(name); } + FieldBase *Record::get_field(std::string name) const + { + if (has_field(name)) + return m_field_map.at(name); + return nullptr; + } + size_t Record::get_field_count() const { return m_fields.size(); @@ -103,6 +110,9 @@ namespace dml for (auto *field_node = node->first_node(); field_node; field_node = field_node->next_sibling()) { + if (field_node->type() != rapidxml::node_type::node_element) + continue; + FieldBase *field = FieldBase::create_from_xml(field_node); if (has_field(field->get_name())) { diff --git a/src/dml/types/BytField.cpp b/src/dml/types/BytField.cpp index a2b088d..7b0a0f4 100644 --- a/src/dml/types/BytField.cpp +++ b/src/dml/types/BytField.cpp @@ -38,5 +38,22 @@ namespace dml { return "BYT"; } + + template <> + std::string BytField::get_value_string() const + { + std::ostringstream oss; + oss << (int16_t)m_value; + return oss.str(); + } + + template <> + void BytField::set_value_from_string(std::string value) + { + std::istringstream iss(value); + int16_t temp = 0; + iss >> temp; + m_value = temp & 0xFF; + } } } \ No newline at end of file diff --git a/src/dml/types/UBytField.cpp b/src/dml/types/UBytField.cpp index e470230..0534c80 100644 --- a/src/dml/types/UBytField.cpp +++ b/src/dml/types/UBytField.cpp @@ -38,5 +38,22 @@ namespace dml { return "UBYT"; } + + template <> + std::string UBytField::get_value_string() const + { + std::ostringstream oss; + oss << (uint16_t)m_value; + return oss.str(); + } + + template <> + void UBytField::set_value_from_string(std::string value) + { + std::istringstream iss(value); + uint16_t temp = 0; + iss >> temp; + m_value = temp & 0xFF; + } } }