1 // Filename: File.h
2 #pragma once
3 #include <stdint.h>
4 #include <string>        // std::string
5 #include <vector>        // std::vector
6 #include <unordered_map> // std::unordered_map
7 namespace dclass   // open namespace
8 {
9 
10 
11 // Forward declarations
12 class DistributedType;
13 class Class;
14 class Struct;
15 class Field;
16 class HashGenerator;
17 
18 struct Import {
19     std::string module;
20     std::vector<std::string> symbols;
21 
22     inline Import(const std::string& module_name);
23 };
24 
25 // A File represents the complete list of Distributed Class descriptions as read from a .dc file.
26 class File
27 {
28 
29   public:
30     File(); // constructor
31     ~File(); // destructor
32 
33     // get_num_classes returns the number of classes in the file
34     inline size_t get_num_classes() const;
35     // get_class returns the <n>th class read from the .dc file(s).
36     inline Class* get_class(unsigned int n);
37     inline const Class* get_class(unsigned int n) const;
38 
39     // get_num_structs returns the number of structs in the file.
40     inline size_t get_num_structs() const;
41     // get_struct returns the <n>th struct in the file.
42     inline Struct* get_struct(unsigned int n);
43     inline const Struct* get_struct(unsigned int n) const;
44 
45     // get_class_by_id returns the requested class or nullptr if there is no such class.
46     Class* get_class_by_id(unsigned int id);
47     const Class* get_class_by_id(unsigned int id) const;
48     // get_class_by_name returns the requested class or nullptr if there is no such class.
49     Class* get_class_by_name(const std::string &name);
50     const Class* get_class_by_name(const std::string &name) const;
51 
52     // get_num_types returns the number of types in the file.
53     //     All type ids will be within the range 0 <= id < get_num_types().
54     inline size_t get_num_types() const;
55     // get_type_by_id returns the requested type or nullptr if there is no such type.
56     inline DistributedType* get_type_by_id(unsigned int id);
57     inline const DistributedType* get_type_by_id(unsigned int id) const;
58     // get_type_by_name returns the requested type or nullptr if there is no such type.
59     inline DistributedType* get_type_by_name(const std::string &name);
60     inline const DistributedType* get_type_by_name(const std::string &name) const;
61 
62     // get_field_by_id returns the request field or nullptr if there is no such field.
63     inline Field* get_field_by_id(unsigned int id);
64     inline const Field* get_field_by_id(unsigned int id) const;
65 
66     // get_num_imports returns the number of imports in the file.
67     inline size_t get_num_imports() const;
68     // get_import retuns the <n>th import in the file.
69     inline Import* get_import(unsigned int n);
70     inline const Import* get_import(unsigned int n) const;
71 
72     // has_keyword returns true if a keyword with the name <keyword> is declared in the file.
73     inline bool has_keyword(const std::string& keyword) const;
74     // get_num_keywords returns the number of keywords declared in the file.
75     inline size_t get_num_keywords() const;
76     // get_keyword returns the <n>th keyword declared in the file.
77     inline const std::string& get_keyword(unsigned int n) const;
78 
79     // add_class adds the newly-allocated class to the file.
80     //     Returns false if there is a name conflict.
81     bool add_class(Class *dclass);
82 
83     // add_struct adds the newly-allocated struct definition to the file.
84     //     Returns false if there is a name conflict.
85     bool add_struct(Struct *dstruct);
86 
87     // add_import adds a newly-allocated import to the file.
88     //     Imports with duplicate modules are combined.
89     void add_import(Import* import);
90 
91     // add_typedef adds the alias <name> to the file for the type <type>.
92     //     Returns false if there is a name conflict.
93     bool add_typedef(const std::string& name, DistributedType* type);
94 
95     // add_keyword adds a keyword with the name <keyword> to the list of declared keywords.
96     void add_keyword(const std::string &keyword);
97 
98     // get_hash returns a 32-bit hash representing the file.
99     uint32_t get_hash() const;
100 
101     // generate_hash accumulates the properties of this file into the hash.
102     virtual void generate_hash(HashGenerator& hashgen) const;
103 
104   private:
105     // add_field gives the field a unique id within the file.
106     void add_field(Field *field);
107     friend class Class;
108     friend class Struct;
109 
110     std::vector<Struct*> m_structs;
111     std::vector<Class*> m_classes;
112     std::vector<Import*> m_imports; // list of python imports in the file
113     std::vector<std::string> m_keywords;
114 
115     std::vector<Field*> m_fields_by_id;
116     std::vector<DistributedType*> m_types_by_id;
117     std::unordered_map<std::string, DistributedType*> m_types_by_name;
118 };
119 
120 
121 } // close namespace dclass
122 #include "File.ipp"
123