Lua wrapper. More...
#include <luapp_state.hpp>
Public Member Functions | |
state () | |
Constructor. | |
~state () | |
Destructor. | |
lua_State * | get_state () |
Return the associated Lua state. | |
std::string | table_to_string (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 one 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. | |
c_function | get_lua_error_function () const |
Returns the current 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 (const var &vValue) |
Puts a value on the stack. | |
void | push_value (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 > | |
void | push_userdata (T *pData) |
Puts a user data (C++ pointer) 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_userdata (int iIndex=-1) |
Returns the user data (C++ pointer) 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 state * | get_state (lua_State *pLua) |
Public Attributes | |
std::string | sComString |
Private Member Functions | |
state (const state &) | |
state & | operator= (const state &) |
Private Attributes | |
lua_State * | pLua_ |
c_function | pErrorFunction_ |
print_function | pPrintFunction_ |
Static Private Attributes | |
static std::map< lua_State *, state * > | lLuaStateMap_ |
Lua wrapper.
Wraps the Lua api into a single class.
Definition at line 33 of file luapp_state.hpp.
lua::state::state | ( | ) |
Constructor.
Definition at line 39 of file luapp_state.cpp.
lua::state::~state | ( | ) |
Destructor.
Definition at line 57 of file luapp_state.cpp.
lua::state::state | ( | const state & | ) | [private] |
void lua::state::call_function | ( | const std::string & | sFunctionName, | |
const std::vector< var > & | lArgumentStack | |||
) |
void lua::state::call_function | ( | const std::string & | sFunctionName | ) |
void lua::state::copy_table | ( | state & | mLua, | |
const std::string & | sSrcName, | |||
const std::string & | sDestName = "" | |||
) |
Copy the content of a table from one Lua state to another.
mLua | The other Lua state into wich the table will be copied | |
sSrcName | The name of the table in the source state | |
sDestName | The name of the table in the destination state |
Definition at line 106 of file luapp_state.cpp.
void lua::state::do_file | ( | const std::string & | sFile | ) |
Executes a Lua file.
sFile | The name of the file |
Definition at line 240 of file luapp_state.cpp.
void lua::state::do_string | ( | const std::string & | sStr | ) |
Executes a string containing Lua instructions.
sStr | The string to execute |
Definition at line 287 of file luapp_state.cpp.
std::string lua::state::format_error | ( | const std::string & | sError | ) |
Formats a raw error string.
sError | The raw error string |
Definition at line 460 of file luapp_state.cpp.
T* lua::state::get | ( | int | iIndex = -1 |
) | const [inline] |
Returns the Lunar object at the given index.
iIndex | The index at which to search for the value |
Definition at line 278 of file luapp_state.hpp.
bool lua::state::get_bool | ( | int | iIndex = -1 |
) |
Returns the value at the given index converted to a bool.
iIndex | The index at which to search for the value |
Definition at line 717 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.
iID | The id of the key associated to the value | |
iIndex | The position of the table in the stack |
Definition at line 940 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.
sName | The name of the key associated to the value | |
iIndex | The position of the table in the stack |
Definition at line 935 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.
sName | The name of the key associated to the value | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
bDefaultValue | The default value | |
bSetValue | If '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 |
Definition at line 1039 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.
sName | The name of the key associated to the value | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
dDefaultValue | The default value | |
bSetValue | If '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 |
Definition at line 979 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.
sName | The name of the key associated to the value | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
iDefaultValue | The default value | |
bSetValue | If '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 |
Definition at line 949 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.
sName | The name of the key associated to the value | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
sDefaultValue | The default value | |
bSetValue | If '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 |
Definition at line 1009 of file luapp_state.cpp.
void lua::state::get_global | ( | const std::string & | sName | ) |
Puts a global variable on the stack.
sName | The name of the global variable |
Definition at line 781 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.
sName | The name of the variable (global scope) | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
bDefaultValue | The default value |
Definition at line 907 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.
sName | The name of the variable (global scope) | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
dDefaultValue | The default value |
Definition at line 851 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.
sName | The name of the variable (global scope) | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
iDefaultValue | The default value |
Definition at line 823 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.
sName | The name of the variable (global scope) | |
bCritical | If 'true', an error will be printed if the variable is not found. Else, it will be assigned its default value | |
sDefaultValue | The default value |
Definition at line 879 of file luapp_state.cpp.
c_function lua::state::get_lua_error_function | ( | ) | const |
Returns the current error formatting function.
Definition at line 483 of file luapp_state.cpp.
double lua::state::get_number | ( | int | iIndex = -1 |
) |
Returns the value at the given index converted to a number.
iIndex | The index at which to search for the value |
Definition at line 712 of file luapp_state.cpp.
state * lua::state::get_state | ( | lua_State * | pLua | ) | [static] |
Definition at line 65 of file luapp_state.cpp.
lua_State * lua::state::get_state | ( | ) |
Return the associated Lua state.
Definition at line 74 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.
iIndex | The index at which to search for the value |
Definition at line 722 of file luapp_state.cpp.
uint lua::state::get_top | ( | ) |
Returns the number of value on the stack.
Definition at line 739 of file luapp_state.cpp.
type lua::state::get_type | ( | int | iIndex = -1 |
) |
Returns the type of the value on the stack.
iIndex | The index of the value to analyse |
Definition at line 744 of file luapp_state.cpp.
std::string lua::state::get_type_name | ( | type | mType | ) |
Returns the name of a type.
mType | The type to serialize |
Definition at line 763 of file luapp_state.cpp.
T* lua::state::get_userdata | ( | int | iIndex = -1 |
) | [inline] |
Returns the user data (C++ pointer) at the given index.
Definition at line 268 of file luapp_state.hpp.
var lua::state::get_value | ( | int | iIndex = -1 |
) |
Returns the value at the given index.
iIndex | The index at which to search for the value |
Definition at line 727 of file luapp_state.cpp.
bool lua::state::is_serializable | ( | int | iIndex = -1 |
) |
Checks if a variable is serializable.
iIndex | The index on the stack of the variable |
Definition at line 496 of file luapp_state.cpp.
void lua::state::new_table | ( | ) |
Creates a new empty table and pushes it on the stack.
Definition at line 696 of file luapp_state.cpp.
bool lua::state::next | ( | int | iIndex = -2 |
) |
Iterates over the table at the given index.
iIndex | The index of the table to iterate on |
Definition at line 701 of file luapp_state.cpp.
void lua::state::pop | ( | uint | uiNumber = 1 |
) |
Removes the value at the top of the stack.
uiNumber | The number of value to remove |
Definition at line 707 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.
sError | The error string to output |
Definition at line 470 of file luapp_state.cpp.
void lua::state::push | ( | const var & | vValue | ) |
Puts a value on the stack.
vValue | The value to push on the stack |
Definition at line 623 of file luapp_state.cpp.
void lua::state::push_bool | ( | bool | bValue | ) |
Puts a boolean on the stack.
bValue | The value to push on the stack |
Definition at line 607 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.
sName | The name of this variable |
Definition at line 640 of file luapp_state.cpp.
T* lua::state::push_new | ( | ) | [inline] |
pushes a new Lunar object on the stack.
Definition at line 215 of file luapp_state.hpp.
void lua::state::push_nil | ( | uint | uiNumber = 1 |
) |
Puts "nil" (null) on the stack.
uiNumber | The number of "nil" to push |
Definition at line 617 of file luapp_state.cpp.
void lua::state::push_number | ( | const double & | dValue | ) |
Puts a number on the stack.
dValue | The value to push on the stack (converted to float) |
Definition at line 602 of file luapp_state.cpp.
void lua::state::push_string | ( | const std::string & | sValue | ) |
Puts a string on the stack.
sValue | The value to push on the stack |
Definition at line 612 of file luapp_state.cpp.
void lua::state::push_userdata | ( | T * | pData | ) | [inline] |
Puts a user data (C++ pointer) on the stack.
pData | The pointer to put on the stack |
Definition at line 206 of file luapp_state.hpp.
void lua::state::push_value | ( | int | iIndex | ) |
pushes a copy of the value at the given index on the stack.
iIndex | The index of the value to push |
Definition at line 635 of file luapp_state.cpp.
void lua::state::reg | ( | ) | [inline] |
regs a Lunar class to be used on this state.
Definition at line 102 of file luapp_state.hpp.
void lua::state::reg | ( | const std::string & | sFunctionName, | |
c_function | mFunction | |||
) |
std::string lua::state::serialize | ( | const std::string & | sTab = "" , |
|
int | iIndex = -1 | |||
) |
Writes the content of a variable in a string.
sTab | Number of space to put in front of each line | |
iIndex | The index on the stack of the variable |
Definition at line 529 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.
sName | The name of the global variable |
Definition at line 516 of file luapp_state.cpp.
void lua::state::set_field | ( | int | iID | ) |
Writes a value into a Lua table.
iID | The ID of the key associated to the value |
Definition at line 1077 of file luapp_state.cpp.
void lua::state::set_field | ( | const std::string & | sName | ) |
Writes a value into a Lua table.
sName | The name of the key associated to the value |
Definition at line 1069 of file luapp_state.cpp.
void lua::state::set_field_bool | ( | int | iID, | |
bool | bValue | |||
) |
Writes a bool into a Lua table.
iID | The ID of the key associated to the value | |
bValue | The value to set |
Definition at line 1134 of file luapp_state.cpp.
void lua::state::set_field_bool | ( | const std::string & | sName, | |
bool | bValue | |||
) |
Writes a bool into a Lua table.
sName | The name of the key associated to the value | |
bValue | The value to set |
Definition at line 1106 of file luapp_state.cpp.
void lua::state::set_field_double | ( | int | iID, | |
double | dValue | |||
) |
Writes a float into a Lua table.
iID | The ID of the key associated to the value | |
dValue | The value to set |
Definition at line 1120 of file luapp_state.cpp.
void lua::state::set_field_double | ( | const std::string & | sName, | |
double | dValue | |||
) |
Writes a float into a Lua table.
sName | The name of the key associated to the value | |
dValue | The value to set |
Definition at line 1092 of file luapp_state.cpp.
void lua::state::set_field_int | ( | int | iID, | |
int | iValue | |||
) |
Writes an int into a Lua table.
iID | The ID of the key associated to the value | |
iValue | The value to set |
Definition at line 1113 of file luapp_state.cpp.
void lua::state::set_field_int | ( | const std::string & | sName, | |
int | iValue | |||
) |
Writes an int into a Lua table.
sName | The name of the key associated to the value | |
iValue | The value to set |
Definition at line 1085 of file luapp_state.cpp.
void lua::state::set_field_string | ( | int | iID, | |
const std::string & | sValue | |||
) |
Writes a string into a Lua table.
iID | The ID of the key associated to the value | |
sValue | The value to set |
Definition at line 1127 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.
sName | The name of the key associated to the value | |
sValue | The value to set |
Definition at line 1099 of file luapp_state.cpp.
void lua::state::set_global | ( | const std::string & | sName | ) |
Sets the value of a global Lua variable.
sName | The name of this variable |
Definition at line 645 of file luapp_state.cpp.
void lua::state::set_lua_error_function | ( | c_function | pFunc | ) |
Sets a custom error formatting function.
pFunc | The error function |
Definition at line 475 of file luapp_state.cpp.
void lua::state::set_print_error_function | ( | print_function | pFunc | ) |
Sets a custom error printing function.
pFunc | The error printing function |
Definition at line 488 of file luapp_state.cpp.
void lua::state::set_top | ( | uint | uiSize | ) |
Changes the stack size.
uiSize | The new size of the stack |
Definition at line 1141 of file luapp_state.cpp.
std::string lua::state::table_to_string | ( | const std::string & | sTable | ) |
Concatenates a Lua table into a string.
sTable | The name of the table in Lua |
Definition at line 79 of file luapp_state.cpp.
std::map< lua_State *, state * > lua::state::lLuaStateMap_ [static, private] |
Definition at line 517 of file luapp_state.hpp.
c_function lua::state::pErrorFunction_ [private] |
Definition at line 521 of file luapp_state.hpp.
lua_State* lua::state::pLua_ [private] |
Definition at line 519 of file luapp_state.hpp.
print_function lua::state::pPrintFunction_ [private] |
Definition at line 522 of file luapp_state.hpp.
std::string lua::state::sComString |
Definition at line 508 of file luapp_state.hpp.