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 "nomlib/config.hpp"
36#include "nomlib/ptree/ptree_config.hpp"
37#include "nomlib/ptree/ptree_types.hpp"
38
39namespace nom {
40
42class Value
43{
44 public:
45 typedef Value SelfType;
46 typedef Value* RawPtr;
47 typedef Value& Reference;
48
49 // TODO: Rename to const_iterator?
50 typedef ValueConstIterator ConstIterator;
51
52 // TODO: Rename to iterator?
53 typedef ValueIterator Iterator;
54
55 typedef std::vector<std::string> Members;
56
67 {
68 Null = 0, // Type 0 (default)
69 SignedInteger, // Type 1
70 UnsignedInteger, // Type 2
71 RealNumber, // Type 3
72 String, // Type 4
73 Boolean, // Type 5
74 ArrayValues, // Type 6
75 ObjectValues // Type 7
76 };
77
81 static const Value& null;
82
85 Value(); // type 0
86
87 ~Value();
88
92 Value(int val);
93
102 Value(uint val);
103
108 Value(real64 val);
109
128 Value(const char* str);
129
133 Value(const std::string& str);
134
138 Value(bool val);
139
143 Value(const Object& obj);
144
147
149 Value(const Value& rhs);
150
152 Value::SelfType& operator =(const SelfType& rhs);
153
161 void swap(Value& rhs);
162
169 bool operator <(const Value& rhs) const;
170
174 bool operator <=(const Value& rhs) const;
175
179 bool operator >=(const Value& rhs) const;
180
184 bool operator >(const Value& rhs) const;
185
191 bool operator ==(const Value& rhs) const;
192
196 bool operator !=(const Value& rhs) const;
197
201 bool operator!() const;
202
207 int compare(const Value& rhs) const;
208
212 Value::RawPtr get();
213
217 Value::Reference ref();
218
223 enum Value::ValueType type() const;
224
231 const std::string type_name() const;
232
234 bool null_type() const;
235
238 bool int_type() const;
239
242 bool uint_type() const;
243
246 bool double_type() const;
247
250 bool float_type() const;
251
254 bool string_type() const;
255
258 bool bool_type() const;
259
265 bool array_type() const;
266
272 bool object_type() const;
273
274 const std::string stringify() const;
275
279 int get_int() const;
280
285 uint get_uint() const;
286
291 real64 get_double() const;
292
300 real32 get_float() const;
301
308 const char* get_cstring() const;
309
316 std::string get_string() const;
317
321 bool get_bool() const;
322
323 bool array_valid() const;
324 bool object_valid() const;
325
330 const Object array() const;
331
336 const Object object() const;
337
342 nom::size_type size() const;
343
350 bool empty() const;
351
358 void clear();
359
370 Value& operator[](ArrayIndex index);
371
374 Value& operator[](int index);
375
391 const Value& operator[](ArrayIndex index) const;
392
395 const Value& operator[](int index) const;
396
405 Value& operator[](const char* key);
406
407 Value& operator[](const std::string& key);
408
409 const Value& operator[](const char* key) const;
410 const Value& operator[](const std::string& key) const;
411
413 Value& push_back(const Value& val);
414
423 const Value& find(const std::string& key) const;
424
432 Value erase(const std::string& key);
433
447 // Value erase( int index );
448
456 Members member_names() const;
457
458 Value::ConstIterator begin() const;
459
460 Value::ConstIterator end() const;
461
470 Value::Iterator begin();
471
480 Value::Iterator end();
481
488 const std::string dump(const Value& object, int depth = 0) const;
489
490 private:
492 const std::string dump_key(const Value& key) const;
493
495 const std::string dump_value(const Value& val) const;
496
498 const std::string print_key(const std::string& type, uint size) const;
499
501 const std::string print_value(const std::string& val) const;
502
513 {
514 int int_; // Type 1
515 uint uint_; // Type 2
516 real64 real_; // Type 3
517 bool bool_; // Type 4
518 // NOTE: Instance-owned pointer.
519 const char* string_; // Type 5
520 // NOTE: Instance-owned pointer.
521 Object* object_; // Type 6 and 7
522 };
523
526
529};
530
535std::ostream& operator <<(std::ostream& os, const Value& val);
536
537} // namespace nom
538
539#endif // include guard defined
540
Read-only iteration for nom::Value.
Generic interface for opaque data containers.
Definition Value.hpp:43
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:67
ValueHolder value_
The stored value in this instance.
Definition Value.hpp:528
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
bool operator==(const Value &rhs) const
Equality comparison operator.
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.
Value::SelfType & operator=(const SelfType &rhs)
Copy assignment operator.
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 operator<(const Value &rhs) const
Lesser than comparison operator.
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.
bool operator>=(const Value &rhs) const
Greater than or equal to comparison operator.
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.
bool operator>(const Value &rhs) const
Greater than or equal to comparison operator.
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:525
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.
bool operator<=(const Value &rhs) const
Lesser than or equal to comparison operator.
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.
bool operator!=(const Value &rhs) const
Not equal comparison operator.
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.
static const Value & null
Declared value of Null for this object.
Definition Value.hpp:81
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:513