Lua_icxx  1.02 (Aug 2011)
LuaTempResult Class Reference

Manage the result of a call to a Lua interpreter, still on the Lua stack. More...

#include <LuaTempResult.h>

List of all members.

Classes

class  Item
 Represent one of the items of a LuaTempResult. More...

Public Member Functions

 LuaTempResult (lua_State *, int prevStackTop, int errCode=0, const std::string &errMsgPrefix="")
 only to be called by Lua_icxx classes
bool ok () const
 false if error, true otherwise:
bool isError () const
 true if error, false otherwise
int getErrCode () const
 err code for call (undefined if ok() is true)
std::string getErrMsg () const
 err msg for call (empty if ok() is true)
size_t getNumItems () const
 how many items in result; result.getItem() can take index=0 to result.getNumItems() - 1
bool isNil (int indx=FIRST_ITEM_INDEX) const
 returns true if result getItem at given index (or 0 if none given, ie first getItem) is Lua nil
int getStackPos (int index) const
 Get the position of given item on stack; return 0 (invalid stack pos) if index out of range.
lua_State * getLuaState () const
 Get Lua state used by this result.
void push (int index=FIRST_ITEM_INDEX) const
 Push one of the items on the stack; pushes nil if index out of range or isError() is true.
 LuaTempResult (const LuaTempResult &rhs)
 this is forbidden, but must be made available to compiler
Item getItem (int index=FIRST_ITEM_INDEX) const
 Get specific result getItem; index must be between 1 and numIntems(), ie getItem(1) or (*this)[1] is first getItem; getItem(-1) or (*this)[-1] is last getItem; index < 1 or > getNumItems() is undefined (assertion will fail)
Item operator[] (int index) const
 Get specific result getItem; index must be between 1 and numIntems(), ie getItem(1) or (*this)[1] is first getItem; getItem(-1) or (*this)[-1] is last getItem; index < 1 or > getNumItems() is undefined (assertion will fail)
template<typename TT >
TT getAs (int indx=FIRST_ITEM_INDEX) const
 Convert item to a value such as int, float, etc.
template<typename TT >
 operator TT () const
 Convert item to a value such as int, float, etc.
template<typename TT >
bool operator== (TT val) const
 Compare first getItem of result to another value such as int, float etc.
template<typename TT >
bool operator!= (TT val) const
 Compare first getItem of result to another value such as int, float etc.
template<typename TT >
bool operator>= (TT val) const
 Compare first getItem of result to another value such as int, float etc.
template<typename TT >
bool operator<= (TT val) const
 Compare first getItem of result to another value such as int, float etc.
template<typename TT >
bool operator> (TT val) const
 Compare first getItem of result to another value such as int, float etc.
template<typename TT >
bool operator< (TT val) const
 Compare first getItem of result to another value such as int, float etc.

Static Public Attributes

static const int FIRST_ITEM_INDEX = 1
 getItem indexing starts at this array offset

Detailed Description

Manage the result of a call to a Lua interpreter, still on the Lua stack.

This should only be created by Lua_icxx classes. The result is a tuple of 0 to N items, depending on what the call returned. The result also has a status that indicates whether the call succeeded, and associated error message.

Note that as the name indicates, these are meant to be used as temporary wrappers for values on the Lua stack; they must not be stored as data members (use LuaObjRef or a derived class for this). Local storage is fine:

    LuaTempResult res = lua.eval("...");
    LuaTempResult res2 = lua.eval("...");
    // automatically cleans up stack when goes out of scope

Also, LuaTempResult cannot be reset, because this would mess up the stack:

    LuaTempResult res = lua.eval("..."); // OK, same as res( lua.eval... )
    res = lua.eval("..."); // compilation ERROR!

Definition at line 64 of file LuaTempResult.h.