-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonDbValues.h
executable file
·397 lines (312 loc) · 9.31 KB
/
JsonDbValues.h
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
386
387
388
389
390
391
392
393
394
395
396
397
#ifndef __json_db_values_h__
#define __json_db_values_h__
/*
Copyright (C) 2010 Wouter van Kleunen
This file is part of JsonDb.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JsonDb. If not, see <http://www.gnu.org/licenses/>.
*/
#include <boost/enable_shared_from_this.hpp>
#include <boost/format.hpp>
#include <deque>
#include <iostream>
class Value
: public boost::enable_shared_from_this<Value>
{
protected:
ValueKey key;
static std::string Indent(unsigned int level)
{
return std::string(level * 2, ' ');
}
public:
enum ValueTypeId
{
VALUE_NUMBER_INTEGER = 0x10,
VALUE_NUMBER_REAL = 0x20,
VALUE_NUMBER_BOOL = 0x30,
VALUE_STRING = 0x40,
VALUE_ARRAY = 0x50,
VALUE_OBJECT = 0x60,
VALUE_NULL = 0x70
};
Value(ValueKey _key)
: key(_key)
{ }
virtual ~Value()
{ }
// Get the key value
ValueKey GetKey() const { return key; }
// Set the key value
void SetKey(ValueKey key) { this->key = key; }
// Return the type identifier as a string
virtual char const *GetTypeString() const = 0;
// Return the type identifier as a string
virtual ValueTypeId GetType() const = 0;
// Return as integer
virtual int GetValueInt() const
{
throw std::runtime_error((boost::format("Failed to convert object to integer, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Return as real
virtual double GetValueReal() const
{
throw std::runtime_error((boost::format("Failed to convert object to real, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Return as boolean
virtual bool GetValueBoolean() const
{
throw std::runtime_error((boost::format("Failed to convert object to boolean, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Return as string
virtual std::string GetValueString() const
{
throw std::runtime_error((boost::format("Failed to convert object to string, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Get entry with the specified path
virtual ValuePointer Get(JsonDb::TransactionHandle &transaction, std::string const &path, NotExistsResolution not_exist_resolution)
{
throw std::runtime_error((boost::format("Failed to get subelement with name '%s', item is of type '%s'") % path % GetTypeString()).str().c_str());
}
// Get entry with the specified path from element at the specified index
virtual ValuePointer Get(JsonDb::TransactionHandle &transaction, size_t index)
{
throw std::runtime_error((boost::format("Failed to get element by index, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Append an item to a list
virtual void Append(JsonDb::TransactionHandle &transaction, ValueKey key)
{
throw std::runtime_error((boost::format("Failed to append element, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Print to a stream
virtual void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level = 0) const = 0;
// Serialize to a stream
virtual void Serialize(std::ostream &output) const
{
throw std::runtime_error((boost::format("Failed to serialize object of this type: '%s'") % GetTypeString()).str().c_str());
}
// Delete this element
virtual void Delete(JsonDb::TransactionHandle &transaction)
{
transaction->Delete(GetKey());
}
// Delete subelement element
virtual void Delete(JsonDb::TransactionHandle &transaction, ValuePointer const &element)
{
throw std::runtime_error((boost::format("Failed to delete subelement from object, item is of type '%s'") % GetTypeString()).str().c_str());
}
// Unserialize from a stream
static ValuePointer Unserialize(ValueKey key, std::istream &input);
// Walk through the database and retrieve all keys
virtual void Walk(JsonDb::TransactionHandle &transaction, std::set<ValueKey> &keys)
{
keys.insert(GetKey());
}
};
// The null element
class ValueNull
: public Value
{
public:
typedef int Type;
ValueNull(ValueKey key)
: Value(key)
{ }
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
char const *GetTypeString() const
{
return "Null";
}
ValueTypeId GetType() const
{
return VALUE_NULL;
}
};
// An integer
class ValueNumberInteger
: public Value
{
public:
typedef int Type;
ValueNumberInteger(ValueKey key, Type _value = 0)
: Value(key)
, value(_value)
{ }
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow reading as integer
int GetValueInt() const
{
return value;
}
char const *GetTypeString() const
{
return "Integer";
}
ValueTypeId GetType() const
{
return VALUE_NUMBER_INTEGER;
}
private:
Type value;
};
// Real pointer number
class ValueNumberReal
: public Value
{
public:
typedef double Type;
ValueNumberReal(ValueKey key, Type _value = 0.0f)
: Value(key)
, value(_value)
{ }
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow reading as real
double GetValueReal() const
{
return value;
}
char const *GetTypeString() const
{
return "Real";
}
ValueTypeId GetType() const
{
return VALUE_NUMBER_REAL;
}
private:
Type value;
};
// A boolean
class ValueNumberBoolean
: public Value
{
public:
typedef bool Type;
ValueNumberBoolean(ValueKey key, Type _value = false)
: Value(key)
, value(_value)
{ }
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow reading as boolean
bool GetValueBoolean() const
{
return value;
}
char const *GetTypeString() const
{
return "Boolean";
}
ValueTypeId GetType() const
{
return VALUE_NUMBER_BOOL;
}
private:
Type value;
};
// A string
class ValueString
: public Value
{
public:
typedef std::string Type;
ValueString(ValueKey key, Type _value = Type())
: Value(key)
, value(_value)
{ }
// Allow serialize and print
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow reading as string
std::string GetValueString() const
{
return value;
}
char const *GetTypeString() const
{
return "String";
}
ValueTypeId GetType() const
{
return VALUE_STRING;
}
private:
Type value;
};
// An array
class ValueArray
: public Value
{
public:
typedef std::vector<ValueKey> Type;
ValueArray(ValueKey key, Type _values = Type())
: Value(key)
, values(_values)
{ }
// Allow serialize and printing
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow path functions
ValuePointer Get(JsonDb::TransactionHandle &transaction, size_t index);
// Append an item to a list
void Append(JsonDb::TransactionHandle &transaction, ValueKey key);
// Delete this element and all subelements
void Delete(JsonDb::TransactionHandle &transaction);
// Delete subelement
void Delete(JsonDb::TransactionHandle &transaction, ValuePointer const &element);
char const *GetTypeString() const
{
return "Array";
}
ValueTypeId GetType() const
{
return VALUE_ARRAY;
}
// Walk through the database and retrieve all keys
void Walk(JsonDb::TransactionHandle &transaction, std::set<ValueKey> &keys);
private:
Type values;
};
// An object
class ValueObject
: public Value
{
public:
typedef std::map<std::string, ValueKey> Type;
ValueObject(ValueKey key, Type _values = Type())
: Value(key)
, values(_values)
{ }
// Allow serialize and printing
void Serialize(std::ostream &output) const;
void Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const;
// Allow path functions
ValuePointer Get(JsonDb::TransactionHandle &transaction, std::string const &path, NotExistsResolution not_exists_resolution);
// Delete this element and all subelements
void Delete(JsonDb::TransactionHandle &transaction);
// Delete a element from this object
void Delete(JsonDb::TransactionHandle &transaction, ValuePointer const &element);
void Walk(JsonDb::TransactionHandle &transaction, std::set<ValueKey> &keys);
ValueTypeId GetType() const
{
return VALUE_OBJECT;
}
char const *GetTypeString() const
{
return "Object";
}
private:
Type values;
};
#endif