1 // Filename: MolecularField.cpp 2 #include "util/HashGenerator.h" 3 #include "dc/Class.h" 4 5 #include "MolecularField.h" 6 namespace dclass // open namespace dclass 7 { 8 9 10 // constructor MolecularField(Class * cls,const std::string & name)11MolecularField::MolecularField(Class* cls, const std::string &name) : 12 Field(nullptr, name), Struct(cls->get_file()) 13 { 14 Field::m_type = this; 15 } 16 17 // destructor ~MolecularField()18MolecularField::~MolecularField() 19 { 20 } 21 22 // as_molecular returns this as a MolecularField if it is molecular, or nullptr otherwise. as_molecular()23MolecularField* MolecularField::as_molecular() 24 { 25 return this; 26 } as_molecular() const27const MolecularField* MolecularField::as_molecular() const 28 { 29 return this; 30 } 31 32 // add_field adds a new Field as part of the Molecular. 33 // Returns false if the field could not be added. add_field(Field * field)34bool MolecularField::add_field(Field* field) 35 { 36 // Moleculars cannot be nested 37 if(field->as_molecular()) { 38 return false; 39 } 40 41 // Copy our keywords from the first add field 42 if(m_fields.size() == 0) { 43 copy_keywords(*field); 44 } 45 46 // Each field has to have the same set of keywords 47 else if(!has_matching_keywords(*field)) { 48 return false; 49 } 50 51 // Add the field 52 m_fields.push_back(field); 53 54 // See if we still have a fixed byte size. 55 if(has_fixed_size() || m_fields.size() == 1) { 56 if(field->get_type()->has_fixed_size()) { 57 m_size += field->get_type()->get_size(); 58 } else { 59 m_size = 0; 60 } 61 } 62 63 if(has_default_value() || m_fields.size() == 1) { 64 // If any atomic field of the molecular has a default value, 65 // the molecular is also considerd to have a default value. 66 if(field->has_default_value()) { 67 m_default_value = true; 68 } 69 70 m_default_value += field->get_default_value(); 71 } 72 73 return true; 74 } 75 set_default_value(const std::string &)76bool MolecularField::set_default_value(const std::string&) 77 { 78 // MolecularField default values are implict from their 79 // atomic components and cannot be defined manually. 80 return false; 81 } 82 83 // generate_hash accumulates the properties of this field into the hash. generate_hash(HashGenerator & hashgen) const84void MolecularField::generate_hash(HashGenerator& hashgen) const 85 { 86 hashgen.add_int(Field::m_id); 87 hashgen.add_string(Field::m_name); 88 // We aren't the owner of the fields so we only use their id in the hash 89 hashgen.add_int(m_fields.size()); 90 for(const auto& it : m_fields) { 91 hashgen.add_int(it->get_id()); 92 } 93 } 94 95 96 } // close namespace dclass 97