lua::function Class Reference

A helper to write Lua glues. More...

#include <luapp_function.hpp>

List of all members.

Public Member Functions

 function (const std::string &sName, lua_State *pLua, uint uiReturnNbr=0u)
 Default constructor.
 function (const std::string &sName, state *pLua, uint uiReturnNbr=0u)
 Default constructor.
 ~function ()
 Destructor.
void add (uint uiIndex, const std::string &sName, type mLuaType, bool bOptional=false)
 Adds an argument to that function.
void new_param_set ()
 Tells the function there is another parameter set that will be provided.
uint get_param_set_rank ()
 Returns the indice of the paramater set that the user has provided.
bool check (bool bPrintError=true)
 Checks if all the arguments types and retreive them.
utils::wptr< argumentget (uint uiIndex)
 Returns the argument at the provided index.
bool is_provided (uint uiIndex) const
 Checks if an argument has been provided (in case it is optional).
uint get_argument_count () const
 Returns the number of provided arguments.
void push (const std::string &sValue)
 Adds a return value to the function.
void push (const double &dValue)
 Adds a return value to the function.
void push (float fValue)
 Adds a return value to the function.
void push (int iValue)
 Adds a return value to the function.
void push (uint uiValue)
 Adds a return value to the function.
void push (bool bValue)
 Adds a return value to the function.
void push_nil (uint uiNbr=1)
 Adds nil to the return values.
void notify_pushed ()
 Tells this function you pushed a return value.
int on_return ()
 ends the function.
const std::string & get_name () const
 Returns the name of this function.
stateget_state () const
 Returns the state used by this function.

Private Member Functions

 function (const function &)
functionoperator= (function &)

Private Attributes

std::string sName_
statepLua_
uint uiArgumentCount_
uint uiReturnNbr_
uint uiReturnCount_
std::vector< argument_listlArgListStack_
argument_listpArgList_

Detailed Description

A helper to write Lua glues.

A glue is a C++ function that is executed in Lua. Due to the way Lua communicates with C++, creating such functions can become boring if you check every argument's type, or if you allow optional arguments, or even two possible types for a single argument.
This is done quite easilly with this class.
Look at the source code for existing glues to see how things work (UIObject::_SetPoint() is a good example).

Definition at line 175 of file luapp_function.hpp.


Constructor & Destructor Documentation

lua::function::function ( const std::string &  sName,
lua_State *  pLua,
uint  uiReturnNbr = 0u 
)

Default constructor.

Parameters:
sName The name of your function (used to print errors in the log)
pLua The Lua state to use
uiReturnNbr The maximum number of returned values
Note:
Call this at the beginning of your glue

Definition at line 13 of file luapp_function.cpp.

lua::function::function ( const std::string &  sName,
state pLua,
uint  uiReturnNbr = 0u 
)

Default constructor.

Parameters:
sName The name of your function (used to print errors in the log)
pLua The Lua state to use
uiReturnNbr The maximum number of returned values
Note:
Call this at the beginning of your glue

Definition at line 20 of file luapp_function.cpp.

lua::function::~function (  ) 

Destructor.

Definition at line 27 of file luapp_function.cpp.

lua::function::function ( const function  )  [private]

Member Function Documentation

void lua::function::add ( uint  uiIndex,
const std::string &  sName,
type  mLuaType,
bool  bOptional = false 
)

Adds an argument to that function.

Parameters:
uiIndex The index of this argument
sName The name of this argument (used to print errors in the log)
mLuaType The expected type in Lua
bOptional 'true' if this argument is not essential and can be ommited
Note:
Optional arguments work just like in C++ : if you say argument n is optional, then all the following arguments will have to be optional too.
You can add several different arguments with the same index, but they must have different Lua types. The function will then choose the right one acording to the actual Lua type that the user has provided.

Definition at line 31 of file luapp_function.cpp.

bool lua::function::check ( bool  bPrintError = true  ) 

Checks if all the arguments types and retreive them.

Parameters:
bPrintError If set to 'false', this function will fail silently
Returns:
'true' if everything went fine
Note:
You should always check the return value of this function.

Definition at line 90 of file luapp_function.cpp.

utils::wptr< argument > lua::function::get ( uint  uiIndex  ) 

Returns the argument at the provided index.

Parameters:
uiIndex The index of the argument
Returns:
The argument at the provided index

Definition at line 61 of file luapp_function.cpp.

uint lua::function::get_argument_count (  )  const

Returns the number of provided arguments.

Returns:
The number of provided arguments
Note:
Should be called after Check() has been called.

Definition at line 85 of file luapp_function.cpp.

const std::string & lua::function::get_name (  )  const

Returns the name of this function.

Returns:
The name of this function

Definition at line 252 of file luapp_function.cpp.

uint lua::function::get_param_set_rank (  ) 

Returns the indice of the paramater set that the user has provided.

Returns:
The indice of the paramater set that the user has provided
Note:
Should be called after Check() has been called.

Definition at line 56 of file luapp_function.cpp.

state * lua::function::get_state (  )  const

Returns the state used by this function.

Returns:
The state used by this function

Definition at line 358 of file luapp_function.cpp.

bool lua::function::is_provided ( uint  uiIndex  )  const

Checks if an argument has been provided (in case it is optional).

Parameters:
uiIndex The index of the argument
Returns:
'true' if the user has provided this argument
Note:
You should always check the return value of this function whenever you want to use an optional argument.

Definition at line 72 of file luapp_function.cpp.

void lua::function::new_param_set (  ) 

Tells the function there is another parameter set that will be provided.

Definition at line 49 of file luapp_function.cpp.

void lua::function::notify_pushed (  ) 

Tells this function you pushed a return value.

Note:
See push for more infos.

Definition at line 340 of file luapp_function.cpp.

int lua::function::on_return (  ) 

ends the function.

Returns:
The number of returned values
Note:
Use the return of this function as return value of your glue.
Note that this class will automatically fill the stack with nil until the proper number of return values is reached.

Definition at line 348 of file luapp_function.cpp.

function& lua::function::operator= ( function  )  [private]
void lua::function::push ( bool  bValue  ) 

Adds a return value to the function.

Parameters:
bValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 317 of file luapp_function.cpp.

void lua::function::push ( uint  uiValue  ) 

Adds a return value to the function.

Parameters:
uiValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 307 of file luapp_function.cpp.

void lua::function::push ( int  iValue  ) 

Adds a return value to the function.

Parameters:
iValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 297 of file luapp_function.cpp.

void lua::function::push ( float  fValue  ) 

Adds a return value to the function.

Parameters:
fValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 287 of file luapp_function.cpp.

void lua::function::push ( const double &  dValue  ) 

Adds a return value to the function.

Parameters:
dValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 277 of file luapp_function.cpp.

void lua::function::push ( const std::string &  sValue  ) 

Adds a return value to the function.

Parameters:
sValue The value
Note:
- If you want to return a complex lua object (a table, a function, ...), you must put it on the stack yourself, and then call notify_pushed();
  • Values are immediatly pushed onto the lua stack, so the order in which you return your values is important.
  • If, for some reason, your can't push one of your return values, push nil instead.
  • Note that this class will automatically fill the stack with nil until the proper number of return values is reached when on_return() is called.

Definition at line 267 of file luapp_function.cpp.

void lua::function::push_nil ( uint  uiNbr = 1  ) 

Adds nil to the return values.

Parameters:
uiNbr The number of nil to push
Note:
Calling push(var()) does exactly the same thing. But this function is clearer.

Definition at line 327 of file luapp_function.cpp.


Member Data Documentation

Definition at line 378 of file luapp_function.hpp.

Definition at line 379 of file luapp_function.hpp.

Definition at line 374 of file luapp_function.hpp.

std::string lua::function::sName_ [private]

Definition at line 373 of file luapp_function.hpp.

Definition at line 375 of file luapp_function.hpp.

Definition at line 377 of file luapp_function.hpp.

Definition at line 376 of file luapp_function.hpp.


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

Generated on Wed May 16 21:32:42 2012 for lua++ by  doxygen 1.6.1