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