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 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 >
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

lua::state::state (  ) 

Constructor.

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

Definition at line 39 of file luapp_state.cpp.

lua::state::~state (  ) 

Destructor.

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

Definition at line 57 of file luapp_state.cpp.


Member Function Documentation

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

Executes a Lua function with arguments.

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

Definition at line 367 of file luapp_state.cpp.

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

Executes a Lua function.

Parameters:
sFunctionName The 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 328 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 one Lua state to another.

Parameters:
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
Note:
If sDestName is ommited, the table will have the same name in both Lua states.

Definition at line 106 of file luapp_state.cpp.

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

Executes a Lua file.

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

Definition at line 240 of file luapp_state.cpp.

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

Executes a string containing Lua instructions.

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

Definition at line 289 of file luapp_state.cpp.

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

Formats a raw error string.

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

Definition at line 414 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:
iIndex The index at which to search for the value
Returns:
The Lunar object at the given index

Definition at line 262 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:
iIndex The index at which to search for the value
Returns:
The value at the given index converted to a bool

Definition at line 671 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:
iID The id of the key associated to the value
iIndex The position of the table in the stack
Note:
Puts 'nil' if the key couldn't be found.

Definition at line 894 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:
sName The name of the key associated to the value
iIndex The position of the table in the stack
Note:
Puts 'nil' if the key couldn't be found.

Definition at line 889 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:
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
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 993 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:
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
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 933 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:
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
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 903 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:
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
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 963 of file luapp_state.cpp.

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

Puts a global variable on the stack.

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

Definition at line 735 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:
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
Returns:
The bool value

Definition at line 861 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:
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
Returns:
The float value

Definition at line 805 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:
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
Returns:
The int value

Definition at line 777 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:
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
Returns:
The string value

Definition at line 833 of file luapp_state.cpp.

c_function lua::state::get_lua_error_function (  )  const

Returns the current error formatting function.

Returns:
The current error formatting function
Note:
See set_lua_error_function() for more information.
This method is mostly usefull if you want to save the current error function, use another one temporarily, and restore the previous one.

Definition at line 437 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:
iIndex The index at which to search for the value
Returns:
The value at the given index converted to a number

Definition at line 666 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.

Returns:
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.

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

Definition at line 676 of file luapp_state.cpp.

uint lua::state::get_top (  ) 

Returns the number of value on the stack.

Returns:
The number of value on the stack

Definition at line 693 of file luapp_state.cpp.

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

Returns the type of the value on the stack.

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

Definition at line 698 of file luapp_state.cpp.

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

Returns the name of a type.

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

Definition at line 717 of file luapp_state.cpp.

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

Returns the value at the given index.

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

Definition at line 681 of file luapp_state.cpp.

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

Checks if a variable is serializable.

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

Definition at line 450 of file luapp_state.cpp.

void lua::state::new_table (  ) 

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

Definition at line 650 of file luapp_state.cpp.

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

Iterates over the table at the given index.

Parameters:
iIndex The 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 655 of file luapp_state.cpp.

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

Removes the value at the top of the stack.

Parameters:
uiNumber The number of value to remove

Definition at line 661 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:
sError The error string to output
Note:
This function will print out the current file and line

Definition at line 424 of file luapp_state.cpp.

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

Puts a value on the stack.

Parameters:
vValue The value to push on the stack

Definition at line 577 of file luapp_state.cpp.

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

Puts a boolean on the stack.

Parameters:
bValue The value to push on the stack

Definition at line 561 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:
sName The name of this variable

Definition at line 594 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 206 of file luapp_state.hpp.

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

Puts "nil" (null) on the stack.

Parameters:
uiNumber The number of "nil" to push

Definition at line 571 of file luapp_state.cpp.

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

Puts a number on the stack.

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

Definition at line 556 of file luapp_state.cpp.

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

Puts a string on the stack.

Parameters:
sValue The value to push on the stack

Definition at line 566 of file luapp_state.cpp.

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

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

Parameters:
iIndex The index of the value to push

Definition at line 589 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.

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

Binds a C++ function to a Lua function.

Parameters:
sFunctionName The name of the Lua function
mFunction The C++ function to bind

Definition at line 409 of file luapp_state.cpp.

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

Writes the content of a variable in a string.

Parameters:
sTab Number of space to put in front of each line
iIndex The 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 483 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:
sName The name of the global variable
Returns:
The content of the variable

Definition at line 470 of file luapp_state.cpp.

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

Writes a value into a Lua table.

Parameters:
iID The 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 1031 of file luapp_state.cpp.

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

Writes a value into a Lua table.

Parameters:
sName The 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 1023 of file luapp_state.cpp.

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

Writes a bool into a Lua table.

Parameters:
iID The ID of the key associated to the value
bValue The 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 1088 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:
sName The name of the key associated to the value
bValue The 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 1060 of file luapp_state.cpp.

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

Writes a float into a Lua table.

Parameters:
iID The ID of the key associated to the value
dValue The 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 1074 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:
sName The name of the key associated to the value
dValue The 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 1046 of file luapp_state.cpp.

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

Writes an int into a Lua table.

Parameters:
iID The ID of the key associated to the value
iValue The 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 1067 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:
sName The name of the key associated to the value
iValue The 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 1039 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:
iID The ID of the key associated to the value
sValue The 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 1081 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:
sName The name of the key associated to the value
sValue The 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 1053 of file luapp_state.cpp.

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

Sets the value of a global Lua variable.

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

Definition at line 599 of file luapp_state.cpp.

void lua::state::set_lua_error_function ( c_function  pFunc  ) 

Sets a custom error formatting function.

Parameters:
pFunc The 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 429 of file luapp_state.cpp.

void lua::state::set_print_error_function ( print_function  pFunc  ) 

Sets a custom error printing function.

Parameters:
pFunc The error printing function
Note:
By default, errors are sent through std::cerr.

Definition at line 442 of file luapp_state.cpp.

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

Changes the stack size.

Parameters:
uiSize The 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 1095 of file luapp_state.cpp.

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

Concatenates a Lua table into a string.

Parameters:
sTable The name of the table in Lua

Definition at line 79 of file luapp_state.cpp.


Member Data Documentation

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

Definition at line 498 of file luapp_state.hpp.

Definition at line 502 of file luapp_state.hpp.

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

Definition at line 500 of file luapp_state.hpp.

Definition at line 503 of file luapp_state.hpp.

Definition at line 492 of file luapp_state.hpp.


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

Generated on Sun Apr 29 19:27:31 2012 for lua++ by  doxygen 1.6.1