Contents User Forum Source Index APIs by Task APIs by Level Data


G3D::AnyVal Class Reference

A 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::AABoxaabox (const G3D::AABox &defaultVal) const
const G3D::AABoxaabox () 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::Color3color3 (const G3D::Color3 &defaultVal) const
const G3D::Color3color3 () const
const G3D::Color4color4 (const G3D::Color4 &defaultVal) const
const G3D::Color4color4 () const
const G3D::CoordinateFramecoordinateFrame (const G3D::CoordinateFrame &defaultVal) const
const G3D::CoordinateFramecoordinateFrame () const
void deserialize (G3D::TextInput &t)
const AnyValget (const std::string &key) const
const AnyValget (const std::string &key, const AnyVal &defaultVal) const
const AnyValget (int i) const
const AnyValget (int i, const AnyVal &defaultVal) const
void getKeys (G3D::Array< std::string > &) const
bool isNil () const
void load (const std::string &filename)
const G3D::Matrix3matrix3 (const G3D::Matrix3 &defaultVal) const
const G3D::Matrix3matrix3 () const
const G3D::Matrix4matrix4 (const G3D::Matrix4 &defaultVal) const
const G3D::Matrix4matrix4 () 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
AnyValoperator= (const AnyVal &)
AnyValoperator[] (const char *)
const AnyValoperator[] (const char *) const
AnyValoperator[] (const std::string &)
const AnyValoperator[] (const std::string &) const
AnyValoperator[] (int)
const AnyValoperator[] (int) const
const G3D::Quatquat (const G3D::Quat &defaultVal) const
const G3D::Quatquat () const
const G3D::Rect2Drect2D (const G3D::Rect2D &defaultVal) const
const G3D::Rect2Drect2D () 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::Vector2vector2 (const G3D::Vector2 &defaultVal) const
const G3D::Vector2vector2 () const
const G3D::Vector3vector3 (const G3D::Vector3 &defaultVal) const
const G3D::Vector3vector3 () const
const G3D::Vector4vector4 (const G3D::Vector4 &defaultVal) const
const G3D::Vector4vector4 () 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 (  ) 

G3D::AnyVal::AnyVal ( G3D::TextInput t  )  [explicit]

Deserialize.

G3D::AnyVal::AnyVal ( double   ) 

Construct a number.

G3D::AnyVal::AnyVal ( bool   )  [explicit]

G3D::AnyVal::AnyVal ( const G3D::Vector2  ) 

G3D::AnyVal::AnyVal ( const G3D::Vector3  ) 

G3D::AnyVal::AnyVal ( const G3D::Vector4  ) 

G3D::AnyVal::AnyVal ( const G3D::Color3  ) 

G3D::AnyVal::AnyVal ( const G3D::Color4  ) 

G3D::AnyVal::AnyVal ( const std::string &   ) 

G3D::AnyVal::AnyVal ( const char *   ) 

G3D::AnyVal::AnyVal ( const G3D::Quat  ) 

G3D::AnyVal::AnyVal ( const G3D::Rect2D  ) 

G3D::AnyVal::AnyVal ( const G3D::AABox  ) 

G3D::AnyVal::AnyVal ( const G3D::CoordinateFrame  ) 

G3D::AnyVal::AnyVal ( const G3D::Matrix3  ) 

G3D::AnyVal::AnyVal ( const G3D::Matrix4  ) 

G3D::AnyVal::AnyVal ( const AnyVal  ) 

G3D::AnyVal::AnyVal ( Type  arrayOrTable  ) 

G3D::AnyVal::~AnyVal (  ) 

Frees the underlying storage.


Member Function Documentation

const G3D::AABox& G3D::AnyVal::aabox ( const G3D::AABox defaultVal  )  const

const G3D::AABox& G3D::AnyVal::aabox (  )  const

void G3D::AnyVal::append ( const AnyVal  ) 

Extend this array by one element.

bool G3D::AnyVal::boolean ( bool  b  )  const

bool G3D::AnyVal::boolean (  )  const

const G3D::Color3& G3D::AnyVal::color3 ( const G3D::Color3 defaultVal  )  const

const G3D::Color3& G3D::AnyVal::color3 (  )  const

const G3D::Color4& G3D::AnyVal::color4 ( const G3D::Color4 defaultVal  )  const

const G3D::Color4& G3D::AnyVal::color4 (  )  const

const G3D::CoordinateFrame& G3D::AnyVal::coordinateFrame ( const G3D::CoordinateFrame defaultVal  )  const

const G3D::CoordinateFrame& G3D::AnyVal::coordinateFrame (  )  const

void G3D::AnyVal::deserialize ( G3D::TextInput t  ) 

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

If out of bounds, IndexOutOfBounds is thrown.

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  ) 

const G3D::Matrix3& G3D::AnyVal::matrix3 ( const G3D::Matrix3 defaultVal  )  const

const G3D::Matrix3& G3D::AnyVal::matrix3 (  )  const

const G3D::Matrix4& G3D::AnyVal::matrix4 ( const G3D::Matrix4 defaultVal  )  const

const G3D::Matrix4& G3D::AnyVal::matrix4 (  )  const

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 CoordinateFrame & (  )  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 AnyVal  ) 

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

Table reference.

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

Table reference.

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

Array dereference.

If the index is out of bounds, IndexOutOfBounds is thrown

const G3D::Quat& G3D::AnyVal::quat ( const G3D::Quat defaultVal  )  const

const G3D::Quat& G3D::AnyVal::quat (  )  const

const G3D::Rect2D& G3D::AnyVal::rect2D ( const G3D::Rect2D defaultVal  )  const

const G3D::Rect2D& G3D::AnyVal::rect2D (  )  const

void G3D::AnyVal::save ( const std::string &  filename  )  const

void G3D::AnyVal::serialize ( G3D::TextOutput t  )  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

const G3D::Vector2& G3D::AnyVal::vector2 ( const G3D::Vector2 defaultVal  )  const

const G3D::Vector2& G3D::AnyVal::vector2 (  )  const

const G3D::Vector3& G3D::AnyVal::vector3 ( const G3D::Vector3 defaultVal  )  const

const G3D::Vector3& G3D::AnyVal::vector3 (  )  const

const G3D::Vector4& G3D::AnyVal::vector4 ( const G3D::Vector4 defaultVal  )  const

const G3D::Vector4& G3D::AnyVal::vector4 (  )  const


The documentation for this class was generated from the following file:
Generated on Thu Aug 2 11:40:44 2007 for G3D by doxygen 1.5.2
Hosted by SourceForge.net Logo