dml: Fields don't need to know which Record they belong to

This was preventing progress on the Python binding
This commit is contained in:
Joshua Scott 2018-04-02 02:54:13 +01:00
parent 9034c893a7
commit 016e360b98
5 changed files with 26 additions and 38 deletions

View File

@ -15,6 +15,11 @@ namespace dml
friend Record; friend Record;
friend FieldBase; friend FieldBase;
public: public:
Field(std::string name) : FieldBase(name)
{
m_type_hash = typeid(ValueT).hash_code();
m_value = ValueT();
}
virtual ~Field() = default; virtual ~Field() = default;
ValueT get_value() const ValueT get_value() const
@ -112,13 +117,6 @@ namespace dml
if (!value.empty()) if (!value.empty())
set_value_from_string(value); 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: private:
ValueT m_value; ValueT m_value;
@ -126,9 +124,9 @@ namespace dml
* Returns a new Field with the same name, transferability, * Returns a new Field with the same name, transferability,
* type, and value but with a different owner Record. * type, and value but with a different owner Record.
*/ */
Field<ValueT> *clone(const Record &record) const final Field<ValueT> *clone() const final
{ {
auto *clone = new Field<ValueT>(m_name, record); auto *clone = new Field<ValueT>(m_name);
clone->m_transferable = true; clone->m_transferable = true;
clone->m_value = m_value; clone->m_value = m_value;
return clone; return clone;

View File

@ -19,9 +19,9 @@ namespace dml
{ {
friend Record; friend Record;
public: public:
FieldBase(std::string name);
virtual ~FieldBase() = default; virtual ~FieldBase() = default;
const Record &get_record() const;
std::string get_name() const; std::string get_name() const;
bool is_transferable() const; bool is_transferable() const;
@ -52,27 +52,23 @@ namespace dml
/** /**
* Creates a new Field from XML data. * 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: protected:
std::string m_name; std::string m_name;
bool m_transferable; bool m_transferable;
size_t m_type_hash; size_t m_type_hash;
FieldBase(std::string name, const Record& record);
/** /**
* Returns a new Field with the same name, transferability * Returns a new Field with the same name, transferability
* and value but with a different owner Record. * 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 * Copies the value of another Field into this one
* if the types are the same. * if the types are the same.
*/ */
virtual void set_value(FieldBase *other) = 0; virtual void set_value(FieldBase *other) = 0;
private:
const Record &m_record;
}; };
typedef std::vector<FieldBase *> FieldList; typedef std::vector<FieldBase *> FieldList;

View File

@ -73,7 +73,7 @@ namespace dml
} }
// Create the field // Create the field
auto *field = new Field<ValueT>(name, *this); auto *field = new Field<ValueT>(name);
field->m_transferable = transferable; field->m_transferable = transferable;
add_field(field); add_field(field);
return field; return field;

View File

@ -5,19 +5,13 @@ namespace ki
{ {
namespace dml namespace dml
{ {
FieldBase::FieldBase(std::string name, const Record& record) FieldBase::FieldBase(std::string name)
: m_record(record)
{ {
m_name = name; m_name = name;
m_transferable = true; m_transferable = true;
m_type_hash = 0; m_type_hash = 0;
} }
const Record &FieldBase::get_record() const
{
return m_record;
}
std::string FieldBase::get_name() const std::string FieldBase::get_name() const
{ {
return m_name; return m_name;
@ -28,7 +22,7 @@ namespace dml
return m_transferable; 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"); auto *type_attr = node->first_attribute("TYPE");
if (!type_attr) if (!type_attr)
@ -41,27 +35,27 @@ namespace dml
FieldBase *field; FieldBase *field;
if (type == "BYT") if (type == "BYT")
field = new BytField("", record); field = new BytField("");
else if (type == "UBYT") else if (type == "UBYT")
field = new UBytField("", record); field = new UBytField("");
else if (type == "SHRT") else if (type == "SHRT")
field = new ShrtField("", record); field = new ShrtField("");
else if (type == "USHRT") else if (type == "USHRT")
field = new UShrtField("", record); field = new UShrtField("");
else if (type == "INT") else if (type == "INT")
field = new IntField("", record); field = new IntField("");
else if (type == "UINT") else if (type == "UINT")
field = new UIntField("", record); field = new UIntField("");
else if (type == "STR") else if (type == "STR")
field = new StrField("", record); field = new StrField("");
else if (type == "WSTR") else if (type == "WSTR")
field = new WStrField("", record); field = new WStrField("");
else if (type == "FLT") else if (type == "FLT")
field = new FltField("", record); field = new FltField("");
else if (type == "DBL") else if (type == "DBL")
field = new DblField("", record); field = new DblField("");
else if (type == "GID") else if (type == "GID")
field = new GidField("", record); field = new GidField("");
else else
{ {
std::ostringstream oss; std::ostringstream oss;

View File

@ -22,7 +22,7 @@ namespace dml
Record::Record(const Record& record) Record::Record(const Record& record)
{ {
for (auto it = record.fields_begin(); it != record.fields_end(); ++it) 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 bool Record::has_field(std::string name) const
@ -103,7 +103,7 @@ namespace dml
for (auto *field_node = node->first_node(); for (auto *field_node = node->first_node();
field_node; field_node = field_node->next_sibling()) 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())) if (has_field(field->get_name()))
{ {
// Is the old field the same type as the one created from // Is the old field the same type as the one created from