Repository navigation
Expand file tree
/
Copy pathDynObject.h
More file actions
386 lines (269 loc) · 9.23 KB
/
Copy pathDynObject.h
File metadata and controls
386 lines (269 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* \file DynObject.h
* \author Jan Milik <milikjan@fit.cvut.cz>
* \date 2013-12-04
*
* \brief Header file for the json class.
*/
#ifndef Dyn_2HV25IX3
#define Dyn_2HV25IX3
#include <string>
#include <vector>
#include <map>
#include <istream>
#include <sstream>
#include "Object.h"
#include "TextLoc.h"
#include "Lexer.h"
#include "Logger.h"
#include "PrettyPrinter.h"
#include "utils.h"
#include "string_utils.h"
namespace cppapp {
/** \addtogroup dynamic
*
* \brief Dynamic object system.
*
* @{
*/
//// DynObject //////////////////////////////////////////////////////
class DynBoolean;
class DynNumber;
class DynString;
class DynObject : public Object {
private:
TextLoc location;
public:
DynObject() :
location("<unknown>")
{}
DynObject(TextLoc location) :
location(location)
{}
virtual ~DynObject() {}
TextLoc getLocation() const { return location; }
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool isDict() const { return false; }
virtual bool isList() const { return false; }
virtual bool isBool() const { return false; }
virtual bool isNum() const { return false; }
virtual bool isString() const { return false; }
virtual bool isNull() const { return false; }
virtual bool isError() const { return false; }
virtual int getSize() const { return 0; }
/**
* \name Subscription Protocol
*/
///@{
virtual bool hasStrItem(std::string key) { return false; }
virtual Ref<DynObject> getStrItem(std::string key, Ref<DynObject> deflt);
virtual Ref<DynObject> getStrItem(std::string key) { return getStrItem(key, NULL); }
virtual void setStrItem(std::string key, Ref<DynObject> value) {}
virtual bool hasIntItem(int index) { return false; }
virtual Ref<DynObject> getIntItem(int key);
virtual void setIntItem(int key, Ref<DynObject> value) {}
virtual bool hasItem(Ref<DynObject> key);
virtual Ref<DynObject> getItem(Ref<DynObject> key);
virtual void setItem(Ref<DynObject> key, Ref<DynObject> value);
virtual Ref<DynObject> getDottedItem(std::string key);
virtual void setDottedItem(std::string key, Ref<DynObject> value);
virtual bool getStrBool(std::string key, bool defaultValue);
virtual int getStrInt(std::string key, int defaultValue);
virtual double getStrDouble(std::string key, double defaultValue);
virtual std::string getStrString(std::string key, std::string defaultValue);
virtual Ref<DynObject> getKeys();
///@}
virtual bool equals(Ref<DynObject> other) const;
/**
* \name Iteration Protocol
*/
///@{
virtual Ref<DynObject> getIterator() { return this; }
virtual Ref<DynObject> getNext();
///@}
virtual Ref<DynBoolean> toBool();
virtual Ref<DynNumber> toNum();
virtual Ref<DynString> toString();
/**
* \name C Type Conversion
*/
///@{
virtual bool getBool() const { return true; }
virtual double getDouble() const { return 0; }
virtual int getInt() const { return (int)getDouble(); }
virtual std::string getString() const { return "DynObject"; }
operator bool() { return getBool(); }
operator double() { return getDouble(); }
operator int() { return getInt(); }
operator std::string() { return getString(); }
///@}
};
#define DYN_FOR_EACH(it_name__, collection__) \
for ( \
Ref<DynObject> iterator__ = collection__->getIterator(), \
it_name__ = iterator__->getNext(); \
!it_name__->isError(); \
it_name__ = iterator__->getNext() \
)
//// DynDict ////////////////////////////////////////////////////////
class DynString;
class DynDict : public DynObject {
public:
typedef std::map<std::string, Ref<DynObject> > Map;
private:
Map _values;
public:
DynDict(TextLoc loc) :
DynObject(loc)
{}
virtual ~DynDict() { _values.clear(); }
virtual bool isDict() const { return true; }
virtual int getSize() const { return _values.size(); }
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool hasStrItem(std::string key);
virtual Ref<DynObject> getStrItem(std::string key, Ref<DynObject> deflt);
virtual void setStrItem(std::string key, Ref<DynObject> value);
virtual Ref<DynObject> getKeys();
void update(Ref<DynDict> dict)
{
FOR_EACH(*dict, it) {
setStrItem(it->first, it->second);
}
}
Map::iterator begin() { return _values.begin(); }
Map::iterator end() { return _values.end(); }
Map::const_iterator begin() const { return _values.begin(); }
Map::const_iterator end() const { return _values.end(); }
};
//// DynList ////////////////////////////////////////////////////////
class DynList : public DynObject {
public:
typedef std::vector<Ref<DynObject> > Vector;
private:
std::vector<Ref<DynObject> > _values;
public:
DynList(TextLoc loc) :
DynObject(loc)
{}
virtual ~DynList() { _values.clear(); }
virtual bool isList() const { return true; }
virtual int getSize() const { return _values.size(); }
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool hasIntItem(int index);
virtual Ref<DynObject> getIntItem(int key);
virtual void setIntItem(int key, Ref<DynObject> value);
virtual Ref<DynObject> getIterator();
void append(Ref<DynObject> obj) { _values.push_back(obj); }
Vector::iterator begin() { return _values.begin(); }
Vector::iterator end() { return _values.end(); }
};
class DynListIter : public DynObject {
private:
bool started_;
DynList::Vector::iterator iterator_;
Ref<DynList> list_;
public:
DynListIter(Ref<DynList> list) :
started_(false),
list_(list)
{ }
virtual Ref<DynObject> getNext();
};
#define DYN_NEW_LIST (new cppapp::DynList(TextLoc(__FILE__, __LINE__)))
//// DynScalar //////////////////////////////////////////////////////
template<class T>
class DynScalar : public DynObject {
private:
T _value;
public:
DynScalar(TextLoc loc, T value) :
DynObject(loc), _value(value)
{}
T getValue() const { return _value; }
void setValue(T value) { _value = value; }
};
//// DynBoolean /////////////////////////////////////////////////////
class DynBoolean : public DynScalar<bool> {
public:
DynBoolean(TextLoc loc, bool value) :
DynScalar<bool>(loc, value)
{}
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool isBool() const { return true; }
virtual Ref<DynBoolean> toBool() { return this; }
virtual bool getBool() const { return getValue(); }
virtual double getDouble() const { return getValue() ? 1 : 0; }
virtual std::string getString() const { return getValue() ? "true" : "false"; }
static bool parse(Lexer *lexer, bool *result);
static bool parse(std::string str, bool *result);
};
//// DynNumber //////////////////////////////////////////////////////
class DynNumber : public DynScalar<double> {
public:
DynNumber(TextLoc loc, double value) :
DynScalar<double>(loc, value)
{}
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool isNum() const { return true; }
virtual Ref<DynNumber> toNum() { return this; }
virtual bool getBool() const { return getValue() != 0.0; }
virtual double getDouble() const { return getValue(); }
virtual std::string getString() const {
std::ostringstream out;
out << getValue();
return out.str();
}
static bool parse(Lexer *lexer, double *result);
static bool parse(std::string str, double *result);
};
//// DynString //////////////////////////////////////////////////////
class DynString : public DynScalar<std::string> {
public:
DynString(TextLoc loc, std::string value) :
DynScalar<std::string>(loc, value)
{}
virtual void print(Ref<PrettyPrinter> printer, int level = 0);
virtual bool isString() const { return true; }
virtual Ref<DynString> toString() { return this; }
virtual bool getBool() const;
virtual double getDouble() const;
virtual std::string getString() const { return getValue(); }
};
#define DYN_NEW_STRING(value) (new cppapp::DynString(TextLoc(__FILE__, __LINE__), (value)))
//// DynNull ////////////////////////////////////////////////////////
class DynNull : public DynObject {
public:
virtual bool isNull() const { return true; }
virtual bool getBool() const { return false; }
virtual double getDouble() const { return 0.0; }
virtual std::string getString() const { return "null"; }
static Ref<DynNull> getInstance();
};
//// DynError ///////////////////////////////////////////////////////
class DynError : public DynObject {
private:
std::string message_;
TextLoc errorLoc_;
public:
DynError(TextLoc loc, std::string message, TextLoc errorLoc) :
DynObject(loc), message_(message), errorLoc_(errorLoc)
{}
virtual bool isError() const { return true; }
virtual TextLoc getErrorLoc() const { return errorLoc_; }
virtual bool getBool() const { return false; }
virtual std::string getString() const
{
std::ostringstream out;
out << message_;
out << " (" << getLocation().fileName << ":" << getLocation().line << ")";
return out.str();
}
};
#define DYN_MAKE_ERROR(msg) new DynError( \
TextLoc(__FILE__, __LINE__), \
(msg), \
TextLoc(__FILE__, __LINE__) \
);
/** @} */
} // namespace cppapp
#endif /* end of include guard: Dyn_2HV25IX3 */