]> git.localhorst.tv Git - l2e.git/blob - src/loader/ParsedSource.h
added forwarding headers
[l2e.git] / src / loader / ParsedSource.h
1 /*
2  * ParsedSource.h
3  *
4  *  Created on: Aug 26, 2012
5  *      Author: holy
6  */
7
8 #ifndef LOADER_PARSEDSOURCE_H_
9 #define LOADER_PARSEDSOURCE_H_
10
11 #include "fwd.h"
12
13 #include <iosfwd>
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <vector>
18
19 namespace loader {
20
21 class Literal {
22
23 public:
24         enum Type {
25                 ARRAY_VALUES,
26                 ARRAY_PROPS,
27                 BOOLEAN,
28                 COLOR,
29                 NUMBER,
30                 PATH,
31                 STRING,
32                 VECTOR,
33                 OBJECT
34         };
35
36 public:
37         explicit Literal(const std::vector<Value *> &);
38         Literal(const std::string &, const std::vector<PropertyList *> &);
39         explicit Literal(bool);
40         Literal(int r, int g, int b, int a = 255);
41         explicit Literal(int number);
42         Literal(const std::string &dir, const std::string &path);
43         Literal(const std::string &);
44         Literal(int x, int y);
45         Literal(const std::string &typeName, PropertyList *properties);
46         ~Literal();
47 private:
48         Literal(const Literal &);
49         Literal &operator =(const Literal &);
50
51 public:
52         Type GetType() const { return type; }
53         bool IsArray() const { return GetType() == ARRAY_VALUES || GetType() == ARRAY_PROPS; }
54         bool IsObject() const { return GetType() == OBJECT; }
55         int ArraySize() const { return GetType() == ARRAY_VALUES ? GetValues().size() : GetPropertyLists().size(); }
56
57         const std::vector<Value *> &GetValues() const;
58         const std::vector<PropertyList *> &GetPropertyLists() const;
59         bool GetBoolean() const;
60         int GetRed() const;
61         int GetGreen() const;
62         int GetBlue() const;
63         int GetAlpha() const;
64         int GetNumber() const;
65         const std::string &GetString() const;
66         int GetX() const;
67         int GetY() const;
68         const std::string &GetTypeName() const;
69         const PropertyList *GetProperties() const;
70
71 private:
72         PropertyList *props;
73         std::string typeName, str;
74         std::vector<Value *> values;
75         std::vector<PropertyList *> propertyLists;
76         int i1, i2, i3, i4;
77         bool b;
78         Type type;
79
80 };
81
82
83 class Value {
84
85 public:
86         explicit Value(const std::string &identifier)
87         : literal(0), identifier(identifier), isLiteral(false) { }
88         explicit Value(Literal *literal)
89         : literal(literal), isLiteral(true) { }
90         ~Value();
91 private:
92         Value(const Value &);
93         Value &operator =(const Value &);
94
95 public:
96         bool IsLiteral() const { return isLiteral; }
97         const Literal &GetLiteral() const;
98         const std::string &GetIdentifier() const;
99
100 private:
101         Literal *literal;
102         std::string identifier;
103         bool isLiteral;
104
105 };
106
107
108 class PropertyList {
109
110 public:
111         PropertyList() { }
112         ~PropertyList();
113 private:
114         PropertyList(const PropertyList &);
115         PropertyList &operator =(const PropertyList &);
116
117 public:
118         void SetProperty(const std::string &name, Value *value) {
119                 props[name] = value;
120         }
121
122         typedef std::map<std::string, Value *>::iterator Iterator;
123         typedef std::map<std::string, Value *>::const_iterator ConstIterator;
124         Iterator Begin() { return props.begin(); }
125         ConstIterator Begin() const { return props.begin(); }
126         Iterator End() { return props.end(); }
127         ConstIterator End() const { return props.end(); }
128
129 private:
130         std::map<std::string, Value *> props;
131
132 };
133
134
135 class Declaration {
136
137 public:
138         Declaration(const std::string &typeName, const std::string &identifier)
139         : typeName(typeName), identifier(identifier) { }
140         virtual ~Declaration() { }
141 private:
142         Declaration(const Declaration &);
143         Declaration &operator =(const Declaration &);
144
145 public:
146         const std::string &TypeName() const { return typeName; }
147         const std::string &Identifier() const { return identifier; }
148
149 private:
150         std::string typeName;
151         std::string identifier;
152
153 };
154
155
156 class Definition
157 : public Declaration {
158
159 public:
160         Definition(const std::string &typeName, const std::string &identifier)
161         : Declaration(typeName, identifier), value(0), isLiteral(false) { }
162         virtual ~Definition();
163 private:
164         Definition(const Definition &);
165         Definition &operator =(const Definition &);
166
167 public:
168         void SetValue(Literal *);
169         void SetValue(PropertyList *);
170
171         bool HasLiteralValue() const { return isLiteral && value; }
172         bool HasProperties() const { return !isLiteral && value; }
173         Literal *GetLiteral();
174         const Literal *GetLiteral() const;
175         PropertyList *GetProperties();
176         const PropertyList *GetProperties() const;
177
178 private:
179         void *value;
180         bool isLiteral;
181
182 };
183
184
185 class ParsedSource {
186
187 public:
188         ParsedSource() { }
189         ~ParsedSource();
190 private:
191         ParsedSource(const ParsedSource &);
192         ParsedSource &operator =(const ParsedSource &);
193
194 public:
195         void AddDeclaration(Declaration *);
196         void AddDefinition(Definition *);
197         void ExportDeclaration(Declaration *);
198         void ExportIdentifier(const std::string &);
199
200         bool IsDeclared(const std::string &) const;
201         Declaration &GetDeclaration(const std::string &);
202         const Declaration &GetDeclaration(const std::string &) const;
203         bool IsDefined(const std::string &) const;
204         Definition &GetDefinition(const std::string &);
205         const Definition &GetDefinition(const std::string &) const;
206
207         const std::map<std::string, Declaration *> &Declarations() const { return declarations; }
208         const std::map<std::string, Definition *> &Definitions() const { return definitions; }
209         const std::set<std::string> &Exports() const { return exports; }
210
211 public:
212         void WriteHeader(std::ostream &) const;
213
214 private:
215         std::map<std::string, Declaration *> declarations;
216         std::map<std::string, Definition *> definitions;
217         std::set<std::string> exports;
218
219 };
220
221 }
222
223
224 namespace std {
225
226 ostream &operator <<(ostream &, const loader::ParsedSource &);
227 ostream &operator <<(ostream &, const loader::Literal &);
228
229 }
230
231 #endif /* LOADER_PARSEDSOURCE_H_ */