Diluculum::LuaState Class Reference

#include <LuaState.hpp>

List of all members.

Public Member Functions

 LuaState (bool loadStdLib=true)
 LuaState (lua_State *state, bool loadStdLib=false)
virtual ~LuaState ()
LuaValueList doFile (const std::string &fileName)
LuaValueList doString (const std::string &what)
LuaVariable operator[] (const std::string &variable)
lua_State * getState ()
 Returns the encapsulated lua_State*.

Private Member Functions

LuaValueList doStringOrFile (bool isString, const std::string &str)
void throwOnLuaError (int retCode)

Private Attributes

lua_State * state_
 The underlying lua_State*.
const bool ownsState_


Detailed Description

LuaState: The Next Generation. The pleasant way to do perform relevant operations on a Lua state.

(My previous implementation of a class named LuaState was pretty low-level. It was essentially a C++ wrapper around a lua_State*, without higher level operations. This is an attempt to allow me to do the kind of things I do most of the time without much effort.)

Definition at line 50 of file LuaState.hpp.


Constructor & Destructor Documentation

Diluculum::LuaState::LuaState bool  loadStdLib = true  )  [explicit]
 

Constructs a LuaState that owns a lua_State*. In other words, this will create the underlying Lua state on construction and destroy it when this LuaState is destroyed.

Parameters:
loadStdLib If true (the default), makes all the Lua standard libraries available.
Exceptions:
LuaError If something goes wrong.

Diluculum::LuaState::LuaState lua_State *  state,
bool  loadStdLib = false
[explicit]
 

Constructs a LuaState that doesn't own the underlying Lua state. In other words, this LuaState will use a user-supplied lua_State* and its destructor will not lua_close() it.

Parameters:
state The lua_State* that will be used by this LuaState.
loadStdLib If true, makes all the Lua standard libraries available (default is false, because it is very likely that the libraries are already opened in the Lua state passed as the first parameter).
Exceptions:
LuaError If something goes wrong.

virtual Diluculum::LuaState::~LuaState  )  [virtual]
 

Destructs a LuaState. This calls lua_close() on the underlying lua_State*.


Member Function Documentation

LuaValueList Diluculum::LuaState::doFile const std::string &  fileName  )  [inline]
 

Executes the file passed as parameter and returns all the values returned by this execution. Notice that when a LuaValueList is assigned to a LuaValue, just the first value in the list is assigned, so this is handy for situations when only the first return value is desired.

Parameters:
fileName The file to be executed.
Returns:
All the values returned by the file execution.
Exceptions:
LuaError LuaError or any of its subclasses can be thrown. In particular, LuaTypeError will be thrown if the execution returns a type not supported by LuaType.

Definition at line 91 of file LuaState.hpp.

References doStringOrFile().

LuaValueList Diluculum::LuaState::doString const std::string &  what  )  [inline]
 

Executes the string passed as parameter and returns all the values returned by this execution. Notice that when a LuaValueList is assigned to a LuaValue, just the first value in the list is assigned, so this is handy for situations when only the first return value is desired.

Parameters:
what The string to be interpreted.
Returns:
All the values returned by the execution of what.
Exceptions:
LuaError LuaError or any of its subclasses can be thrown. In particular, LuaTypeError will be thrown if the execution returns a type not supported by LuaType.

Definition at line 105 of file LuaState.hpp.

References doStringOrFile().

LuaValueList Diluculum::LuaState::doStringOrFile bool  isString,
const std::string &  str
[private]
 

Since The implementation of doString and doFile() are quite similar, it looked like a good idea to use the same function to implement both at a lower level. This is it.

Parameters:
isString If true, means that it is desired to execute the contents of a string. If false, means that it is desired to execute the contents of a file.
str Can be either a string of Lua code to be executed or a file with Lua code to be executed. The exact interpretation of this parameter depends on the first parameter, isString.
Exceptions:
LuaError LuaError or any of its subclasses can be thrown. In particular, LuaTypeError will be thrown if the execution returns a type not supported by LuaType.

Referenced by doFile(), and doString().

lua_State* Diluculum::LuaState::getState  )  [inline]
 

Returns the encapsulated lua_State*.

Definition at line 121 of file LuaState.hpp.

References state_.

LuaVariable Diluculum::LuaState::operator[] const std::string &  variable  ) 
 

Returns a LuaVariable representing the global variable named variable. Since the returned value also has a subscript operator, this is a handy way to access variables stored in tables. Also, since the LuaVariable has read/write support, this can be used even to modify the variables stored in the LuaState.

Parameters:
variable The name of the global variable to be accessed.
Returns:
The global variable named variable. If no such variable exists, returns a variable containing Nil.
Note:
This operator does not throw exceptions.

void Diluculum::LuaState::throwOnLuaError int  retCode  )  [private]
 

Throws an exception if the number passed as parameter corresponds to an error code from a function from the Lua API. The exception thrown is of the proper type, that is, of the subclass of LuaError that best describes the error. Furthermore, the string associated with the exception (the what parameter) is set to whatever Lua returned as the error message.

If the number passed as parameter is 0 (which is the code for "no error"), the function does nothing.

Parameters:
retCode The return code for some Lua API function; that's the value we want to check for "errorness".
Exceptions:
LuaRunTimeError If retCode == LUA_ERRRUN.
LuaFileError If retCode == LUA_ERRFILE.
LuaRunTimeError If retCode == LUA_ERRRUN.
LuaSyntaxError If retCode == LUA_ERRSYNTAX.
LuaMemoryError If retCode == LUA_ERRMEM.
LuaError If retCode is not a recognized Lua error code.


Member Data Documentation

const bool Diluculum::LuaState::ownsState_ [private]
 

Does this LuaState owns state_? (This used by the destructor to decide whether it has to lua_close() it or not.)

Definition at line 146 of file LuaState.hpp.

lua_State* Diluculum::LuaState::state_ [private]
 

The underlying lua_State*.

Definition at line 141 of file LuaState.hpp.

Referenced by getState().


The documentation for this class was generated from the following file:
Generated on Tue Feb 6 09:30:26 2007 for Diluculum by  doxygen 1.4.6