1 // Filename: Field.h
2 #pragma once
3 #include "KeywordList.h"
4 namespace dclass   // open namespace
5 {
6 
7 // Foward declarations
8 class DistributedType;
9 class File;
10 class Class;
11 class Struct;
12 class MolecularField;
13 class HashGenerator;
14 
15 // A Field is a member of a class or struct.
16 class Field : public KeywordList
17 {
18   public:
19     Field(DistributedType* type, const std::string &name = "");
20     virtual ~Field();
21 
22     // as_molecular returns this as a MolecularField if it is molecular, or nullptr otherwise.
23     virtual MolecularField* as_molecular();
24     virtual const MolecularField* as_molecular() const;
25 
26     // get_id returns a unique index number associated with this field.
27     inline unsigned int get_id() const;
28     // get_name returns the field's name.  An unnamed field returns the empty string.
29     inline const std::string& get_name() const;
30     // get_type returns the DistributedType of the field.
31     inline DistributedType* get_type();
32     inline const DistributedType* get_type() const;
33     // get_struct returns the Struct that contains this field.
34     inline Struct* get_struct();
35     inline const Struct* get_struct() const;
36 
37     // has_default_value returns true if a default value was defined for this field.
38     inline bool has_default_value() const;
39     // get_default_value returns the default value for this field.
40     //     If a default value hasn't been set, returns an implicit default.
41     inline const std::string& get_default_value() const;
42 
43     // set_name sets the name of this field.  Returns false if a field with
44     //     the same name already exists in the containing struct.
45     bool set_name(const std::string& name);
46 
47     // set_type sets the distributed type of the field and clear's the default value.
48     void set_type(DistributedType* type);
49 
50     // set_default_value defines a default value for this field.
51     //     Returns false if the value is invalid for the field's type.
52     virtual bool set_default_value(const std::string& default_value);
53 
54     // generate_hash accumulates the properties of this field into the hash.
55     virtual void generate_hash(HashGenerator& hashgen) const;
56 
57   protected:
58     // set_id sets the unique index number associated with the field.
59     void set_id(unsigned int id);
60     friend class File;
61 
62     // set_struct sets a pointer to the struct containing the field.
63     void set_struct(Struct* strct);
64     friend class Struct;
65     friend class Class;
66 
67     Struct* m_struct;
68     unsigned int m_id;
69     std::string m_name;
70     DistributedType* m_type;
71 
72     bool m_has_default_value; // is true if an explicity default has been set
73     std::string m_default_value; // the binary data of the default value encoded in a string
74 };
75 
76 // Field comparison operators for sorting
77 inline bool operator==(const Field& lhs, const Field& rhs)
78 {
79     return lhs.get_id() == rhs.get_id();
80 }
81 inline bool operator!=(const Field& lhs, const Field& rhs)
82 {
83     return !operator==(lhs, rhs);
84 }
85 inline bool operator< (const Field& lhs, const Field& rhs)
86 {
87     return lhs.get_id() < rhs.get_id();
88 }
89 inline bool operator> (const Field& lhs, const Field& rhs)
90 {
91     return  operator< (rhs, lhs);
92 }
93 inline bool operator<=(const Field& lhs, const Field& rhs)
94 {
95     return !operator> (lhs, rhs);
96 }
97 inline bool operator>=(const Field& lhs, const Field& rhs)
98 {
99     return !operator< (lhs, rhs);
100 }
101 struct FieldPtrComp {
102     inline bool operator()(const Field* lhs, const Field* rhs) const;
103 };
104 
105 } // close namespace dclass
106 #include "Field.ipp"
107