From 016e360b98c60c15074bb1d19636c075de2dff4c Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Mon, 2 Apr 2018 02:54:13 +0100 Subject: [PATCH] dml: Fields don't need to know which Record they belong to This was preventing progress on the Python binding --- include/ki/dml/Field.h | 16 +++++++--------- include/ki/dml/FieldBase.h | 10 +++------- include/ki/dml/Record.h | 2 +- src/dml/FieldBase.cpp | 32 +++++++++++++------------------- src/dml/Record.cpp | 4 ++-- 5 files changed, 26 insertions(+), 38 deletions(-) diff --git a/include/ki/dml/Field.h b/include/ki/dml/Field.h index 4c7f435..6f158a8 100644 --- a/include/ki/dml/Field.h +++ b/include/ki/dml/Field.h @@ -15,6 +15,11 @@ namespace dml friend Record; friend FieldBase; public: + Field(std::string name) : FieldBase(name) + { + m_type_hash = typeid(ValueT).hash_code(); + m_value = ValueT(); + } virtual ~Field() = default; ValueT get_value() const @@ -112,13 +117,6 @@ namespace dml if (!value.empty()) set_value_from_string(value); } - protected: - Field(std::string name, const Record &record) - : FieldBase(name, record) - { - m_type_hash = typeid(ValueT).hash_code(); - m_value = ValueT(); - } private: ValueT m_value; @@ -126,9 +124,9 @@ namespace dml * Returns a new Field with the same name, transferability, * type, and value but with a different owner Record. */ - Field *clone(const Record &record) const final + Field *clone() const final { - auto *clone = new Field(m_name, record); + auto *clone = new Field(m_name); clone->m_transferable = true; clone->m_value = m_value; return clone; diff --git a/include/ki/dml/FieldBase.h b/include/ki/dml/FieldBase.h index 9451a00..6f7cc6d 100644 --- a/include/ki/dml/FieldBase.h +++ b/include/ki/dml/FieldBase.h @@ -19,9 +19,9 @@ namespace dml { friend Record; public: + FieldBase(std::string name); virtual ~FieldBase() = default; - const Record &get_record() const; std::string get_name() const; bool is_transferable() const; @@ -52,27 +52,23 @@ namespace dml /** * Creates a new Field from XML data. */ - static FieldBase *create_from_xml(const Record& record, const rapidxml::xml_node<> *node); + static FieldBase *create_from_xml(const rapidxml::xml_node<> *node); protected: std::string m_name; bool m_transferable; size_t m_type_hash; - FieldBase(std::string name, const Record& record); - /** * Returns a new Field with the same name, transferability * and value but with a different owner Record. */ - virtual FieldBase *clone(const Record &record) const = 0; + virtual FieldBase *clone() const = 0; /** * Copies the value of another Field into this one * if the types are the same. */ virtual void set_value(FieldBase *other) = 0; - private: - const Record &m_record; }; typedef std::vector FieldList; diff --git a/include/ki/dml/Record.h b/include/ki/dml/Record.h index b3c412a..4a55c7f 100644 --- a/include/ki/dml/Record.h +++ b/include/ki/dml/Record.h @@ -73,7 +73,7 @@ namespace dml } // Create the field - auto *field = new Field(name, *this); + auto *field = new Field(name); field->m_transferable = transferable; add_field(field); return field; diff --git a/src/dml/FieldBase.cpp b/src/dml/FieldBase.cpp index d1c9f0b..11f1464 100644 --- a/src/dml/FieldBase.cpp +++ b/src/dml/FieldBase.cpp @@ -5,19 +5,13 @@ namespace ki { namespace dml { - FieldBase::FieldBase(std::string name, const Record& record) - : m_record(record) + FieldBase::FieldBase(std::string name) { m_name = name; m_transferable = true; m_type_hash = 0; } - const Record &FieldBase::get_record() const - { - return m_record; - } - std::string FieldBase::get_name() const { return m_name; @@ -28,7 +22,7 @@ namespace dml return m_transferable; } - FieldBase* FieldBase::create_from_xml(const Record& record, const rapidxml::xml_node<>* node) + FieldBase* FieldBase::create_from_xml(const rapidxml::xml_node<>* node) { auto *type_attr = node->first_attribute("TYPE"); if (!type_attr) @@ -41,27 +35,27 @@ namespace dml FieldBase *field; if (type == "BYT") - field = new BytField("", record); + field = new BytField(""); else if (type == "UBYT") - field = new UBytField("", record); + field = new UBytField(""); else if (type == "SHRT") - field = new ShrtField("", record); + field = new ShrtField(""); else if (type == "USHRT") - field = new UShrtField("", record); + field = new UShrtField(""); else if (type == "INT") - field = new IntField("", record); + field = new IntField(""); else if (type == "UINT") - field = new UIntField("", record); + field = new UIntField(""); else if (type == "STR") - field = new StrField("", record); + field = new StrField(""); else if (type == "WSTR") - field = new WStrField("", record); + field = new WStrField(""); else if (type == "FLT") - field = new FltField("", record); + field = new FltField(""); else if (type == "DBL") - field = new DblField("", record); + field = new DblField(""); else if (type == "GID") - field = new GidField("", record); + field = new GidField(""); else { std::ostringstream oss; diff --git a/src/dml/Record.cpp b/src/dml/Record.cpp index 489e86f..9389a48 100644 --- a/src/dml/Record.cpp +++ b/src/dml/Record.cpp @@ -22,7 +22,7 @@ namespace dml Record::Record(const Record& record) { for (auto it = record.fields_begin(); it != record.fields_end(); ++it) - add_field((*it)->clone(*this)); + add_field((*it)->clone()); } bool Record::has_field(std::string name) const @@ -103,7 +103,7 @@ namespace dml for (auto *field_node = node->first_node(); field_node; field_node = field_node->next_sibling()) { - FieldBase *field = FieldBase::create_from_xml(*this, field_node); + FieldBase *field = FieldBase::create_from_xml(field_node); if (has_field(field->get_name())) { // Is the old field the same type as the one created from