Diluculum::LuaValue Class Reference

#include <LuaValue.hpp>

List of all members.

Classes

union  PossibleTypes
 This is used just to know the size of the data_ member. More...

Public Member Functions

 LuaValue ()
 Constructs a LuaValue with a nil value.
 LuaValue (bool b)
 Constructs a LuaValue with boolean type and b value.
 LuaValue (float n)
 Constructs a LuaValue with number type and n value.
 LuaValue (double n)
 Constructs a LuaValue with number type and n value.
 LuaValue (long double n)
 Constructs a LuaValue with number type and n value.
 LuaValue (short n)
 Constructs a LuaValue with number type and n value.
 LuaValue (unsigned short n)
 Constructs a LuaValue with number type and n value.
 LuaValue (int n)
 Constructs a LuaValue with number type and n value.
 LuaValue (unsigned n)
 Constructs a LuaValue with number type and n value.
 LuaValue (long n)
 Constructs a LuaValue with number type and n value.
 LuaValue (unsigned long n)
 Constructs a LuaValue with number type and n value.
 LuaValue (const std::string &s)
 Constructs a LuaValue with string type and s value.
 LuaValue (const char *s)
 Constructs a LuaValue with string type and s value.
 LuaValue (const LuaValueMap &t)
 Constructs a LuaValue with table type and t value.
 LuaValue (lua_CFunction f)
 Constructs a LuaValue with function type and f value.
 LuaValue (const LuaFunction &f)
 Constructs a LuaValue with function type and f value.
 LuaValue (const LuaUserData &ud)
 Constructs a LuaValue with "user data" type and ud value.
 LuaValue (const LuaValueList &v)
 LuaValue (const LuaValue &other)
 Copy constructor.
 ~LuaValue ()
 Destroys the LuaValue, freeing all the resources owned by it.
LuaValueoperator= (const LuaValue &rhs)
 Assignment operator.
const LuaValueListoperator= (const LuaValueList &rhs)
int type () const
std::string typeName () const
lua_Number asNumber () const
lua_Integer asInteger () const
const std::string & asString () const
bool asBoolean () const
LuaValueMap asTable () const
const LuaFunctionasFunction () const
const LuaUserDataasUserData () const
LuaUserDataasUserData ()
template<class T >
asObjectPtr () const
template<class T >
asObjectPtr ()
bool operator< (const LuaValue &rhs) const
bool operator> (const LuaValue &rhs) const
bool operator== (const LuaValue &rhs) const
bool operator!= (const LuaValue &rhs) const
LuaValueoperator[] (const LuaValue &key)
const LuaValueoperator[] (const LuaValue &key) const

Private Member Functions

void destroyObjectAtData ()

Private Attributes

char data_ [sizeof(PossibleTypes)]
int dataType_


Detailed Description

A class that somewhat mimics a Lua value. Notice that a LuaValue is a C++-side thing. There is absolutely no relationship between a LuaValue and a Lua state. This is particularly important for tables and userdata: in Lua, these things are garbage-collectible objects, and variables are just references to them. In Diluculum, a LuaValue represents the value (hence the name!). So, if a LuaValue holds a table, then it contains a collection of keys and values. Similarly, if it holds a userdata, it actually contains a block of memory with some data.

Definition at line 51 of file LuaValue.hpp.


Constructor & Destructor Documentation

Diluculum::LuaValue::LuaValue (  ) 

Constructs a LuaValue with a nil value.

Diluculum::LuaValue::LuaValue ( bool  b  ) 

Constructs a LuaValue with boolean type and b value.

Diluculum::LuaValue::LuaValue ( float  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( double  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( long double  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( short  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( unsigned short  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( int  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( unsigned  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( long  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( unsigned long  n  ) 

Constructs a LuaValue with number type and n value.

Diluculum::LuaValue::LuaValue ( const std::string &  s  ) 

Constructs a LuaValue with string type and s value.

Diluculum::LuaValue::LuaValue ( const char *  s  ) 

Constructs a LuaValue with string type and s value.

Diluculum::LuaValue::LuaValue ( const LuaValueMap t  ) 

Constructs a LuaValue with table type and t value.

Diluculum::LuaValue::LuaValue ( lua_CFunction  f  ) 

Constructs a LuaValue with function type and f value.

Diluculum::LuaValue::LuaValue ( const LuaFunction f  ) 

Constructs a LuaValue with function type and f value.

Diluculum::LuaValue::LuaValue ( const LuaUserData ud  ) 

Constructs a LuaValue with "user data" type and ud value.

Diluculum::LuaValue::LuaValue ( const LuaValueList v  ) 

Constructs a LuaValue from a LuaValueList. The first value on the list is used to initialize the LuaValue. If the LuaValueList is empty, initializes the constructed LuaValue to Nil.

Diluculum::LuaValue::LuaValue ( const LuaValue other  ) 

Copy constructor.

Diluculum::LuaValue::~LuaValue (  )  [inline]

Destroys the LuaValue, freeing all the resources owned by it.

Definition at line 116 of file LuaValue.hpp.

References destroyObjectAtData().


Member Function Documentation

bool Diluculum::LuaValue::asBoolean (  )  const

Return the value as a boolean.

Exceptions:
TypeMismatchError If the value is not a boolean (this is a strict check; no type conversion is performed).

const LuaFunction& Diluculum::LuaValue::asFunction (  )  const

Return the value as a const Lua function.

Exceptions:
TypeMismatchError If the value is not a Lua function. (this is a strict check; no type conversion is performed).

lua_Integer Diluculum::LuaValue::asInteger (  )  const

Return the value as an integer.

Exceptions:
TypeMismatchError If the value is not a number (this is a strict check; no type conversion is performed -- no other than the conversion from lua_Number to lua_Integer, that is).

lua_Number Diluculum::LuaValue::asNumber (  )  const

Return the value as a number.

Exceptions:
TypeMismatchError If the value is not a number (this is a strict check; no type conversion is performed).

template<class T >
T Diluculum::LuaValue::asObjectPtr (  )  [inline]

Assuming that the value stores a C++ object exported to or instantiated in Lua, returns a pointer to the C++ object.

Definition at line 212 of file LuaValue.hpp.

References asUserData(), and Diluculum::LuaUserData::getData().

template<class T >
T Diluculum::LuaValue::asObjectPtr (  )  const [inline]

Assuming that the value stores a C++ object exported to or instantiated in Lua, returns a pointer to the (const) C++ object.

Definition at line 202 of file LuaValue.hpp.

References asUserData(), and Diluculum::LuaUserData::getData().

const std::string& Diluculum::LuaValue::asString (  )  const

Return the value as a string.

Exceptions:
TypeMismatchError If the value is not a string (this is a strict check; no type conversion is performed).

LuaValueMap Diluculum::LuaValue::asTable (  )  const

Returns the value as a table (LuaValueMap).

Note:
Notice that the table is returned by value. You may strongly consider using the subscript operator (that returns a reference) for accessing the values stored in a table-typed LuaValue.
Exceptions:
TypeMismatchError If the value is not a table (this is a strict check; no type conversion is performed).

LuaUserData& Diluculum::LuaValue::asUserData (  ) 

Return the value as a (full) user data.

Note:
Since this is returned as a non-const reference, the LuaUserData::getData() method can be used to get read/write access to the raw user data.
Exceptions:
TypeMismatchError If the value is not a (full) user data (this is a strict check; no type conversion is performed).

const LuaUserData& Diluculum::LuaValue::asUserData (  )  const

Return the value as a const (full) user data.

Exceptions:
TypeMismatchError If the value is not a (full) user data (this is a strict check; no type conversion is performed).

Referenced by asObjectPtr().

void Diluculum::LuaValue::destroyObjectAtData (  )  [private]

Destroys the object allocated at the data_ member, freeing its resources.

Referenced by ~LuaValue().

bool Diluculum::LuaValue::operator!= ( const LuaValue rhs  )  const [inline]

"Different" operator for LuaValues.

Returns:
true if *this and rhs don't have the same value. false otherwise.

Definition at line 261 of file LuaValue.hpp.

bool Diluculum::LuaValue::operator< ( const LuaValue rhs  )  const

"Less than" operator for LuaValues.

Returns:
The order relationship is quite arbitrary for LuaValues, but this has to be defined in order to LuaValueMap work nicely. Anyway, here are the rules used to determine who is less than who:
  • First, typeName() is called for both LuaValues and they are compared with the usual "less than" operator for strings.
  • If both type names are equal, but something different than "nil" and "table", then the values contained in the LuaValues are compared using the "less than" operator for that type.
  • If both type names are "nil", the function returns false.
  • If both type names are "table", then the number of elements in each table are compared. The shorter table is "less than" the larger table.
  • If both tables have the same size, then each entry is recursively compared (that is, using the rules described here). For each entry, the key is compared first, than the value. This is done until finding something "less than" the other thing.
  • If no differences are found, false is obviously returned.

const LuaValueList& Diluculum::LuaValue::operator= ( const LuaValueList rhs  ) 

Assigns a LuaValueList to a LuaValue. The first value on the list is used to initialize the LuaValue. If the LuaValueList is empty, sets the LuaValue to Nil.

LuaValue& Diluculum::LuaValue::operator= ( const LuaValue rhs  ) 

Assignment operator.

bool Diluculum::LuaValue::operator== ( const LuaValue rhs  )  const

"Equal" operator for LuaValues.

Returns:
true if *this and rhs have the same value. false otherwise.

bool Diluculum::LuaValue::operator> ( const LuaValue rhs  )  const

"Greater than" operator for LuaValues.

Returns:
The rules for determining who is greater than who are similar to the ones described in operator<.

const LuaValue& Diluculum::LuaValue::operator[] ( const LuaValue key  )  const

Returns a const reference to a field of this LuaValue (assuming it is a table). If there is no value associated with the key passed as parameter, returns Nil.

Exceptions:
TypeMismatchError If this LuaValue does not hold a table.

LuaValue& Diluculum::LuaValue::operator[] ( const LuaValue key  ) 

Returns a reference to a field of this LuaValue (assuming it is a table). If there is no value associated with the key passed as parameter, inserts a new value (nil) and returns a reference to it.

Exceptions:
TypeMismatchError If this LuaValue does not hold a table.

int Diluculum::LuaValue::type (  )  const [inline]

Returns one of the LUA_T* constants from lua.h, representing the type stored in this LuaValue.

Definition at line 130 of file LuaValue.hpp.

References dataType_.

std::string Diluculum::LuaValue::typeName (  )  const

Returns the type of this LuaValue as a string, just like the Lua built-in function type().

Returns:
One of the following strings: "nil", "boolean", "number", "string", "table", "function", "userdata".


Member Data Documentation

char Diluculum::LuaValue::data_[sizeof(PossibleTypes)] [private]

This stores the actual data of this LuaValue.

Implementation details: This member is large enough to store the largest value. The values are allocated here using placement new, with destructors explicitly called whenever necessary.

Definition at line 302 of file LuaValue.hpp.

The actual type stored in this LuaValue. The values here are the type constants defined by Lua, like LUA_TNUMBER and LUA_TNIL.

Definition at line 307 of file LuaValue.hpp.

Referenced by type().


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

Generated on Fri Jan 15 22:07:24 2010 for Diluculum by  doxygen 1.5.9