nomlib
Loading...
Searching...
No Matches
nom::Value Class Reference

Generic interface for opaque data containers. More...

#include <Value.hpp>

Classes

union  ValueHolder
 Container for the data types able to be held. More...

Public Types

enum  ValueType {
  Null = 0 , SignedInteger , UnsignedInteger , RealNumber ,
  String , Boolean , ArrayValues , ObjectValues
}
 Supported data types able to be held inside the object. More...
typedef Value SelfType
typedef ValueRawPtr
typedef ValueReference
typedef ValueConstIterator ConstIterator
typedef ValueIterator Iterator
typedef std::vector< std::string > Members

Public Member Functions

 Value ()
 Default constructor; constructs an object with NullValue data type.
 Value (int val)
 Construct an object using a signed integer value.
 Value (uint val)
 Construct an object using an unsigned (non-negative) integer value.
 Value (real64 val)
 Construct an object using a double-precision floating point "real" number value.
 Value (const char *str)
 Construct an object from a C-style string value.
 Value (const std::string &str)
 Construct an object from a C++ string value (std::string).
 Value (bool val)
 Construct an object from a boolean value.
 Value (const Object &obj)
 Construct an object with either array or object node values.
 Value (ValueType type)
 Construct a Value container node of a specified type.
 Value (const Value &rhs)
 Copy constructor.
Value::SelfType & operator= (const SelfType &rhs)
 Copy assignment operator.
void swap (Value &rhs)
 Exchange the contents of the container; copy & swap idiom.
bool operator< (const Value &rhs) const
 Lesser than comparison operator.
bool operator<= (const Value &rhs) const
 Lesser than or equal to comparison operator.
bool operator>= (const Value &rhs) const
 Greater than or equal to comparison operator.
bool operator> (const Value &rhs) const
 Greater than or equal to comparison operator.
bool operator== (const Value &rhs) const
 Equality comparison operator.
bool operator!= (const Value &rhs) const
 Not equal comparison operator.
bool operator! () const
 Returns Value::null.
int compare (const Value &rhs) const
 Internal helper method for comparing array & object node containers.
Value::RawPtr get ()
 Obtain a pointer to the object.
Value::Reference ref ()
 Obtain a reference to the object.
enum Value::ValueType type () const
 Obtain the enumeration type of the object.
const std::string type_name () const
 Obtain the enumeration type of the object as a std::string.
bool null_type () const
 Query if the value type type stored in the object is NULL.
bool int_type () const
 Query if the value type type stored in the object is a signed integer.
bool uint_type () const
 Query if the value type type stored in the object is an unsigned integer (non-negative.
bool double_type () const
 Query if the value type type stored in the object is a double- precision floating point real number.
bool float_type () const
 Query if the value type type stored in the object is a double- precision floating point real number.
bool string_type () const
 Query if the value type type stored in the object is a string value.
bool bool_type () const
 Query if the value type type stored in the object is a boolean value.
bool array_type () const
 Query if the value type type stored in the object are array values.
bool object_type () const
 Query if the value type type stored in the object are object values.
const std::string stringify () const
int get_int () const
 Obtain the signed integer value stored within the container.
uint get_uint () const
 Obtain the unsigned (non-negative) integer value stored within the container.
real64 get_double () const
 Obtain the double-precision floating point "real" number value stored within the container.
real32 get_float () const
 Obtain the double-precision floating point "real" number value stored within the container.
const char * get_cstring () const
 Obtain the C style string value stored within the container.
std::string get_string () const
 Obtain the string value stored within the container.
bool get_bool () const
 Obtain the boolean value stored within the container.
bool array_valid () const
bool object_valid () const
const Object array () const
 Obtain the array values of the object.
const Object object () const
 Obtain the object tree of the object.
nom::size_type size () const
 Obtain the size of the object's contained values.
bool empty () const
 Obtain boolean response in regards to container's empty status.
void clear ()
 Remove all array and object members.
Valueoperator[] (ArrayIndex index)
 Obtain a stored element by index number.
Valueoperator[] (int index)
const Valueoperator[] (ArrayIndex index) const
 Obtain a stored element by index number.
const Valueoperator[] (int index) const
Valueoperator[] (const char *key)
 Access an object node's container.
Valueoperator[] (const std::string &key)
const Valueoperator[] (const char *key) const
const Valueoperator[] (const std::string &key) const
Valuepush_back (const Value &val)
 Insert array elements.
const Valuefind (const std::string &key) const
 Search the object for an existing member.
Value erase (const std::string &key)
 Remove the named member.
Members member_names () const
 Remove an array object.
Value::ConstIterator begin () const
Value::ConstIterator end () const
Value::Iterator begin ()
 Iterator access to the beginning of the object's tree.
Value::Iterator end ()
 Iterator access to the end of the object's tree.
const std::string dump (const Value &object, int depth=0) const
 Dump the object's complete value tree.

Static Public Attributes

static const Valuenull
 Declared value of Null for this object.

Private Member Functions

const std::string dump_key (const Value &key) const
 Internal helper method for nom::Value::dump.
const std::string dump_value (const Value &val) const
 Internal helper method for nom::Value::dump.
const std::string print_key (const std::string &type, uint size) const
 Internal helper method for nom::Value::dump_key.
const std::string print_value (const std::string &val) const
 Internal helper method for nom::Value::dump_value.

Private Attributes

enum ValueType type_
 The type of stored value in this instance.
ValueHolder value_
 The stored value in this instance.

Detailed Description

Generic interface for opaque data containers.

Inspired by Apple's PropertyList API & made possible by JSONCPP, ...

[STUB]

References (class design & inspiration):

JSON RFC json_spirit jsoncpp cijson (of the Cinder engine) Boost::PropertyTree Apple's PropertyList (.plist)

Todo
Implement support for (un)-signed 64-bit integers
Todo
Implement support for comments (XML & JSON style)

Definition at line 42 of file Value.hpp.

Member Typedef Documentation

◆ ConstIterator

typedef ValueConstIterator nom::Value::ConstIterator

Definition at line 50 of file Value.hpp.

◆ Iterator

typedef ValueIterator nom::Value::Iterator

Definition at line 53 of file Value.hpp.

◆ Members

typedef std::vector<std::string> nom::Value::Members

Definition at line 55 of file Value.hpp.

◆ RawPtr

typedef Value* nom::Value::RawPtr

Definition at line 46 of file Value.hpp.

◆ Reference

typedef Value& nom::Value::Reference

Definition at line 47 of file Value.hpp.

◆ SelfType

typedef Value nom::Value::SelfType

Definition at line 45 of file Value.hpp.

Member Enumeration Documentation

◆ ValueType

Supported data types able to be held inside the object.

Remarks
ValueType::ObjectValue type is a singly-list of key / value pairs; this allows one to use this class to serialize and unserialize many common file formats, such as: JSON, XML, INI & plist.
Note
Borrowed over from the wonderful JsonCpp API.

Definition at line 66 of file Value.hpp.

Constructor & Destructor Documentation

◆ Value() [1/7]

nom::Value::Value ( int val)

Construct an object using a signed integer value.

Note
Type 1

◆ Value() [2/7]

nom::Value::Value ( uint val)

Construct an object using an unsigned (non-negative) integer value.

Remarks
It may be necessary to append the 'u' symbol after the number in order to have the compiler recognize the request and route to the correct constructor.
Note
Type 2

◆ Value() [3/7]

nom::Value::Value ( real64 val)

Construct an object using a double-precision floating point "real" number value.

Note
Type 3

◆ Value() [4/7]

nom::Value::Value ( const char * str)

Construct an object from a C-style string value.

Remarks
A duplicate copy of the passed C string value is made upon construction.
Note
This constructor makes construction using string literals possible:
Note
Without defining this constructor, string literals were being overloaded by the boolean constructor.
Note
Type 4

◆ Value() [5/7]

nom::Value::Value ( const std::string & str)

Construct an object from a C++ string value (std::string).

Note
Type 4

◆ Value() [6/7]

nom::Value::Value ( bool val)

Construct an object from a boolean value.

Note
Type 5

◆ Value() [7/7]

nom::Value::Value ( const Object & obj)

Construct an object with either array or object node values.

Note
Type 6 or 7

Member Function Documentation

◆ array()

const Object nom::Value::array ( ) const

Obtain the array values of the object.

Returns
Return-by-value cloned copy of the nom::Array pointer held by this object.

◆ array_type()

bool nom::Value::array_type ( ) const

Query if the value type type stored in the object are array values.

Remarks
nom::Value object may be either an array OR object type at any given time.

◆ begin()

Value::Iterator nom::Value::begin ( )

Iterator access to the beginning of the object's tree.

Returns
nom::ValueIterator object pointing to the beginning of the array or object. On err, an invalid, empty nom::ValueIterator is returned.
Remarks
The nom::Value object must be initialized as a nom::ArrayValue or nom::ObjectValue type.

◆ clear()

void nom::Value::clear ( )

Remove all array and object members.

Remarks
The object's value type is unchanged.
Note
This method has no effect unless the object's container is one of two node types: array or object.

◆ compare()

int nom::Value::compare ( const Value & rhs) const

Internal helper method for comparing array & object node containers.

Note
Borrowed from JsonCpp library – thanks!

◆ dump()

const std::string nom::Value::dump ( const Value & object,
int depth = 0 ) const

Dump the object's complete value tree.

Remarks
This is a useful method for debugging.
Note
This method is used by the << overload function for nom::Value objects.

◆ empty()

bool nom::Value::empty ( ) const

Obtain boolean response in regards to container's empty status.

Remarks
The object's type is not modified.
Note
This method will fail with an assert if the container type is not either: null, array or object node type.

◆ end()

Value::Iterator nom::Value::end ( )

Iterator access to the end of the object's tree.

Returns
nom::ValueIterator object pointing to the beginning of the array or object. On err, an invalid, empty nom::ValueIterator is returned.
Remarks
The nom::Value object must be initialized as a nom::ArrayValue or nom::ObjectValue type.

◆ erase()

Value nom::Value::erase ( const std::string & key)

Remove the named member.

Returns
Removed member upon success, or Value::null upon failure.
Remarks
The object is unchanged if the referenced key does not exist.
Note
The object's type is not modified.

◆ find()

const Value & nom::Value::find ( const std::string & key) const

Search the object for an existing member.

Returns
The found member key upon success, or Value::null upon failure.
Note
The object's type is not modified.
This method will fail with an assert if the container type is not either a null or object node type.

◆ get()

Value::RawPtr nom::Value::get ( )

Obtain a pointer to the object.

Returns
A raw pointer to the object. No transfer of ownership is made.

◆ get_bool()

bool nom::Value::get_bool ( ) const

Obtain the boolean value stored within the container.

Returns
On err, boolean false is returned.

◆ get_cstring()

const char * nom::Value::get_cstring ( ) const

Obtain the C style string value stored within the container.

Returns
On err, nullptr is returned.

Remarks
A copy of the stored C string is made, therefore no ownership transfers occur; you are responsible for freeing the returned C string.

◆ get_double()

real64 nom::Value::get_double ( ) const

Obtain the double-precision floating point "real" number value stored within the container.

Returns
On err, zero (0) is returned.

◆ get_float()

real32 nom::Value::get_float ( ) const

Obtain the double-precision floating point "real" number value stored within the container.

Returns
On err, zero (0) is returned.
Remarks
Conversion from the internally stored double variable to a float type is done.

◆ get_int()

int nom::Value::get_int ( ) const

Obtain the signed integer value stored within the container.

Returns
On err, zero (0) is returned.

◆ get_string()

std::string nom::Value::get_string ( ) const

Obtain the string value stored within the container.

Remarks
Conversion from the internally stored char* variable to a std::string is done.
Returns
On err, a null-terminated std::string – "\0" is returned.

◆ get_uint()

uint nom::Value::get_uint ( ) const

Obtain the unsigned (non-negative) integer value stored within the container.

Returns
On err, zero (0) is returned.

◆ member_names()

Members nom::Value::member_names ( ) const

Remove an array object.

Returns
The removed array object upon success, or Value::null on failure, such as when you specify an invalid position index.
Remarks
The object is unchanged if the referenced key does not exist.
Note
The object's type is not modified.
This method will fail with an assert if the container type is not either a null or object node type.
Todo
Verify working state of method.

Obtain the member names (keys) of each pair in this container.

Returns
A vector of strings upon success, or an empty vector upon failure.
Note
This method will fail with an assert if the container type is not either a null or object node type.

◆ object()

const Object nom::Value::object ( ) const

Obtain the object tree of the object.

Returns
Return-by-value cloned copy of the nom::Object pointer held by this object.

◆ object_type()

bool nom::Value::object_type ( ) const

Query if the value type type stored in the object are object values.

Remarks
nom::Value object may be either an array OR object type at any given time.

◆ operator!()

bool nom::Value::operator! ( ) const

Returns Value::null.

Note
Borrowed from JsonCpp library – thanks!

◆ operator!=()

bool nom::Value::operator!= ( const Value & rhs) const

Not equal comparison operator.

Note
Borrowed from JsonCpp library – thanks!

◆ operator<()

bool nom::Value::operator< ( const Value & rhs) const

Lesser than comparison operator.

Note
Borrowed from JsonCpp library – thanks!
Todo
Unit testing of the operator overload logic for lesser than equality.

◆ operator<=()

bool nom::Value::operator<= ( const Value & rhs) const

Lesser than or equal to comparison operator.

Note
Borrowed from JsonCpp library – thanks!

◆ operator==()

bool nom::Value::operator== ( const Value & rhs) const

Equality comparison operator.

Note
Borrowed from JsonCpp library – thanks!
Todo
Unit testing of the operator overload logic for equality.

◆ operator>()

bool nom::Value::operator> ( const Value & rhs) const

Greater than or equal to comparison operator.

Note
Borrowed from JsonCpp library – thanks!

◆ operator>=()

bool nom::Value::operator>= ( const Value & rhs) const

Greater than or equal to comparison operator.

Note
Borrowed from JsonCpp library – thanks!

◆ operator[]() [1/5]

Value & nom::Value::operator[] ( ArrayIndex index)

Obtain a stored element by index number.

Parameters
valUnsigned integer of the element's index to return.
Remarks
It may be necessary to append the 'u' symbol after the number in order to have the compiler recognize the request and route to the correct constructor.
Note
In contrast to the standard STL method overloads for ::operator[](int), nom::Value should always perform bounds checking.

◆ operator[]() [2/5]

const Value & nom::Value::operator[] ( ArrayIndex index) const

Obtain a stored element by index number.

Parameters
valUnsigned integer of the element's index to return.
Remarks
It may be necessary to append the 'u' symbol after the number in order to have the compiler recognize your request properly. (Signed integers are the "default" literal integer type, at least on my development system).
Todo
Fix index methods – VString, ValueBaseIterator & co; this method call relies on our half-finished implementation stemming from the VString class.
Note
In contrast to the standard STL method overloads for ::operator[](int), nom::Value should always perform bounds checking.

◆ operator[]() [3/5]

Value & nom::Value::operator[] ( const char * key)

Access an object node's container.

Remarks
This method call requires an object node container type, and will immediately initialize one, if one is not found to be valid at the time of the call – existing value(s) in the object will be lost.
Note
In contrast to the standard STL method overloads for ::operator[](int), nom::Value should always perform bounds checking.

◆ operator[]() [4/5]

Value & nom::Value::operator[] ( int index)
Note
In contrast to the standard STL method overloads for ::operator[](int), nom::Value should always perform bounds checking.

◆ operator[]() [5/5]

const Value & nom::Value::operator[] ( int index) const
Note
In contrast to the standard STL method overloads for ::operator[](int), nom::Value should always perform bounds checking.

◆ ref()

Value::Reference nom::Value::ref ( )

Obtain a reference to the object.

Returns
A reference to the object.

◆ size()

nom::size_type nom::Value::size ( ) const

Obtain the size of the object's contained values.

Returns
Size of the array or object, or one (1) when not said type. On err – when the object is ValueType::NullValue – zero (0).

◆ swap()

void nom::Value::swap ( Value & rhs)

Exchange the contents of the container; copy & swap idiom.

Remarks
In particular, one must be careful to keep track of copying our char* strings as necessary.
Note
This method is used in the implementation of the copy assignment operator.

◆ type()

enum Value::ValueType nom::Value::type ( ) const

Obtain the enumeration type of the object.

Remarks
The type is defined by the value type(s) set at construction. See also: Value::ValueType enumeration.

◆ type_name()

const std::string nom::Value::type_name ( ) const

Obtain the enumeration type of the object as a std::string.

Remarks
The type is defined by the value type(s) set at construction. See also: Value::ValueType enumeration.
Todo
Rename to stringify_type?

Member Data Documentation

◆ null

const Value& nom::Value::null
static

Declared value of Null for this object.

Remarks
Value::null is the default constructor's type.

Definition at line 81 of file Value.hpp.

◆ type_

enum ValueType nom::Value::type_
private

The type of stored value in this instance.

Definition at line 525 of file Value.hpp.

◆ value_

ValueHolder nom::Value::value_
private

The stored value in this instance.

Definition at line 528 of file Value.hpp.


The documentation for this class was generated from the following file: