nomlib
Loading...
Searching...
No Matches
Value.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_PTREE_VALUE_HPP
30#define NOMLIB_SYSTEM_PTREE_VALUE_HPP
31
32#include <string>
33#include <vector>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/ptree/ptree_config.hpp"
38#include "nomlib/ptree/ptree_types.hpp"
39
40namespace nom {
41
43class NOM_EXPORT Value
44{
45 public:
46 typedef Value SelfType;
47 typedef Value* RawPtr;
48 typedef Value& Reference;
49
50 // TODO: Rename to const_iterator?
51 typedef ValueConstIterator ConstIterator;
52
53 // TODO: Rename to iterator?
54 typedef ValueIterator Iterator;
55
56 typedef std::vector<std::string> Members;
57
68 {
69 Null = 0, // Type 0 (default)
70 SignedInteger, // Type 1
71 UnsignedInteger, // Type 2
72 RealNumber, // Type 3
73 String, // Type 4
74 Boolean, // Type 5
75 ArrayValues, // Type 6
76 ObjectValues // Type 7
77 };
78
82 NOM_EXPORT static const Value& null;
83
86 Value(); // type 0
87
88 ~Value();
89
93 Value(int val);
94
103 Value(uint val);
104
109 Value(real64 val);
110
129 Value(const char* str);
130
134 Value(const std::string& str);
135
139 Value(bool val);
140
144 Value(const Object& obj);
145
148
150 Value(const Value& rhs);
151
153 Value::SelfType& operator =(const SelfType& rhs);
154
162 void swap(Value& rhs);
163
170 bool operator <(const Value& rhs) const;
171
175 bool operator <=(const Value& rhs) const;
176
180 bool operator >=(const Value& rhs) const;
181
185 bool operator >(const Value& rhs) const;
186
192 bool operator ==(const Value& rhs) const;
193
197 bool operator !=(const Value& rhs) const;
198
202 bool operator!() const;
203
208 int compare(const Value& rhs) const;
209
213 Value::RawPtr get();
214
218 Value::Reference ref();
219
224 enum Value::ValueType type() const;
225
232 const std::string type_name() const;
233
235 bool null_type() const;
236
239 bool int_type() const;
240
243 bool uint_type() const;
244
247 bool double_type() const;
248
251 bool float_type() const;
252
255 bool string_type() const;
256
259 bool bool_type() const;
260
266 bool array_type() const;
267
273 bool object_type() const;
274
275 const std::string stringify() const;
276
280 int get_int() const;
281
286 uint get_uint() const;
287
292 real64 get_double() const;
293
301 real32 get_float() const;
302
309 const char* get_cstring() const;
310
317 std::string get_string() const;
318
322 bool get_bool() const;
323
324 bool array_valid() const;
325 bool object_valid() const;
326
331 const Object array() const;
332
337 const Object object() const;
338
343 nom::size_type size() const;
344
351 bool empty() const;
352
359 void clear();
360
371 Value& operator[](ArrayIndex index);
372
375 Value& operator[](int index);
376
392 const Value& operator[](ArrayIndex index) const;
393
396 const Value& operator[](int index) const;
397
406 Value& operator[](const char* key);
407
408 Value& operator[](const std::string& key);
409
410 const Value& operator[](const char* key) const;
411 const Value& operator[](const std::string& key) const;
412
414 Value& push_back(const Value& val);
415
424 const Value& find(const std::string& key) const;
425
433 Value erase(const std::string& key);
434
448 // Value erase( int index );
449
457 Members member_names() const;
458
459 Value::ConstIterator begin() const;
460
461 Value::ConstIterator end() const;
462
471 Value::Iterator begin();
472
481 Value::Iterator end();
482
489 const std::string dump(const Value& object, int depth = 0) const;
490
491 private:
493 const std::string dump_key(const Value& key) const;
494
496 const std::string dump_value(const Value& val) const;
497
499 const std::string print_key(const std::string& type, uint size) const;
500
502 const std::string print_value(const std::string& val) const;
503
514 {
515 int int_; // Type 1
516 uint uint_; // Type 2
517 real64 real_; // Type 3
518 bool bool_; // Type 4
519 // NOTE: Instance-owned pointer.
520 const char* string_; // Type 5
521 // NOTE: Instance-owned pointer.
522 Object* object_; // Type 6 and 7
523 };
524
527
530};
531
536NOM_EXPORT std::ostream& operator <<(std::ostream& os, const Value& val);
537
538} // namespace nom
539
540#endif // include guard defined
541
Read-only iteration for nom::Value.
Generic interface for opaque data containers.
Definition Value.hpp:44
void clear()
Remove all array and object members.
Value::Iterator begin()
Iterator access to the beginning of the object's tree.
Value()
Default constructor; constructs an object with NullValue data type.
const Value & operator[](ArrayIndex index) const
Obtain a stored element by index number.
ValueType
Supported data types able to be held inside the object.
Definition Value.hpp:68
ValueHolder value_
The stored value in this instance.
Definition Value.hpp:529
Value(const std::string &str)
Construct an object from a C++ string value (std::string).
Value(const Object &obj)
Construct an object with either array or object node values.
const Value & operator[](int index) const
Value(ValueType type)
Construct a Value container node of a specified type.
Value & operator[](int index)
const Value & find(const std::string &key) const
Search the object for an existing member.
enum Value::ValueType type() const
Obtain the enumeration type of the object.
void swap(Value &rhs)
Exchange the contents of the container; copy & swap idiom.
const std::string type_name() const
Obtain the enumeration type of the object as a std::string.
const Object array() const
Obtain the array values of the object.
nom::size_type size() const
Obtain the size of the object's contained values.
bool float_type() const
Query if the value type type stored in the object is a double- precision floating point real number.
Value(bool val)
Construct an object from a boolean value.
Value & push_back(const Value &val)
Insert array elements.
bool array_type() const
Query if the value type type stored in the object are array values.
const char * get_cstring() const
Obtain the C style string value stored within the container.
Value(const char *str)
Construct an object from a C-style string value.
Value::Iterator end()
Iterator access to the end of the object's tree.
Value::Reference ref()
Obtain a reference to the object.
bool uint_type() const
Query if the value type type stored in the object is an unsigned integer (non-negative.
Members member_names() const
Remove an array object.
const Object object() const
Obtain the object tree of the object.
Value::RawPtr get()
Obtain a pointer to the object.
Value(const Value &rhs)
Copy constructor.
bool string_type() const
Query if the value type type stored in the object is a string value.
const std::string dump_value(const Value &val) const
Internal helper method for nom::Value::dump.
int get_int() const
Obtain the signed integer value stored within the container.
const std::string print_value(const std::string &val) const
Internal helper method for nom::Value::dump_value.
real64 get_double() const
Obtain the double-precision floating point "real" number value stored within the container.
Value(int val)
Construct an object using a signed integer value.
const std::string print_key(const std::string &type, uint size) const
Internal helper method for nom::Value::dump_key.
Value(real64 val)
Construct an object using a double-precision floating point "real" number value.
std::string get_string() const
Obtain the string value stored within the container.
bool operator!() const
Returns Value::null.
enum ValueType type_
The type of stored value in this instance.
Definition Value.hpp:526
static NOM_EXPORT const Value & null
Declared value of Null for this object.
Definition Value.hpp:82
const std::string dump_key(const Value &key) const
Internal helper method for nom::Value::dump.
bool double_type() const
Query if the value type type stored in the object is a double- precision floating point real number.
bool empty() const
Obtain boolean response in regards to container's empty status.
Value & operator[](ArrayIndex index)
Obtain a stored element by index number.
bool bool_type() const
Query if the value type type stored in the object is a boolean value.
Value erase(const std::string &key)
Remove the named member.
bool object_type() const
Query if the value type type stored in the object are object values.
bool get_bool() const
Obtain the boolean value stored within the container.
const std::string dump(const Value &object, int depth=0) const
Dump the object's complete value tree.
uint get_uint() const
Obtain the unsigned (non-negative) integer value stored within the container.
Value(uint val)
Construct an object using an unsigned (non-negative) integer value.
real32 get_float() const
Obtain the double-precision floating point "real" number value stored within the container.
int compare(const Value &rhs) const
Internal helper method for comparing array & object node containers.
bool int_type() const
Query if the value type type stored in the object is a signed integer.
bool null_type() const
Query if the value type type stored in the object is NULL.
Value & operator[](const char *key)
Access an object node's container.
Container for the data types able to be held.
Definition Value.hpp:514