Diluculum  1.0
Public Member Functions | Private Member Functions | Private Attributes
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)
LuaValueList call (LuaFunction &func, const LuaValueList &params, const std::string &chunkName="Diluculum chunk")
LuaVariable operator[] (const std::string &variable)
LuaValueMap globals ()
lua_State * getState ()
 Returns the encapsulated lua_State*.

Private Member Functions

LuaValueList doStringOrFile (bool isString, const std::string &str)

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:
loadStdLibIf true (the default), makes all the Lua standard libraries available.
Exceptions:
LuaErrorIf 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:
stateThe lua_State* that will be used by this LuaState.
loadStdLibIf 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:
LuaErrorIf something goes wrong.
virtual Diluculum::LuaState::~LuaState ( ) [virtual]

Destructs a LuaState. If this LuaState owns the underlying lua_State*, lua_close() will be called on it. See the constructors' documentation for details on the lua_State* ownership.


Member Function Documentation

LuaValueList Diluculum::LuaState::call ( LuaFunction func,
const LuaValueList params,
const std::string &  chunkName = "Diluculum chunk" 
)

Calls a given Lua function on this Lua state.

Parameters:
funcThe function to be called.
paramsthe list of parameters to pass to the function.
chunkNameThe string to use as the "chunk name" in the call. This is something added to error messages, in order to make easier to stop where the error was.
Exceptions:
LuaError(or any of its subclasses), if some error is found during the function execution.
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:
fileNameThe file to be executed.
Returns:
All the values returned by the file execution.
Exceptions:
LuaErrorLuaError 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 93 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:
whatThe string to be interpreted.
Returns:
All the values returned by the execution of what.
Exceptions:
LuaErrorLuaError 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 107 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:
isStringIf 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.
strCan 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:
LuaErrorLuaError 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 150 of file LuaState.hpp.

References state_.

LuaValueMap Diluculum::LuaState::globals ( )

Provides access to the table of global variables.

Note:
The returned table will not contain "_G" not "package", because including them would result in tables referencing themselves in a infinitely recursive manner. In Lua, tables are reference types, so this recursion is OK. In Diluculum, tables are value types, so this would result in a crash.
Returns:
The table of global variables in this Lua 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:
variableThe 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.
Trying to access "_G" (the Lua globals table) with LuaState::operator[] will not work (debug builds will assert()). Please use globals() instead.

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 175 of file LuaState.hpp.

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

The underlying lua_State*.

Definition at line 170 of file LuaState.hpp.

Referenced by getState().


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