lua++
Public Member Functions | Static Public Member Functions | Public Attributes | Private Attributes | Static Private Attributes
lua::state Class Reference

Lua wrapper. More...

#include <luapp_state.hpp>

List of all members.

Public Member Functions

 state ()
 Constructor.
 ~state ()
 Destructor.
lua_State * get_state ()
 Return the associated Lua state.
std::string conc_table (const std::string &sTable)
 Concatenates a Lua table into a string.
void copy_table (state &mLua, const std::string &sSrcName, const std::string &sDestName="")
 Copy the content of a table from a Lua state to another.
void do_file (const std::string &sFile)
 Executes a Lua file.
void do_string (const std::string &sStr)
 Executes a string containing Lua instructions.
void call_function (const std::string &sfunctionName)
 Executes a Lua function.
void call_function (const std::string &sfunctionName, const std::vector< var > &lArgumentStack)
 Executes a Lua function with arguments.
void reg (const std::string &sfunctionName, c_function mFunction)
 Binds a C++ function to a Lua function.
template<class T >
void reg ()
 regs a Lunar class to be used on this state.
void print_error (const std::string &sError)
 Prints an error string in the log file with the Lua tag.
std::string format_error (const std::string &sError)
 Formats a raw error string.
void set_lua_error_function (c_function pFunc)
 Sets a custom error formatting function.
void set_print_error_function (print_function pFunc)
 Sets a custom error printing function.
bool is_serializable (int iIndex=-1)
 Checks if a variable is serializable.
std::string serialize_global (const std::string &sName)
 Writes the content of a global variable in a string.
std::string serialize (const std::string &sTab="", int iIndex=-1)
 Writes the content of a variable in a string.
void push_number (const double &dValue)
 Puts a number on the stack.
void push_bool (bool bValue)
 Puts a boolean on the stack.
void push_string (const std::string &sValue)
 Puts a string on the stack.
void push_var (const var &vValue)
 Puts a value on the stack.
void pushValue (int iIndex)
 pushes a copy of the value at the given index on the stack.
void push_nil (uint uiNumber=1)
 Puts "nil" (null) on the stack.
void push_global (const std::string &sName)
 Puts the value of a global Lua variable on the stack.
template<class T >
T * push_new ()
 pushes a new Lunar object on the stack.
void set_global (const std::string &sName)
 Sets the value of a global Lua variable.
void new_table ()
 Creates a new empty table and pushes it on the stack.
bool next (int iIndex=-2)
 Iterates over the table at the given index.
void pop (uint uiNumber=1)
 Removes the value at the top of the stack.
double get_number (int iIndex=-1)
 Returns the value at the given index converted to a number.
bool get_bool (int iIndex=-1)
 Returns the value at the given index converted to a bool.
std::string get_string (int iIndex=-1)
 Returns the value at the given index converted to a string.
var get_value (int iIndex=-1)
 Returns the value at the given index.
template<class T >
T * get (int iIndex=-1) const
 Returns the Lunar object at the given index.
uint get_top ()
 Returns the number of value on the stack.
type get_type (int iIndex=-1)
 Returns the type of the value on the stack.
std::string get_type_name (type mType)
 Returns the name of a type.
void get_global (const std::string &sName)
 Puts a global variable on the stack.
int get_global_int (const std::string &sName, bool bCritical=true, int iDefaultValue=0)
 Reads an int from Lua.
double get_global_double (const std::string &sName, bool bCritical=true, double dDefaultValue=0.0)
 Reads a float from Lua.
std::string get_global_string (const std::string &sName, bool bCritical=true, const std::string &sDefaultValue="")
 Wrapper to read a string from Lua.
bool get_global_bool (const std::string &sName, bool bCritical=true, bool bDefaultValue=false)
 Reads a bool from Lua.
void get_field (const std::string &sName, int iIndex=-1)
 Puts a value from a Lua table on the stack.
void get_field (int iID, int iIndex=-1)
 Puts a value from a Lua table on the stack.
int get_field_int (const std::string &sName, bool bCritical=true, int iDefaultValue=0, bool bSetValue=false)
 Reads an int from a Lua table.
double get_field_double (const std::string &sName, bool bCritical=true, double dDefaultValue=0.0, bool bSetValue=false)
 Reads a float from a Lua table.
std::string get_field_string (const std::string &sName, bool bCritical=true, const std::string &sDefaultValue="", bool bSetValue=false)
 Reads a string from a Lua table.
bool get_field_bool (const std::string &sName, bool bCritical=true, bool bDefaultValue=false, bool bSetValue=false)
 Reads a bool from a Lua table.
void set_field (const std::string &sName)
 Writes a value into a Lua table.
void set_field (int iID)
 Writes a value into a Lua table.
void set_field_int (const std::string &sName, int iValue)
 Writes an int into a Lua table.
void set_field_double (const std::string &sName, double dValue)
 Writes a float into a Lua table.
void set_field_string (const std::string &sName, const std::string &sValue)
 Writes a string into a Lua table.
void set_field_bool (const std::string &sName, bool bValue)
 Writes a bool into a Lua table.
void set_field_int (int iID, int iValue)
 Writes an int into a Lua table.
void set_field_double (int iID, double dValue)
 Writes a float into a Lua table.
void set_field_string (int iID, const std::string &sValue)
 Writes a string into a Lua table.
void set_field_bool (int iID, bool bValue)
 Writes a bool into a Lua table.
void set_top (uint uiSize)
 Changes the stack size.

Static Public Member Functions

static stateget_state (lua_State *pLua)

Public Attributes

std::string sComString

Private Attributes

lua_State * pLua_
c_function pErrorFunction_
print_function pPrintFunction_

Static Private Attributes

static std::map< lua_State
*, state * > 
lLuaStateMap_

Detailed Description

Lua wrapper.

Wraps the Lua api into a single class.

Definition at line 33 of file luapp_state.hpp.


Constructor & Destructor Documentation

Constructor.

Note:
Opens a new Lua state and registers all base functions.

Definition at line 42 of file luapp_state.cpp.

Destructor.

Note:
Definately closes the associated Lua state (all data will be lost).

Definition at line 62 of file luapp_state.cpp.


Member Function Documentation

void lua::state::call_function ( const std::string &  sfunctionName)

Executes a Lua function.

Parameters:
sfunctionNameThe name of the function to execute
Note:
This function wil throw an Exception if any error occurs. Don't forget to catch them.

Definition at line 351 of file luapp_state.cpp.

void lua::state::call_function ( const std::string &  sfunctionName,
const std::vector< var > &  lArgumentStack 
)

Executes a Lua function with arguments.

Parameters:
sfunctionNameThe name of the function to execute
lArgumentStackThe agument stack (order matters)
Note:
This function wil throw an Exception if any error occurs. Don't forget to catch them.

Definition at line 396 of file luapp_state.cpp.

std::string lua::state::conc_table ( const std::string &  sTable)

Concatenates a Lua table into a string.

Parameters:
sTableThe name of the table in Lua

Definition at line 84 of file luapp_state.cpp.

void lua::state::copy_table ( state mLua,
const std::string &  sSrcName,
const std::string &  sDestName = "" 
)

Copy the content of a table from a Lua state to another.

Parameters:
mLuaThe other Lua state into wich the table will be copied
sSrcNameThe name of the table in the source state
sDestNameThe name of the table in the destination state
Note:
If sDestName is ommited, the table will have the same name in both Lua states.

Definition at line 111 of file luapp_state.cpp.

void lua::state::do_file ( const std::string &  sFile)

Executes a Lua file.

Parameters:
sFileThe name of the file
Note:
This function wil throw an Exception if any error occurs. Don't forget to catch them.

Definition at line 251 of file luapp_state.cpp.

void lua::state::do_string ( const std::string &  sStr)

Executes a string containing Lua instructions.

Parameters:
sStrThe string to execute
Note:
This function wil throw an Exception if any error occurs. Don't forget to catch them.

Definition at line 306 of file luapp_state.cpp.

std::string lua::state::format_error ( const std::string &  sError)

Formats a raw error string.

Parameters:
sErrorThe raw error string
Note:
Calls the custom error function if provided, else formats as :
[source]:line: error

Definition at line 449 of file luapp_state.cpp.

template<class T >
T* lua::state::get ( int  iIndex = -1) const [inline]

Returns the Lunar object at the given index.

Parameters:
iIndexThe index at which to search for the value
Returns:
The Lunar object at the given index

Definition at line 252 of file luapp_state.hpp.

bool lua::state::get_bool ( int  iIndex = -1)

Returns the value at the given index converted to a bool.

Parameters:
iIndexThe index at which to search for the value
Returns:
The value at the given index converted to a bool

Definition at line 731 of file luapp_state.cpp.

void lua::state::get_field ( const std::string &  sName,
int  iIndex = -1 
)

Puts a value from a Lua table on the stack.

Parameters:
sNameThe name of the key associated to the value
iIndexThe position of the table in the stack
Note:
Puts 'nil' if the key couldn't be found.

Definition at line 986 of file luapp_state.cpp.

void lua::state::get_field ( int  iID,
int  iIndex = -1 
)

Puts a value from a Lua table on the stack.

Parameters:
iIDThe id of the key associated to the value
iIndexThe position of the table in the stack
Note:
Puts 'nil' if the key couldn't be found.

Definition at line 997 of file luapp_state.cpp.

bool lua::state::get_field_bool ( const std::string &  sName,
bool  bCritical = true,
bool  bDefaultValue = false,
bool  bSetValue = false 
)

Reads a bool from a Lua table.

Parameters:
sNameThe name of the key associated to the value
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
bDefaultValueThe default value
bSetValueIf 'true' and the key wasn't found in the table, this function will create that key in the Lua table and assign it its default value
Returns:
The bool value
Note:
The table that will be used to read the value should be at the top of the stack just before you call that function.

Definition at line 1124 of file luapp_state.cpp.

double lua::state::get_field_double ( const std::string &  sName,
bool  bCritical = true,
double  dDefaultValue = 0.0,
bool  bSetValue = false 
)

Reads a float from a Lua table.

Parameters:
sNameThe name of the key associated to the value
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
dDefaultValueThe default value
bSetValueIf 'true' and the key wasn't found in the table, this function will create that key in the Lua table and assign it its default value
Returns:
The float value
Note:
The table that will be used to read the value should be at the top of the stack just before you call that function.

Definition at line 1049 of file luapp_state.cpp.

int lua::state::get_field_int ( const std::string &  sName,
bool  bCritical = true,
int  iDefaultValue = 0,
bool  bSetValue = false 
)

Reads an int from a Lua table.

Parameters:
sNameThe name of the key associated to the value
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
iDefaultValueThe default value
bSetValueIf 'true' and the key wasn't found in the table, this function will create that key in the Lua table and assign it its default value
Returns:
The int value
Note:
The table that will be used to read the value should be at the top of the stack just before you call that function.

Definition at line 1012 of file luapp_state.cpp.

std::string lua::state::get_field_string ( const std::string &  sName,
bool  bCritical = true,
const std::string &  sDefaultValue = "",
bool  bSetValue = false 
)

Reads a string from a Lua table.

Parameters:
sNameThe name of the key associated to the value
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
sDefaultValueThe default value
bSetValueIf 'true' and the key wasn't found in the table, this function will create that key in the Lua table and assign it its default value
Returns:
The string value
Note:
The table that will be used to read the value should be at the top of the stack just before you call that function.

Definition at line 1087 of file luapp_state.cpp.

void lua::state::get_global ( const std::string &  sName)

Puts a global variable on the stack.

Parameters:
sNameThe name of the global variable
Note:
The name can contain tables, for example : "MyTable.MyVariable" is a valid input.

Definition at line 795 of file luapp_state.cpp.

bool lua::state::get_global_bool ( const std::string &  sName,
bool  bCritical = true,
bool  bDefaultValue = false 
)

Reads a bool from Lua.

Parameters:
sNameThe name of the variable (global scope)
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
bDefaultValueThe default value
Returns:
The bool value

Definition at line 951 of file luapp_state.cpp.

double lua::state::get_global_double ( const std::string &  sName,
bool  bCritical = true,
double  dDefaultValue = 0.0 
)

Reads a float from Lua.

Parameters:
sNameThe name of the variable (global scope)
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
dDefaultValueThe default value
Returns:
The float value

Definition at line 881 of file luapp_state.cpp.

int lua::state::get_global_int ( const std::string &  sName,
bool  bCritical = true,
int  iDefaultValue = 0 
)

Reads an int from Lua.

Parameters:
sNameThe name of the variable (global scope)
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
iDefaultValueThe default value
Returns:
The int value

Definition at line 846 of file luapp_state.cpp.

std::string lua::state::get_global_string ( const std::string &  sName,
bool  bCritical = true,
const std::string &  sDefaultValue = "" 
)

Wrapper to read a string from Lua.

Parameters:
sNameThe name of the variable (global scope)
bCriticalIf 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value
sDefaultValueThe default value
Returns:
The string value

Definition at line 916 of file luapp_state.cpp.

double lua::state::get_number ( int  iIndex = -1)

Returns the value at the given index converted to a number.

Parameters:
iIndexThe index at which to search for the value
Returns:
The value at the given index converted to a number

Definition at line 726 of file luapp_state.cpp.

lua_State * lua::state::get_state ( )

Return the associated Lua state.

Returns:
The associated Lua state

Definition at line 79 of file luapp_state.cpp.

state * lua::state::get_state ( lua_State *  pLua) [static]

Definition at line 70 of file luapp_state.cpp.

std::string lua::state::get_string ( int  iIndex = -1)

Returns the value at the given index converted to a string.

Parameters:
iIndexThe index at which to search for the value
Returns:
The value at the given index converted to a string

Definition at line 736 of file luapp_state.cpp.

Returns the number of value on the stack.

Returns:
The number of value on the stack

Definition at line 753 of file luapp_state.cpp.

type lua::state::get_type ( int  iIndex = -1)

Returns the type of the value on the stack.

Parameters:
iIndexThe index of the value to analyse
Returns:
The type

Definition at line 758 of file luapp_state.cpp.

std::string lua::state::get_type_name ( type  mType)

Returns the name of a type.

Parameters:
mTypeThe type to serialize
Returns:
The name of the type

Definition at line 777 of file luapp_state.cpp.

var lua::state::get_value ( int  iIndex = -1)

Returns the value at the given index.

Parameters:
iIndexThe index at which to search for the value
Returns:
The value at the given index

Definition at line 741 of file luapp_state.cpp.

bool lua::state::is_serializable ( int  iIndex = -1)

Checks if a variable is serializable.

Parameters:
iIndexThe index on the stack of the variable
Returns:
'true' for strings, numbers, booleans and tables.

Definition at line 480 of file luapp_state.cpp.

Creates a new empty table and pushes it on the stack.

Definition at line 692 of file luapp_state.cpp.

bool lua::state::next ( int  iIndex = -2)

Iterates over the table at the given index.

Parameters:
iIndexThe index of the table to iterate on
Note:
Typical loop (with table at index i) :
for (pLua->push_nil(); pLua->next(i); pLua->pop())

Definition at line 703 of file luapp_state.cpp.

void lua::state::pop ( uint  uiNumber = 1)

Removes the value at the top of the stack.

Parameters:
uiNumberThe number of value to remove

Definition at line 715 of file luapp_state.cpp.

void lua::state::print_error ( const std::string &  sError)

Prints an error string in the log file with the Lua tag.

Parameters:
sErrorThe error string to output
Note:
This function will print out the current file and line

Definition at line 459 of file luapp_state.cpp.

void lua::state::push_bool ( bool  bValue)

Puts a boolean on the stack.

Parameters:
bValueThe value to push on the stack

Definition at line 595 of file luapp_state.cpp.

void lua::state::push_global ( const std::string &  sName)

Puts the value of a global Lua variable on the stack.

Parameters:
sNameThe name of this variable

Definition at line 628 of file luapp_state.cpp.

template<class T >
T* lua::state::push_new ( ) [inline]

pushes a new Lunar object on the stack.

Returns:
A pointer to the Lunar object

Definition at line 196 of file luapp_state.hpp.

void lua::state::push_nil ( uint  uiNumber = 1)

Puts "nil" (null) on the stack.

Parameters:
uiNumberThe number of "nil" to push

Definition at line 605 of file luapp_state.cpp.

void lua::state::push_number ( const double &  dValue)

Puts a number on the stack.

Parameters:
dValueThe value to push on the stack (converted to float)

Definition at line 590 of file luapp_state.cpp.

void lua::state::push_string ( const std::string &  sValue)

Puts a string on the stack.

Parameters:
sValueThe value to push on the stack

Definition at line 600 of file luapp_state.cpp.

void lua::state::push_var ( const var vValue)

Puts a value on the stack.

Parameters:
vValueThe value to push on the stack

Definition at line 611 of file luapp_state.cpp.

void lua::state::pushValue ( int  iIndex)

pushes a copy of the value at the given index on the stack.

Parameters:
iIndexThe index of the value to push

Definition at line 623 of file luapp_state.cpp.

void lua::state::reg ( const std::string &  sfunctionName,
c_function  mFunction 
)

Binds a C++ function to a Lua function.

Parameters:
sfunctionNameThe name of the Lua function
mFunctionThe C++ function to bind

Definition at line 444 of file luapp_state.cpp.

template<class T >
void lua::state::reg ( ) [inline]

regs a Lunar class to be used on this state.

Definition at line 102 of file luapp_state.hpp.

std::string lua::state::serialize ( const std::string &  sTab = "",
int  iIndex = -1 
)

Writes the content of a variable in a string.

Parameters:
sTabNumber of space to put in front of each line
iIndexThe index on the stack of the variable
Note:
Can only serialize strings, numbers, booleans and tables.
Returns:
The content of the variable

Definition at line 512 of file luapp_state.cpp.

std::string lua::state::serialize_global ( const std::string &  sName)

Writes the content of a global variable in a string.

Parameters:
sNameThe name of the global variable
Returns:
The content of the variable

Definition at line 499 of file luapp_state.cpp.

void lua::state::set_field ( const std::string &  sName)

Writes a value into a Lua table.

Parameters:
sNameThe name of the key associated to the value
Note:
The value to put into the table must be at the top of the stack.
The table must be at the index just before the value.
pops the value from the stack.

Definition at line 1161 of file luapp_state.cpp.

void lua::state::set_field ( int  iID)

Writes a value into a Lua table.

Parameters:
iIDThe ID of the key associated to the value
Note:
The value to put into the table must be at the top of the stack.
The table must be at the index just before the value.
pops the value from the stack.

Definition at line 1175 of file luapp_state.cpp.

void lua::state::set_field_bool ( const std::string &  sName,
bool  bValue 
)

Writes a bool into a Lua table.

Parameters:
sNameThe name of the key associated to the value
bValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1228 of file luapp_state.cpp.

void lua::state::set_field_bool ( int  iID,
bool  bValue 
)

Writes a bool into a Lua table.

Parameters:
iIDThe ID of the key associated to the value
bValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1280 of file luapp_state.cpp.

void lua::state::set_field_double ( const std::string &  sName,
double  dValue 
)

Writes a float into a Lua table.

Parameters:
sNameThe name of the key associated to the value
dValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1202 of file luapp_state.cpp.

void lua::state::set_field_double ( int  iID,
double  dValue 
)

Writes a float into a Lua table.

Parameters:
iIDThe ID of the key associated to the value
dValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1254 of file luapp_state.cpp.

void lua::state::set_field_int ( const std::string &  sName,
int  iValue 
)

Writes an int into a Lua table.

Parameters:
sNameThe name of the key associated to the value
iValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1189 of file luapp_state.cpp.

void lua::state::set_field_int ( int  iID,
int  iValue 
)

Writes an int into a Lua table.

Parameters:
iIDThe ID of the key associated to the value
iValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1241 of file luapp_state.cpp.

void lua::state::set_field_string ( const std::string &  sName,
const std::string &  sValue 
)

Writes a string into a Lua table.

Parameters:
sNameThe name of the key associated to the value
sValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1215 of file luapp_state.cpp.

void lua::state::set_field_string ( int  iID,
const std::string &  sValue 
)

Writes a string into a Lua table.

Parameters:
iIDThe ID of the key associated to the value
sValueThe value to set
Note:
The table that will be used to write the value should be at the top of the stack just before you call that function.

Definition at line 1267 of file luapp_state.cpp.

void lua::state::set_global ( const std::string &  sName)

Sets the value of a global Lua variable.

Parameters:
sNameThe name of this variable
Note:
The value taken is taken at the top of the stack, and popped.

Definition at line 633 of file luapp_state.cpp.

Sets a custom error formatting function.

Parameters:
pFuncThe error function
Note:
The string that is passed to this error function only contains the raw error message from Lua : no indication on the location of the error.
The default implementation of this function returns :
[source]:line: error
See format_error().

Definition at line 464 of file luapp_state.cpp.

Sets a custom error printing function.

Parameters:
pFuncThe error printing function

Definition at line 472 of file luapp_state.cpp.

void lua::state::set_top ( uint  uiSize)

Changes the stack size.

Parameters:
uiSizeThe new size of the stack
Note:
If the stack has more elements, they will be erased.
If is has less, the stack will be filled with nil.

Definition at line 1293 of file luapp_state.cpp.


Member Data Documentation

std::map< lua_State *, state * > lua::state::lLuaStateMap_ [static, private]

Definition at line 488 of file luapp_state.hpp.

Definition at line 492 of file luapp_state.hpp.

lua_State* lua::state::pLua_ [private]

Definition at line 490 of file luapp_state.hpp.

Definition at line 493 of file luapp_state.hpp.

Definition at line 482 of file luapp_state.hpp.


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