1 // Filename: Struct.h 2 #pragma once 3 #include <string> // std::string 4 #include <vector> // std::vector 5 #include <unordered_map> // std::unordered_map 6 7 #include "DistributedType.h" 8 namespace dclass // open namespace 9 { 10 11 12 // Foward declarations 13 class HashGenerator; 14 class File; 15 class Class; 16 class Field; 17 18 // A Struct provides type composition by combining multiple Fields each with their own type. 19 // Structs may have both anonymous and named Fields. 20 class Struct : public DistributedType 21 { 22 public: 23 Struct(File* file, const std::string &name); 24 virtual ~Struct(); 25 26 // as_struct returns this as a Struct if it is a Struct, or nullptr otherwise. 27 virtual Struct* as_struct(); 28 virtual const Struct* as_struct() const; 29 30 // as_class returns this Struct as a Class if it is a Class, or nullptr otherwise. 31 virtual Class* as_class(); 32 virtual const Class* as_class() const; 33 34 // get_id returns a unique index number associated with this struct. 35 inline unsigned int get_id() const; 36 // get_name returns the name of this struct. 37 inline const std::string& get_name() const; 38 // get_file returns the File object that contains the struct. 39 inline File* get_file(); 40 inline const File* get_file() const; 41 42 // get_num_fields returns the number of fields in the struct. 43 inline size_t get_num_fields() const; 44 // get_field returns the <n>th field of the struct. 45 inline Field* get_field(unsigned int n); 46 inline const Field* get_field(unsigned int n) const; 47 48 // get_field_by_id returns the field with the index <id>, or nullptr if no such field exists. 49 inline Field* get_field_by_id(unsigned int id); 50 inline const Field* get_field_by_id(unsigned int id) const; 51 // get_field_by_name returns the field with <name>, or nullptr if no such field exists. 52 inline Field* get_field_by_name(const std::string& name); 53 inline const Field* get_field_by_name(const std::string& name) const; 54 55 // add_field adds a new Field to the struct. 56 // Returns false if the field could not be added to the struct. 57 virtual bool add_field(Field* field); 58 59 // has_range in this case returns true if any of the fields within the struct have a constraint. 60 virtual bool has_range() const; 61 62 // generate_hash accumulates the properties of this type into the hash. 63 virtual void generate_hash(HashGenerator &hashgen) const; 64 65 protected: 66 Struct(File* file); 67 68 // set_id sets the index number associated with this struct. 69 void set_id(unsigned int id); 70 friend class File; 71 72 File *m_file; 73 unsigned int m_id; 74 std::string m_name; 75 76 std::vector<Field*> m_fields; 77 std::unordered_map<std::string, Field*> m_fields_by_name; 78 std::unordered_map<unsigned int, Field*> m_fields_by_id; 79 80 bool m_has_constraint; 81 }; 82 83 84 } // close namespace dclass 85 #include "Struct.ipp" 86