34 typedef long int TInteger;
35 typedef unsigned long int TUInteger;
38 typedef std::string TString;
39 typedef std::map<TString, Value> TObject;
40 typedef std::vector<Value> TArray;
55 enum Type { TINTEGER, TUINTEGER, TREAL, TBOOL, TSTRING, TOBJECT, TARRAY, TNULL };
65 inline Value() : refcount(NULL), type(TNULL) {}
68 inline Value(Type type_) : refcount(NULL), type(TNULL) { reset(type_); }
72 explicit inline Value(TInteger integer) : refcount(NULL), type(TINTEGER) { data.integer = integer; }
73 explicit inline Value(TUInteger uinteger) : refcount(NULL), type(TUINTEGER) { data.uinteger = uinteger; }
74 explicit inline Value(TReal real) : refcount(NULL), type(TREAL) { data.real = real; }
75 explicit inline Value(TBool
boolean) : refcount(NULL), type(TBOOL) { data.boolean = boolean; }
78 explicit inline Value(
unsigned int uinteger) : refcount(NULL), type(TUINTEGER) {
79 data.uinteger = (TUInteger)uinteger;
81 explicit inline Value(
int integer) : refcount(NULL), type(TINTEGER) { data.integer = (TInteger)integer; }
85 explicit inline Value(TString
string) : refcount(
new TUInteger(0)), type(TSTRING) {
86 data.string =
new TString(
string);
88 explicit inline Value(TObject
object) : refcount(
new TUInteger(0)), type(TOBJECT) {
89 data.object =
new TObject(
object);
91 explicit inline Value(TArray array) : refcount(
new TUInteger(0)), type(TARRAY) { data.array =
new TArray(array); }
96 Value(
const std::vector<T> &ref) : refcount(
new TUInteger(0)), type(TARRAY) {
97 const size_t size = ref.size();
98 data.array =
new TArray(size);
99 for (
size_t i = 0; i < size; i++) {
100 (*data.array)[i] =
Value(ref[i]);
105 inline Value(
const Value &other) : refcount(other.refcount), type(other.type), data(other.data) { incref(); }
108 inline ~
Value() { decref(); }
116 refcount = other.refcount;
120 template <
typename T>
121 inline Value &operator=(
const T &val) {
122 return operator=(
Value(val));
125 inline Value &operator[](
const std::string &key)
const {
return getMember(key); }
126 inline Value &operator[](
const size_t index)
const {
return getIndex(index); }
130 void reset(Type type);
133 inline void reset() { reset(TNULL); }
136 inline Type getType()
const {
return type; }
140 inline TInteger getInteger()
const {
144 inline TUInteger getUInteger()
const {
145 checkType(TUINTEGER);
146 return data.uinteger;
148 inline TReal getReal()
const {
152 inline TBool getBool()
const {
156 inline TString getString()
const {
162 inline Value &getMember(TString key)
const {
164 return (*data.object)[key];
168 inline size_t getArraySize()
const {
170 return data.array->size();
174 inline Value &getIndex(
size_t index)
const {
176 return (*data.array)[index];
183 template <
typename T>
184 inline T cast()
const {
185 throw std::runtime_error(
"Cannot cast Value to desired type");
191 template <
typename T>
192 inline std::vector<T> toVector()
const {
193 const size_t size = getArraySize();
194 std::vector<T> result(size);
195 for (
size_t i = 0; i < size; i++) {
196 result[i] = (*data.array)[i].cast<T>();
204 std::vector<std::string> getMembers()
const;
207 bool isMember(std::string key)
const;
210 inline void setInteger(TInteger integer) {
211 checkTypeReset(TINTEGER);
212 data.integer = integer;
214 inline void setUINteger(TUInteger uinteger) {
215 checkTypeReset(TUINTEGER);
216 data.uinteger = uinteger;
218 inline void setReal(TReal real) {
219 checkTypeReset(TREAL);
222 inline void setReal(TBool
boolean) {
223 checkTypeReset(TBOOL);
224 data.boolean = boolean;
226 inline void setString(TString
string) {
227 checkTypeReset(TSTRING);
228 *data.string = string;
232 inline void setMember(TString key,
Value value) {
233 checkTypeReset(TOBJECT);
234 (*data.object)[key] = value;
238 inline void setArraySize(
size_t size) {
239 checkTypeReset(TARRAY);
240 data.array->resize(size);
244 inline void setIndex(
size_t index,
Value value) {
245 checkTypeReset(TARRAY);
246 (*data.array)[index] = value;
251 static std::string prettyType(Type type);
254 static void wrongType(Type actual, Type requested);
258 inline void checkType(Type type_)
const {
259 if (this->type != type_) {
260 wrongType(this->type, type_);
265 inline void checkTypeReset(Type type_) {
266 if (this->type != type_) reset(TOBJECT);
270 inline void decref() {
271 if (refcount && !((*refcount)--)) clean();
275 inline void incref() {
276 if (refcount) (*refcount)++;
296 inline std::string Value::cast<std::string>()
const {
299 std::stringstream out;
304 std::stringstream out;
305 out << data.uinteger;
309 std::stringstream out;
314 return data.boolean ?
"true" :
"false";
318 return *(data.string);
320 std::stringstream out;
321 out <<
"ARR{" << (
void *)data.array <<
'}';
325 std::stringstream out;
326 out <<
"ARR{" << (
void *)data.object <<
'}';
330 throw std::runtime_error(
"Value could not be cast to string (forgotten?)");
336 inline int Value::cast<int>()
const {
339 return data.uinteger;
344 throw std::runtime_error(
"Cannot cast " + prettyType(type) +
" to integer");
350 inline double Value::cast<double>()
const {
353 return data.uinteger;
359 throw std::runtime_error(
"Cannot cast " + prettyType(type) +
" to double");
365 inline bool Value::cast<bool>()
const {
368 return data.uinteger != 0;
370 return data.integer != 0;
372 return data.real != 0.0;
387 parser_error(
const int line_,
const int pos_, std::string desc);
389 virtual const char *what()
const throw();
393 std::string desc, pretty;
400 Reader(std::istream &stream);
403 Reader(
const std::string &str);
408 bool getValue(
Value &result);
412 char *data, *cur, *lastbr;
416 std::string unescapeString(std::string
string);
431 Writer(std::ostream &stream);
440 void putValue(
const Value &value,
const std::string &delim =
"\n");
447 std::string escapeString(std::string
string);
450 void writeValue(
const Value &value,
const std::string &depth =
"");