G3D::AnyVal Class ReferenceA generic value, useful for defining property trees that can be loaded from and saved to disk.
More...
#include <AnyVal.h>
List of all members.
|
Public Types |
| enum | Type {
NIL,
NUMBER,
BOOLEAN,
STRING,
VECTOR2,
VECTOR3,
VECTOR4,
MATRIX3,
MATRIX4,
QUAT,
COORDINATEFRAME,
COLOR3,
COLOR4,
RECT2D,
AABOX,
ARRAY,
TABLE
} |
Public Member Functions |
| const G3D::AABox & | aabox (const G3D::AABox &defaultVal) const |
| const G3D::AABox & | aabox () const |
| | AnyVal (Type arrayOrTable) |
| | AnyVal (const AnyVal &) |
| | AnyVal (const G3D::Matrix4 &) |
| | AnyVal (const G3D::Matrix3 &) |
| | AnyVal (const G3D::CoordinateFrame &) |
| | AnyVal (const G3D::AABox &) |
| | AnyVal (const G3D::Rect2D &) |
| | AnyVal (const G3D::Quat &) |
| | AnyVal (const char *) |
| | AnyVal (const std::string &) |
| | AnyVal (const G3D::Color4 &) |
| | AnyVal (const G3D::Color3 &) |
| | AnyVal (const G3D::Vector4 &) |
| | AnyVal (const G3D::Vector3 &) |
| | AnyVal (const G3D::Vector2 &) |
| | AnyVal (bool) |
| | AnyVal (double) |
| | AnyVal (G3D::TextInput &t) |
| | AnyVal () |
| void | append (const AnyVal &) |
| bool | boolean (bool b) const |
| bool | boolean () const |
| const G3D::Color3 & | color3 (const G3D::Color3 &defaultVal) const |
| const G3D::Color3 & | color3 () const |
| const G3D::Color4 & | color4 (const G3D::Color4 &defaultVal) const |
| const G3D::Color4 & | color4 () const |
| const G3D::CoordinateFrame & | coordinateFrame (const G3D::CoordinateFrame &defaultVal) const |
| const G3D::CoordinateFrame & | coordinateFrame () const |
| void | deserialize (G3D::TextInput &t) |
| const AnyVal & | get (const std::string &key) const |
| const AnyVal & | get (const std::string &key, const AnyVal &defaultVal) const |
| const AnyVal & | get (int i) const |
| const AnyVal & | get (int i, const AnyVal &defaultVal) const |
| void | getKeys (G3D::Array< std::string > &) const |
| bool | isNil () const |
| void | load (const std::string &filename) |
| const G3D::Matrix3 & | matrix3 (const G3D::Matrix3 &defaultVal) const |
| const G3D::Matrix3 & | matrix3 () const |
| const G3D::Matrix4 & | matrix4 (const G3D::Matrix4 &defaultVal) const |
| const G3D::Matrix4 & | matrix4 () const |
| double | number (double defaultVal) const |
| double | number () const |
| | operator bool () const |
| | operator const AABox & () const |
| | operator const Color3 & () const |
| | operator const Color4 & () const |
| | operator const CoordinateFrame & () const |
| | operator const Matrix3 & () const |
| | operator const Matrix4 & () const |
| | operator const Quat & () const |
| | operator const Rect2D & () const |
| | operator const std::string & () const |
| | operator const Vector2 & () const |
| | operator const Vector3 & () |
| | operator const Vector4 & () const |
| | operator double () const |
| | operator float () const |
| AnyVal & | operator= (const AnyVal &) |
| AnyVal & | operator[] (const char *) |
| const AnyVal & | operator[] (const char *) const |
| AnyVal & | operator[] (const std::string &) |
| const AnyVal & | operator[] (const std::string &) const |
| AnyVal & | operator[] (int) |
| const AnyVal & | operator[] (int) const |
| const G3D::Quat & | quat (const G3D::Quat &defaultVal) const |
| const G3D::Quat & | quat () const |
| const G3D::Rect2D & | rect2D (const G3D::Rect2D &defaultVal) const |
| const G3D::Rect2D & | rect2D () const |
| void | save (const std::string &filename) const |
| void | serialize (G3D::TextOutput &t) const |
| int | size () const |
| const std::string & | string (const std::string &defaultVal) const |
| const std::string & | string () const |
| std::string | toString () const |
| Type | type () const |
| const G3D::Vector2 & | vector2 (const G3D::Vector2 &defaultVal) const |
| const G3D::Vector2 & | vector2 () const |
| const G3D::Vector3 & | vector3 (const G3D::Vector3 &defaultVal) const |
| const G3D::Vector3 & | vector3 () const |
| const G3D::Vector4 & | vector4 (const G3D::Vector4 &defaultVal) const |
| const G3D::Vector4 & | vector4 () const |
| | ~AnyVal () |
Static Public Member Functions |
| static AnyVal | fromFile (const std::string &filename) |
Classes |
| class | CorruptText |
| | Thrown when deserialize() when the input is incorrectly formatted. More...
|
| class | Exception |
| | Base class for all AnyVal exceptions. More...
|
| class | IndexOutOfBounds |
| class | KeyNotFound |
| | Thrown by operator[] when a key is not present. More...
|
| class | WrongType |
| | Thrown when an inappropriate operation is performed (e.g., operator[] on a number). More...
|
Detailed Description
A generic value, useful for defining property trees that can be loaded from and saved to disk.
The values are intentionally restricted to a small set.
See also boost::any for a more general puprose but slightly harder to use "any" for C++.
The semantics of operator[] and the get() methods are slightly different; operator[] acts more like a scripting language that automatically extends arrays and tables instead of generating errors. get() has more strict semantics, like a C++ class.
AnyVal uses copy-on-mutate, so that AnyVal a = b semantically copies b (like int a = b would), although in practice it delays the copy until one is mutated so that it is still fast to "copy" large arrays and tables.
Reading example:
AnyVal property = AnyVal::fromFile("c:/tmp/test.txt"));
Vector3 vel = property["angular velocity"]
\/\/ Using defaults to handle errors:
\/\/ If there was no "enabled" value, this will return the default instead of failing
bool enabled = property["enabled"].boolean(true);
Writing to a file:
AnyVal dict(AnyVal::TABLE);
dict["enabled"] = AnyVal(true);
dict["weight"] = 100;
dict["angular velocity"] = Vector3(1, -3, 4.5);
TextOutput t("c:/tmp/test.txt");
dict.serialize(t);
t.commit();
Example of a data file:
{
heights = [1, 17, 32]
model =
{
color = C3(1, 1, 1)
filename = "foo.md2"
}
position = V3(23, 14, 0)
name = "Elmer"
}
What's the difference from boost::any?
I think that AnyVal will be easier for novice C++ users. It addresses the problem that even though G3D::TextInput makes reading configuration files extremely simple, many people still don't use it. So AnyVal makes it ridiculously simple to read and write a tree of G3D types to a file.
AnyVal:
{
AnyVal tree(TextInput("config.txt"));
bool enabled = tree.get("enabled", false);
Vector3 direction = tree.get("direction", Vector3::zero());
...
}
boost:
{
bool enabled = false;
Vector3 direction;
Table<boost::any> tree;
...write lots of file parsing code...
if (tree.containsKey("enabled")) {
const boost::any& val = tree["enabled"];
try
{
enabled = any_cast<bool>(val);
}
catch(const boost::bad_any_cast &)
{
}
}
if (tree.containsKey("direction")) {
const boost::any& val = tree["direction"];
try
{
direction = any_cast<Vector3>(val);
}
catch(const boost::bad_any_cast &)
{
}
}
...
}
Member Enumeration Documentation
Arrays and tables have heterogeneous element types.
- Enumerator:
-
| NIL |
|
| NUMBER |
|
| BOOLEAN |
|
| STRING |
|
| VECTOR2 |
|
| VECTOR3 |
|
| VECTOR4 |
|
| MATRIX3 |
|
| MATRIX4 |
|
| QUAT |
|
| COORDINATEFRAME |
|
| COLOR3 |
|
| COLOR4 |
|
| RECT2D |
|
| AABOX |
|
| ARRAY |
|
| TABLE |
|
Constructor & Destructor Documentation
| G3D::AnyVal::AnyVal |
( |
double |
|
) |
|
| G3D::AnyVal::AnyVal |
( |
bool |
|
) |
[explicit] |
| G3D::AnyVal::AnyVal |
( |
const std::string & |
|
) |
|
| G3D::AnyVal::AnyVal |
( |
const char * |
|
) |
|
| G3D::AnyVal::AnyVal |
( |
const AnyVal & |
|
) |
|
| G3D::AnyVal::AnyVal |
( |
Type |
arrayOrTable |
) |
|
Frees the underlying storage.
Member Function Documentation
| void G3D::AnyVal::append |
( |
const AnyVal & |
|
) |
|
Extend this array by one element.
| bool G3D::AnyVal::boolean |
( |
bool |
b |
) |
const |
| bool G3D::AnyVal::boolean |
( |
|
) |
const |
| static AnyVal G3D::AnyVal::fromFile |
( |
const std::string & |
filename |
) |
[static] |
| const AnyVal& G3D::AnyVal::get |
( |
const std::string & |
key |
) |
const |
Throws KeyNotFound exception if the key is not present.
| const AnyVal& G3D::AnyVal::get |
( |
const std::string & |
key, |
|
|
const AnyVal & |
defaultVal | |
|
) |
| | const |
Returns defaultVal if this is not a TABLE or the key is not found.
| const AnyVal& G3D::AnyVal::get |
( |
int |
i |
) |
const |
| const AnyVal& G3D::AnyVal::get |
( |
int |
i, |
|
|
const AnyVal & |
defaultVal | |
|
) |
| | const |
If i is out of bounds or this is not an ARRAY, defaultVal is returned.
| void G3D::AnyVal::getKeys |
( |
G3D::Array< std::string > & |
|
) |
const |
For a table, returns the keys.
| bool G3D::AnyVal::isNil |
( |
|
) |
const [inline] |
| void G3D::AnyVal::load |
( |
const std::string & |
filename |
) |
|
| double G3D::AnyVal::number |
( |
double |
defaultVal |
) |
const |
If this value is not a number, returns defaultVal.
| double G3D::AnyVal::number |
( |
|
) |
const |
If this value is not a number throws a WrongType exception.
| G3D::AnyVal::operator bool |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const AABox & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Color3 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Color4 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Matrix3 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Matrix4 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Quat & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Rect2D & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const std::string & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Vector2 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator const Vector3 & |
( |
|
) |
[inline] |
| G3D::AnyVal::operator const Vector4 & |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator double |
( |
|
) |
const [inline] |
| G3D::AnyVal::operator float |
( |
|
) |
const [inline] |
| AnyVal& G3D::AnyVal::operator[] |
( |
const char * |
|
) |
|
Table reference.
If the element does not exist, it is created.
| const AnyVal& G3D::AnyVal::operator[] |
( |
const char * |
|
) |
const |
| AnyVal& G3D::AnyVal::operator[] |
( |
const std::string & |
|
) |
|
Table reference.
If the element does not exist, it is created.
| const AnyVal& G3D::AnyVal::operator[] |
( |
const std::string & |
|
) |
const |
| AnyVal& G3D::AnyVal::operator[] |
( |
int |
|
) |
|
If the index is out of bounds, the array is resized.
If the index is negative, IndexOutOfBounds is thrown.
| const AnyVal& G3D::AnyVal::operator[] |
( |
int |
|
) |
const |
| void G3D::AnyVal::save |
( |
const std::string & |
filename |
) |
const |
| int G3D::AnyVal::size |
( |
|
) |
const |
Number of elements for an array or table.
| const std::string& G3D::AnyVal::string |
( |
const std::string & |
defaultVal |
) |
const |
| const std::string& G3D::AnyVal::string |
( |
|
) |
const |
| std::string G3D::AnyVal::toString |
( |
|
) |
const |
| Type G3D::AnyVal::type |
( |
|
) |
const |
The documentation for this class was generated from the following file:
Generated on Thu Aug 2 11:40:44 2007 for G3D by
1.5.2
Hosted by
|