Support Forum G3D Web Page |
Easy loading and saving of human-readable configuration files.
More...
Classes | |
class | IndexOutOfBounds |
Thrown by operator[] when an array index is not present. More... | |
class | KeyNotFound |
Thrown by operator[] when a key is not present in a const table. More... | |
class | Source |
Where an Any came from in a file. More... | |
Public Types | |
typedef Array< Any > | AnyArray |
typedef Table< String, Any > | AnyTable |
enum | Type { NIL, BOOLEAN, NUMBER, STRING, ARRAY, TABLE, EMPTY_CONTAINER } |
Public Member Functions | |
Any () | |
NIL constructor. More... | |
Any (TextInput &t) | |
Deserialize. More... | |
Any (const Any &x) | |
Any (double x) | |
NUMBER constructor. More... | |
Any (float x) | |
Any (int64 x) | |
NUMBER constructor. More... | |
Any (long x) | |
NUMBER constructor. More... | |
Any (int x) | |
NUMBER constructor. More... | |
Any (char x) | |
NUMBER constructor. More... | |
Any (short x) | |
NUMBER constructor. More... | |
Any (bool x) | |
BOOLEAN constructor. More... | |
Any (const String &x) | |
STRING constructor. More... | |
Any (const char *x) | |
STRING constructor. More... | |
Any (Type t, const String &name="", const String &brackets="", const char separator='\0') | |
t must be ARRAY or TABLE More... | |
template<class T > | |
Any (const T &v) | |
Extensible constructor: call the toAny() method of any class. More... | |
template<class T > | |
Any (const Array< T > &array, const String &name="") | |
Assumes that Any(T) is well-defined, e.g., by T defining a T::toAny() method. More... | |
~Any () | |
template<class T0 > | |
void | append (const T0 &v0) |
template<class T0 , class T1 > | |
void | append (const T0 &v0, const T1 &v1) |
template<class T0 , class T1 , class T2 > | |
void | append (const T0 &v0, const T1 &v1, const T2 &v2) |
template<class T0 , class T1 , class T2 , class T3 > | |
void | append (const T0 &v0, const T1 &v1, const T2 &v2, const T3 &v3) |
const Array< Any > & | array () const |
Directly exposes the underlying data structure for an ARRAY. More... | |
bool | boolean () const |
void | clear () |
Clears all entries. More... | |
const String & | comment () const |
Comments appear before values when they are in serialized form. More... | |
bool | containsKey (const String &key) const |
Returns true if this key is in the TABLE. More... | |
void | deserialize (TextInput &ti) |
Parse from a stream. More... | |
void | deserialize (class BinaryInput &b) |
float | floatValue () const |
template<class T > | |
Any | get (const String &key, const T &defaultVal) const |
For a table, returns the element for key x and defaultVal if it does not exist. More... | |
template<class T > | |
void | getArray (Array< T > &array) const |
Assumes that T defines T(const Any&) More... | |
template<> | |
void | getArray (Array< String > &array) const |
Template specialization to avoid defining G3DString(const Any&) and related methods. More... | |
template<class T > | |
void | getTable (Table< String, T > &table) const |
Assumes that T defines T(const Any&) More... | |
bool | isNil () const |
True if this is the NIL value. More... | |
const Any & | last () const |
Any & | last () |
int | length () const |
void | load (const String &filename) |
Parse from a file. More... | |
void | loadIfExists (const String &filename) |
Load filename file if it exists, otherwise do not modify this. More... | |
const String & | name () const |
If this is named ARRAY or TABLE, returns the name. More... | |
bool | nameBeginsWith (const String &s) const |
If this is named ARRAY or TABLE, returns true if the name begins with s. More... | |
bool | nameBeginsWith (const char *s) const |
If this is named ARRAY or TABLE, returns true if the name begins with s. More... | |
bool | nameEquals (const String &s) const |
If this is named ARRAY or TABLE, returns true if the name is s. More... | |
bool | nameEquals (const char *s) const |
If this is named ARRAY or TABLE, returns true if the name iss. More... | |
Any & | next () |
for an ARRAY, resizes and returns the last element More... | |
double | number () const |
Throws a ParseError exception if this is not a number. More... | |
operator bool () const | |
operator char () const | |
operator double () const | |
operator float () const | |
operator int () const | |
operator int16 () const | |
operator String () const | |
operator uint16 () const | |
operator uint32 () const | |
operator uint8 () const | |
bool | operator!= (const Any &x) const |
bool | operator!= (const String &s) const |
bool | operator!= (const double &v) const |
bool | operator!= (int v) const |
bool | operator!= (bool v) const |
Any & | operator= (const Any &x) |
Removes the comment and name. More... | |
Any & | operator= (Type t) |
t must be ARRAY, TABLE, or NIL. More... | |
template<class T > | |
Any & | operator= (const Array< T > &array) |
Assigns from an array. More... | |
template<class T > | |
Any & | operator= (const T &v) |
Removes the comment and name. More... | |
bool | operator== (const Any &x) const |
True if the Anys are exactly equal, ignoring comments. More... | |
bool | operator== (const String &s) const |
bool | operator== (const double &v) const |
bool | operator== (int v) const |
bool | operator== (bool v) const |
const Any & | operator[] (int i) const |
For an array, returns the ith element. More... | |
Any & | operator[] (int i) |
const Any & | operator[] (const String &key) const |
For a table, returns the element for key. More... | |
const Any & | operator[] (const char *key) const |
Any & | operator[] (const String &key) |
Fetch an element from a table. More... | |
Any & | operator[] (const char *key) |
Fetch an element from a table. More... | |
void | remove (const String &key) |
Removes this key from the Any, which must be a table. More... | |
void | remove (int index) |
Removes this key from the Any, which must be an array, and shifts other elements down to maintain order. More... | |
void | resize (int n) |
Resize to n elements, where new elements are NIL It is an error to call this method if this is not an Any::ARRAY. More... | |
String | resolveStringAsFilename (bool errorIfNotFound=true) const |
If a valid string, takes the string value and creates a fully qualified filename. More... | |
void | save (const String &filename, bool json=false) const |
Uses the serialize method. More... | |
void | serialize (TextOutput &to, bool json=false, bool coerce=false) const |
void | serialize (class BinaryOutput &b) const |
template<class T > | |
void | set (const String &key, const T &val) |
For a table, assigns the element for key k. More... | |
void | setComment (const String &c) |
void | setName (const String &name) |
Set the name used when serializing an ARRAY or TABLE. More... | |
int | size () const |
Number of elements if this is an ARRAY or TABLE. More... | |
const Source & | source () const |
String | sourceDirectory () const |
The parent directory of the location from which this Any was loaded. More... | |
const String & | string () const |
const Table< String, Any > & | table () const |
Directly exposes the underlying data structure for table. More... | |
Type | type () const |
String | unparse (const TextOutput::Settings &s=TextOutput::Settings()) const |
String | unparseJSON (const TextOutput::Settings &s=TextOutput::Settings(), bool allowCoercion=true) const |
void | verify (bool value, const String &message="") const |
Throws a ParseError if value is false. More... | |
void | verifyName (const String &n) const |
Verifies that the name is identifier n (case sensitive). More... | |
void | verifyName (const String &n, const String &m) const |
Verifies that the name is identifier n or m (case sensitive). More... | |
void | verifyName (const String &n, const String &m, const String &p) const |
Verifies that the name is identifier n or m or p (case sensitive). More... | |
void | verifyName (const String &n, const String &m, const String &p, const String &q) const |
Verifies that the name is identifier n or m or p or q (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n) const |
Verifies that the name begins with identifier n (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m) const |
Verifies that the name begins with identifier n or m (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m, const String &p) const |
Verifies that the name begins with identifier n or m or p (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m, const String &p, const String &q) const |
Verifies that the name begins with identifier n or m or p or q (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m, const String &p, const String &q, const String &r) const |
Verifies that the name begins with identifier n or m or p or q or r (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m, const String &p, const String &q, const String &r, const String &s) const |
Verifies that the name begins with identifier n or m or p or q or r or s (case sensitive). More... | |
void | verifyNameBeginsWith (const String &n, const String &m, const String &p, const String &q, const String &r, const String &s, const String &t) const |
Verifies that the name begins with identifier n or m or p or q or r or s or t(case sensitive). More... | |
void | verifySize (int low, int high) const |
Verifies that the size is between low and high, inclusive. More... | |
void | verifySize (int s) const |
Verifies that the size is exactly s. More... | |
void | verifyType (Type t) const |
Verifies that the type is t. More... | |
void | verifyType (Type t0, Type t1) const |
Throws an exception if the type is not t0 or t1. More... | |
Static Public Member Functions | |
static Any | fromFile (const String &filename) |
Load a new Any from filename. More... | |
static Any | parse (const String &src) |
Same as deserialize or load, but operates on a string instead of a stream or file. More... | |
static String | toString (Type t) |
Easy loading and saving of human-readable configuration files.
Any encodes typed, structured data and can serialize it to a human readable format that is very similar to the Python language's data syntax, and fully supports Python's data syntax as well. It is well-suited for quickly creating human-readable file formats, especially since deserialization and serialization preserve comments and an Any can tell you what file and line it came from. The syntax allows most C++ editors to properly highlight Any files, and makes it easy to design little ad-hoc C-like languages in configuration files.
The class is designed so that copying Anys generally is fast, even if it is a large array or table. This is because data is shared between copies until it is mutated, at which point an actual copy occurs.
A sample .Any file:
{ shape = "round",
// in meters radius = 3.7,
position = Vector3(1.0, -1.0, 0.0), video = { format = "RGB8", size = (320, 200)},
data = [1, 7, 15, 2, -inf, 21, 0],
material = #include("rocks.mat") }
Sample code for constructing an Any at runtime:
Reading from an Any:
Generating an Any file:
Interoperation with your own classes:
The custom serialization format was chosen to be terse, easy for humans to read, and easy for machines to parse. It was specifically chosen over formats like XML, YAML, JSON, S-expressions, and Protocol Buffers, although there is no reason you could not write readers and writers for G3D::Any that support those. Any also currently supports the JSON format.
G3D::Any assumes that structures do not contain cycles; it is an error to create a structure like:
Any x(Any::ARRAY); x.array().append(x); // don't do this!
although no exception will be thrown at runtime during that append.
typedef Array<Any> G3D::Any::AnyArray |
typedef Table<String, Any> G3D::Any::AnyTable |
enum G3D::Any::Type |
G3D::Any::Any | ( | ) |
NIL constructor.
Referenced by Any(), append(), get(), operator!=(), operator=(), operator==(), and set().
|
explicit |
Deserialize.
G3D::Any::Any | ( | const Any & | x | ) |
|
explicit |
NUMBER constructor.
|
explicit |
|
inlineexplicit |
NUMBER constructor.
|
explicit |
NUMBER constructor.
|
explicit |
NUMBER constructor.
|
explicit |
NUMBER constructor.
|
explicit |
NUMBER constructor.
|
explicit |
BOOLEAN constructor.
|
explicit |
STRING constructor.
|
explicit |
STRING constructor.
|
explicit |
t must be ARRAY or TABLE
brackets | must be "" (defaults to {} for table, () for array), "[]", "()", or "{}" |
separator | must be ';', ',', or '\0' (defaults to ',' for array and ';' for table) |
|
inlineexplicit |
Extensible constructor: call the toAny() method of any class.
G3D::Any::~Any | ( | ) |
|
inlineexplicit |
Assumes that Any(T) is well-defined, e.g., by T defining a T::toAny() method.
|
inline |
|
inline |
|
inline |
|
inline |
Directly exposes the underlying data structure for an ARRAY.
Referenced by Any(), getArray(), and operator=().
bool G3D::Any::boolean | ( | ) | const |
void G3D::Any::clear | ( | ) |
Clears all entries.
This must be a TABLE or ARRAY
const String& G3D::Any::comment | ( | ) | const |
Comments appear before values when they are in serialized form.
bool G3D::Any::containsKey | ( | const String & | key | ) | const |
Returns true if this key is in the TABLE.
Illegal to call on an object that is not a TABLE.
Referenced by G3D::AnyTableReader::containsUnread(), G3D::AnyTableReader::getFilenameIfPresent(), and G3D::AnyTableReader::getIfPresent().
void G3D::Any::deserialize | ( | class BinaryInput & | b | ) |
float G3D::Any::floatValue | ( | ) | const |
Load a new Any from filename.
For a table, returns the element for key x and defaultVal if it does not exist.
|
inline |
Assumes that T defines T(const Any&)
Referenced by G3D::AnyTableReader::get().
Template specialization to avoid defining G3DString(const Any&) and related methods.
Assumes that T defines T(const Any&)
Referenced by G3D::AnyTableReader::get().
bool G3D::Any::isNil | ( | ) | const |
True if this is the NIL value.
|
inline |
|
inline |
int G3D::Any::length | ( | ) | const |
void G3D::Any::load | ( | const String & | filename | ) |
Parse from a file.
void G3D::Any::loadIfExists | ( | const String & | filename | ) |
Load filename file if it exists, otherwise do not modify this.
const String& G3D::Any::name | ( | ) | const |
If this is named ARRAY or TABLE, returns the name.
Referenced by Any().
bool G3D::Any::nameBeginsWith | ( | const String & | s | ) | const |
If this is named ARRAY or TABLE, returns true if the name begins with s.
The comparision is case insensitive.
bool G3D::Any::nameBeginsWith | ( | const char * | s | ) | const |
If this is named ARRAY or TABLE, returns true if the name begins with s.
The comparision is case insensitive.
bool G3D::Any::nameEquals | ( | const String & | s | ) | const |
If this is named ARRAY or TABLE, returns true if the name is s.
The comparision is case insensitive.
bool G3D::Any::nameEquals | ( | const char * | s | ) | const |
If this is named ARRAY or TABLE, returns true if the name iss.
The comparision is case insensitive.
Any& G3D::Any::next | ( | ) |
for an ARRAY, resizes and returns the last element
double G3D::Any::number | ( | ) | const |
Throws a ParseError exception if this is not a number.
G3D::Any::operator bool | ( | ) | const |
|
inline |
G3D::Any::operator double | ( | ) | const |
G3D::Any::operator float | ( | ) | const |
G3D::Any::operator int | ( | ) | const |
|
inline |
G3D::Any::operator String | ( | ) | const |
|
inline |
G3D::Any::operator uint32 | ( | ) | const |
|
inline |
bool G3D::Any::operator!= | ( | const Any & | x | ) | const |
|
inline |
|
inline |
|
inline |
|
inline |
Assigns from an array.
Assumes that T can be converted to Any. Removes the comment and name
|
inline |
Removes the comment and name.
bool G3D::Any::operator== | ( | const Any & | x | ) | const |
True if the Anys are exactly equal, ignoring comments.
Applies deeply on arrays and tables.
|
inline |
|
inline |
|
inline |
|
inline |
const Any& G3D::Any::operator[] | ( | int | i | ) | const |
For an array, returns the ith element.
Referenced by operator=(), and operator[]().
Any& G3D::Any::operator[] | ( | int | i | ) |
For a table, returns the element for key.
Throws KeyNotFound exception if the element does not exist.
|
inline |
Fetch an element from a table.
This can be used as:
a["key"] = value; (create the key if it did not exist)
or
value = a["key"]; (throw an error if the key did not exist)
Note: In order to cause elements to be correctly created in the first case while still providing "key not found" errors in the second case, the Any returned is a special object that delays the actual fetch until the following assignment or method call. This means that in the event of an error, the exception may be thrown from a line other than the actual fetch. Use the Any::get() or the const Any::operator[]() methods to avoid this behavior and ensure error-checking at fetch time.
|
inline |
Fetch an element from a table.
This can be used as:
a["key"] = value; (create the key if it did not exist)
or
value = a["key"]; (throw an error if the key did not exist)
Note: In order to cause elements to be correctly created in the first case while still providing "key not found" errors in the second case, the Any returned is a special object that delays the actual fetch until the following assignment or method call. This means that in the event of an error, the exception may be thrown from a line other than the actual fetch. Use the Any::get() or the const Any::operator[]() methods to avoid this behavior and ensure error-checking at fetch time.
void G3D::Any::remove | ( | int | index | ) |
Removes this key from the Any, which must be an array, and shifts other elements down to maintain order.
void G3D::Any::resize | ( | int | n | ) |
Resize to n elements, where new elements are NIL It is an error to call this method if this is not an Any::ARRAY.
Referenced by Any(), and operator=().
String G3D::Any::resolveStringAsFilename | ( | bool | errorIfNotFound = true | ) | const |
If a valid string, takes the string value and creates a fully qualified filename.
The file is searched for the following ways:
Strings that begin with '<' and end with '>' are treated as escape sequences and are returned unmodifed.
Referenced by G3D::AnyTableReader::getFilenameIfPresent().
void G3D::Any::save | ( | const String & | filename, |
bool | json = false |
||
) | const |
Uses the serialize method.
If the extension is ".json", uses JSON format with coercion, otherwise uses native Any format.
void G3D::Any::serialize | ( | TextOutput & | to, |
bool | json = false , |
||
bool | coerce = false |
||
) | const |
coerce. | If json=true, should features that JSON doesn't support be coerced or produce errors? |
void G3D::Any::serialize | ( | class BinaryOutput & | b | ) | const |
|
inline |
For a table, assigns the element for key k.
void G3D::Any::setComment | ( | const String & | c | ) |
void G3D::Any::setName | ( | const String & | name | ) |
Set the name used when serializing an ARRAY or TABLE.
Only legal for ARRAY or TABLE. The name must begin with a letter and contain only letters, numbers, underscores and scope operators.
a2z hello Foo::bar color.red this->that __x
The scope operators "::", "->", and ".", may have spaces around them. The name may not contain parentheses.
Referenced by Any().
int G3D::Any::size | ( | ) | const |
Number of elements if this is an ARRAY or TABLE.
Referenced by getArray(), G3D::AnyTableReader::hasMore(), and last().
const Source& G3D::Any::source | ( | ) | const |
String G3D::Any::sourceDirectory | ( | ) | const |
const String& G3D::Any::string | ( | ) | const |
Referenced by G3D::CullFace::CullFace(), G3D::AnyTableReader::get(), and G3D::GKey::GKey().
Directly exposes the underlying data structure for table.
Referenced by getTable().
Type G3D::Any::type | ( | ) | const |
String G3D::Any::unparse | ( | const TextOutput::Settings & | s = TextOutput::Settings() | ) | const |
String G3D::Any::unparseJSON | ( | const TextOutput::Settings & | s = TextOutput::Settings() , |
bool | allowCoercion = true |
||
) | const |
allowCoercion | If false, throws an error if the Any uses features that are not supported by JSON such as named arrays. Otherwise, silently coerces to JSON. |
void G3D::Any::verify | ( | bool | value, |
const String & | message = "" |
||
) | const |
Throws a ParseError if value is false.
Useful for quickly creating parse rules in classes that deserialize from Any.
void G3D::Any::verifyName | ( | const String & | n | ) | const |
Verifies that the name is identifier n (case sensitive).
It may contain identifier operators after this
Verifies that the name is identifier n or m (case sensitive).
It may contain identifier operators after this
Verifies that the name is identifier n or m or p (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyName | ( | const String & | n, |
const String & | m, | ||
const String & | p, | ||
const String & | q | ||
) | const |
Verifies that the name is identifier n or m or p or q (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyNameBeginsWith | ( | const String & | n | ) | const |
Verifies that the name begins with identifier n (case sensitive).
It may contain identifier operators after this
Verifies that the name begins with identifier n or m (case sensitive).
It may contain identifier operators after this
Verifies that the name begins with identifier n or m or p (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyNameBeginsWith | ( | const String & | n, |
const String & | m, | ||
const String & | p, | ||
const String & | q | ||
) | const |
Verifies that the name begins with identifier n or m or p or q (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyNameBeginsWith | ( | const String & | n, |
const String & | m, | ||
const String & | p, | ||
const String & | q, | ||
const String & | r | ||
) | const |
Verifies that the name begins with identifier n or m or p or q or r (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyNameBeginsWith | ( | const String & | n, |
const String & | m, | ||
const String & | p, | ||
const String & | q, | ||
const String & | r, | ||
const String & | s | ||
) | const |
Verifies that the name begins with identifier n or m or p or q or r or s (case sensitive).
It may contain identifier operators after this
void G3D::Any::verifyNameBeginsWith | ( | const String & | n, |
const String & | m, | ||
const String & | p, | ||
const String & | q, | ||
const String & | r, | ||
const String & | s, | ||
const String & | t | ||
) | const |
Verifies that the name begins with identifier n or m or p or q or r or s or t(case sensitive).
It may contain identifier operators after this
void G3D::Any::verifySize | ( | int | low, |
int | high | ||
) | const |
Verifies that the size is between low and high, inclusive.
void G3D::Any::verifySize | ( | int | s | ) | const |
Verifies that the size is exactly s.
void G3D::Any::verifyType | ( | Type | t | ) | const |
Verifies that the type is t.
Referenced by getArray(), and getTable().