$search
#include <context.h>
Public Member Functions | |
void | add_cpackage_dir (const char *path) |
void | add_package (const char *package) |
void | add_package_dir (const char *path) |
void | add_watchdir (const char *path) |
void | add_watcher (LuaContextWatcher *watcher) |
void | add_watchfile (const char *path) |
void | create_table (int narr=0, int nrec=0) |
void | do_file (const char *filename) |
void | do_string (const char *format,...) |
virtual void | fam_event (const char *filename, unsigned int mask) |
void | get_field (int idx, const char *k) |
void | get_global (const char *name) |
lua_State * | get_lua_state () |
void | get_table (int idx) |
bool | is_boolean (int idx) |
bool | is_cfunction (int idx) |
bool | is_function (int idx) |
bool | is_light_user_data (int idx) |
bool | is_nil (int idx) |
bool | is_number (int idx) |
bool | is_string (int idx) |
bool | is_table (int idx) |
bool | is_thread (int idx) |
void | load_string (const char *s) |
void | lock () |
LuaContext (lua_State *L) | |
LuaContext (bool watch_dirs=true, bool enable_tracebacks=true) | |
size_t | objlen (int idx) |
void | pcall (int nargs=0, int nresults=0, int errfunc=0) |
void | pop (int n) |
void | process_fam_events () |
void | push_boolean (bool value) |
void | push_cfunction (lua_CFunction function) |
void | push_fstring (const char *format,...) |
void | push_integer (lua_Integer value) |
void | push_light_user_data (void *p) |
void | push_lstring (const char *s, size_t len) |
void | push_nil () |
void | push_number (lua_Number value) |
void | push_string (const char *value) |
void | push_thread () |
void | push_usertype (void *data, const char *type_name, const char *name_space=0) |
void | push_value (int idx) |
void | push_vfstring (const char *format, va_list arg) |
void | raw_get (int idx) |
void | raw_geti (int idx, int n) |
void | raw_set (int idx) |
void | raw_seti (int idx, int n) |
void | remove (int idx) |
void | remove_global (const char *name) |
void | remove_watcher (LuaContextWatcher *watcher) |
void | restart () |
void | set_boolean (const char *name, bool value) |
void | set_cfunction (const char *name, lua_CFunction function) |
void | set_field (const char *key, int t_index=-2) |
void | set_global (const char *name) |
void | set_integer (const char *name, lua_Integer value) |
void | set_number (const char *name, lua_Number value) |
void | set_start_script (const char *start_script) |
void | set_string (const char *name, const char *value) |
void | set_table (int t_index=-3) |
void | set_usertype (const char *name, void *data, const char *type_name, const char *name_space=0) |
void | setfenv (int idx=-2) |
int | stack_size () |
bool | to_boolean (int idx) |
lua_Integer | to_integer (int idx) |
lua_Number | to_number (int idx) |
const char * | to_string (int idx) |
bool | try_lock () |
void | unlock () |
~LuaContext () | |
Private Member Functions | |
void | assert_unique_name (const char *name, std::string type) |
void | do_file (lua_State *L, const char *s) |
void | do_string (lua_State *L, const char *format,...) |
lua_State * | init_state () |
Private Attributes | |
std::map< std::string, bool > | __booleans |
std::map< std::string, bool > ::iterator | __booleans_it |
std::map< std::string, lua_CFunction > | __cfunctions |
std::map< std::string, lua_CFunction >::iterator | __cfunctions_it |
std::list< std::string > | __cpackage_dirs |
bool | __enable_tracebacks |
FileAlterationMonitor * | __fam |
std::map< std::string, lua_Integer > | __integers |
std::map< std::string, lua_Integer >::iterator | __integers_it |
lua_State * | __L |
Mutex * | __lua_mutex |
std::map< std::string, lua_Number > | __numbers |
std::map< std::string, lua_Number >::iterator | __numbers_it |
bool | __owns_L |
std::list< std::string > | __package_dirs |
std::list< std::string > | __packages |
std::list< std::string >::iterator | __slit |
char * | __start_script |
std::map< std::string, std::string > | __strings |
std::map< std::string, std::string >::iterator | __strings_it |
std::map< std::string, std::pair< void *, std::string > > | __usertypes |
std::map< std::string, std::pair< void *, std::string > >::iterator | __utit |
LockList< LuaContextWatcher * > | __watchers |
Lua C++ wrapper. This thin wrapper allows for easy integration of Fawkes into other applications. It provides convenience methods to some Lua and tolua++ features like setting global variables or pushing/popping values.
It allows raw access to the Lua state since this class does not and should not provide all the features Lua provides. If you use this make sure that you lock the Lua context to avoid multi-threading problems (if that is a possible concern in your application).
LuaContext can use a FileAlterationMonitor on all added package and C package directories. If anything changes in these directories the Lua instance is then automatically restarted (closed, re-opened and re-initialized).
Definition at line 53 of file context.h.
fawkes::LuaContext::LuaContext | ( | bool | watch_dirs = true , |
|
bool | enable_tracebacks = true | |||
) |
Constructor.
watch_dirs | true to watch added package and C package dirs for changes | |
enable_tracebacks | if true an error function is installed at the top of the stackand used for pcalls where errfunc is 0. |
Definition at line 74 of file context.cpp.
fawkes::LuaContext::LuaContext | ( | lua_State * | L | ) |
Wrapper contstructor. This wraps around an existing Lua state. It does not initialize the state in the sense that it would add variables etc. It only provides convenient access to the state methods via a C++ interface. It's mainly intended to be used to create a LuaContext to be passed to LuaContextWatcher::lua_restarted(). The state is not closed on destruction as is done when using the other ctor.
L | Lua state to wrap |
Definition at line 103 of file context.cpp.
fawkes::LuaContext::~LuaContext | ( | ) |
Destructor.
Definition at line 115 of file context.cpp.
void fawkes::LuaContext::add_cpackage_dir | ( | const char * | path | ) |
Add a Lua C package directory. The directory is added to the search path for lua C packages. Files with a .so suffix will be considered as Lua modules.
path | path to add |
Definition at line 353 of file context.cpp.
void fawkes::LuaContext::add_package | ( | const char * | package | ) |
Add a default package. Packages that are added this way are automatically loaded now and on restart().
package | package to add |
Definition at line 372 of file context.cpp.
void fawkes::LuaContext::add_package_dir | ( | const char * | path | ) |
Add a Lua package directory. The directory is added to the search path for lua packages. Files with a .lua suffix will be considered as Lua modules.
path | path to add |
Definition at line 334 of file context.cpp.
void fawkes::LuaContext::add_watchdir | ( | const char * | path | ) |
Add a directory to watch for changes. Files with a .lua suffix will be considered as Lua modules.
path | path to add |
Definition at line 390 of file context.cpp.
void fawkes::LuaContext::add_watcher | ( | fawkes::LuaContextWatcher * | watcher | ) |
Add a context watcher.
watcher | watcher to add |
Definition at line 1384 of file context.cpp.
void fawkes::LuaContext::add_watchfile | ( | const char * | path | ) |
Add a file to watch for changes.
path | path to add |
Definition at line 402 of file context.cpp.
void fawkes::LuaContext::assert_unique_name | ( | const char * | name, | |
std::string | type | |||
) | [private] |
Assert that the name is unique. Checks the internal context structures if the name has been used already. It will accept a value that has already been set that is of the same type as the one supplied. Pass the empty string to avoid this.
name | name to check | |
type | type of value |
Exception | thrown if name is not unique |
Definition at line 664 of file context.cpp.
void fawkes::LuaContext::create_table | ( | int | narr = 0 , |
|
int | nrec = 0 | |||
) |
Create a table on top of the stack.
narr | number of array elements | |
nrec | number of non-array elements |
Definition at line 1060 of file context.cpp.
void fawkes::LuaContext::do_file | ( | lua_State * | L, | |
const char * | filename | |||
) | [private] |
Execute file on a specific Lua state.
L | Lua state to execute the file in. | |
filename | filet to load and excute. |
Definition at line 468 of file context.cpp.
void fawkes::LuaContext::do_file | ( | const char * | filename | ) |
Execute file.
filename | filet to load and excute. |
Definition at line 454 of file context.cpp.
void fawkes::LuaContext::do_string | ( | lua_State * | L, | |
const char * | format, | |||
... | ||||
) | [private] |
Execute string on a specific Lua state.
L | Lua state to execute the string in | |
format | format of string to execute, arguments can be the same as for vasprintf. |
Definition at line 533 of file context.cpp.
void fawkes::LuaContext::do_string | ( | const char * | format, | |
... | ||||
) |
Execute string.
format | format of string to execute, arguments can be the same as for vasprintf. |
Definition at line 562 of file context.cpp.
void fawkes::LuaContext::fam_event | ( | const char * | filename, | |
unsigned int | mask | |||
) | [virtual] |
Event has been raised.
filename | name of the file that triggered the event | |
mask | mask indicating the event. Currently inotify event flags are used, see inotify.h. |
Implements fawkes::FamListener.
Definition at line 1417 of file context.cpp.
void fawkes::LuaContext::get_field | ( | int | idx, | |
const char * | k | |||
) |
Get named value from table. Retrieves the t[k], where k is the given key and t is a table at the given index idx. The value is pushed onto the stack.
idx | index of the table | |
k | key of the table entry |
Definition at line 1127 of file context.cpp.
void fawkes::LuaContext::get_global | ( | const char * | name | ) |
Get global variable.
name | name of the global variable |
Definition at line 1184 of file context.cpp.
lua_State * fawkes::LuaContext::get_lua_state | ( | ) |
Get Lua state. Allows for raw modification of the used Lua state. Remember proper locking!
Definition at line 417 of file context.cpp.
void fawkes::LuaContext::get_table | ( | int | idx | ) |
Get value from table. Assumes that an index k is at the top of the stack. Then t[k] is retrieved, where t is a table at the given index idx. The resulting value is pushed onto the stack, while the key k is popped from the stack, thus the value replaces the key.
idx | index of the table on the stack |
Definition at line 1114 of file context.cpp.
lua_State * fawkes::LuaContext::init_state | ( | ) | [private] |
Initialize Lua state. Initializes the state and makes all necessary initializations.
Definition at line 155 of file context.cpp.
bool fawkes::LuaContext::is_boolean | ( | int | idx | ) |
Check if stack value is a boolean.
idx | stack index of value |
Definition at line 1263 of file context.cpp.
bool fawkes::LuaContext::is_cfunction | ( | int | idx | ) |
Check if stack value is a C function.
idx | stack index of value |
Definition at line 1274 of file context.cpp.
bool fawkes::LuaContext::is_function | ( | int | idx | ) |
Check if stack value is a function.
idx | stack index of value |
Definition at line 1285 of file context.cpp.
bool fawkes::LuaContext::is_light_user_data | ( | int | idx | ) |
Check if stack value is light user data.
idx | stack index of value |
Definition at line 1296 of file context.cpp.
bool fawkes::LuaContext::is_nil | ( | int | idx | ) |
Check if stack value is nil.
idx | stack index of value |
Definition at line 1307 of file context.cpp.
bool fawkes::LuaContext::is_number | ( | int | idx | ) |
Check if stack value is a number.
idx | stack index of value |
Definition at line 1318 of file context.cpp.
bool fawkes::LuaContext::is_string | ( | int | idx | ) |
Check if stack value is a string.
idx | stack index of value |
Definition at line 1329 of file context.cpp.
bool fawkes::LuaContext::is_table | ( | int | idx | ) |
Check if stack value is a table.
idx | stack index of value |
Definition at line 1340 of file context.cpp.
bool fawkes::LuaContext::is_thread | ( | int | idx | ) |
Check if stack value is a thread.
idx | stack index of value |
Definition at line 1351 of file context.cpp.
void fawkes::LuaContext::load_string | ( | const char * | s | ) |
Load Lua string. Loads the Lua string and places it as a function on top of the stack.
s | string to load |
Definition at line 594 of file context.cpp.
void fawkes::LuaContext::lock | ( | ) |
Lock Lua state.
Definition at line 426 of file context.cpp.
size_t fawkes::LuaContext::objlen | ( | int | idx | ) |
Get object length
idx | stack index of value |
Definition at line 1362 of file context.cpp.
void fawkes::LuaContext::pcall | ( | int | nargs = 0 , |
|
int | nresults = 0 , |
|||
int | errfunc = 0 | |||
) |
Protected call. Calls the function on top of the stack. Errors are handled gracefully.
nargs | number of arguments | |
nresults | number of results | |
errfunc | stack index of an error handling function |
Exception | thrown for generic runtime error or if the error function could not be executed. | |
OutOfMemoryException | thrown if not enough memory was available |
Definition at line 630 of file context.cpp.
void fawkes::LuaContext::pop | ( | int | n | ) |
Pop value(s) from stack.
n | number of values to pop |
Definition at line 1018 of file context.cpp.
void fawkes::LuaContext::process_fam_events | ( | ) |
Process FAM events.
Definition at line 1410 of file context.cpp.
void fawkes::LuaContext::push_boolean | ( | bool | value | ) |
Push boolean on top of stack.
value | value to push |
Definition at line 836 of file context.cpp.
void fawkes::LuaContext::push_cfunction | ( | lua_CFunction | function | ) |
Push C function on top of stack.
function | C function to push |
Definition at line 1005 of file context.cpp.
void fawkes::LuaContext::push_fstring | ( | const char * | format, | |
... | ||||
) |
Push formatted string on top of stack.
format | string format |
Definition at line 850 of file context.cpp.
void fawkes::LuaContext::push_integer | ( | lua_Integer | value | ) |
Push integer on top of stack.
value | value to push |
Definition at line 866 of file context.cpp.
void fawkes::LuaContext::push_light_user_data | ( | void * | p | ) |
Push light user data on top of stack.
p | pointer to light user data to push |
Definition at line 879 of file context.cpp.
void fawkes::LuaContext::push_lstring | ( | const char * | s, | |
size_t | len | |||
) |
Push substring on top of stack.
s | string to push | |
len | length of string to push |
Definition at line 893 of file context.cpp.
void fawkes::LuaContext::push_nil | ( | ) |
Push nil on top of stack.
Definition at line 905 of file context.cpp.
void fawkes::LuaContext::push_number | ( | lua_Number | value | ) |
Push number on top of stack.
value | value to push |
Definition at line 918 of file context.cpp.
void fawkes::LuaContext::push_string | ( | const char * | value | ) |
Push string on top of stack.
value | value to push |
Definition at line 931 of file context.cpp.
void fawkes::LuaContext::push_thread | ( | ) |
Push thread on top of stack.
Definition at line 943 of file context.cpp.
void fawkes::LuaContext::push_usertype | ( | void * | data, | |
const char * | type_name, | |||
const char * | name_space = 0 | |||
) |
Push usertype on top of stack.
data | usertype data | |
type_name | type name of the data | |
name_space | C++ namespace of type, prepended to type_name |
Definition at line 986 of file context.cpp.
void fawkes::LuaContext::push_value | ( | int | idx | ) |
Push a copy of the element at the given index on top of the stack.
idx | index of the value to copy |
Definition at line 956 of file context.cpp.
void fawkes::LuaContext::push_vfstring | ( | const char * | format, | |
va_list | arg | |||
) |
Push formatted string on top of stack.
format | string format | |
arg | variadic argument list |
Definition at line 971 of file context.cpp.
void fawkes::LuaContext::raw_get | ( | int | idx | ) |
Get value without invoking meta methods. Similar to get_table(), but does raw access, i.e. without invoking meta-methods.
idx | index of the table |
Definition at line 1162 of file context.cpp.
void fawkes::LuaContext::raw_geti | ( | int | idx, | |
int | n | |||
) |
Get indexed value without invoking meta methods. Pushes t[n] onto the stack, where t is a table at index idx.
idx | index of the table | |
n | index in the table |
Definition at line 1174 of file context.cpp.
void fawkes::LuaContext::raw_set | ( | int | idx | ) |
Set value without invoking meta methods. Similar to set_table(), but does raw access, i.e. without invoking meta-methods.
idx | index of the table |
Definition at line 1138 of file context.cpp.
void fawkes::LuaContext::raw_seti | ( | int | idx, | |
int | n | |||
) |
Set indexed value without invoking meta methods. Sets t[n]=v, where t is a table at index idx and v is the value at the top of the stack.
idx | index of the table | |
n | index in the table |
Definition at line 1151 of file context.cpp.
void fawkes::LuaContext::remove | ( | int | idx | ) |
Remove value from stack.
idx | index of element to remove |
Definition at line 1033 of file context.cpp.
void fawkes::LuaContext::remove_global | ( | const char * | name | ) |
Remove global variable. Assigns nil to the given variable and removes it from internal assignment maps.
name | name of value to remove |
Definition at line 1196 of file context.cpp.
void fawkes::LuaContext::remove_watcher | ( | fawkes::LuaContextWatcher * | watcher | ) |
Remove a context watcher.
watcher | watcher to remove |
Definition at line 1398 of file context.cpp.
void fawkes::LuaContext::restart | ( | ) |
Restart Lua. Creates a new Lua state, initializes it, anf if this went well the current state is swapped with the new state.
Definition at line 275 of file context.cpp.
void fawkes::LuaContext::set_boolean | ( | const char * | name, | |
bool | value | |||
) |
Assign boolean to global variable.
name | name of global variable to assign the value to | |
value | value to assign |
Definition at line 762 of file context.cpp.
void fawkes::LuaContext::set_cfunction | ( | const char * | name, | |
lua_CFunction | function | |||
) |
Assign C function to global variable.
name | name of global variable to assign the value to | |
function | function to assign |
Definition at line 818 of file context.cpp.
void fawkes::LuaContext::set_field | ( | const char * | key, | |
int | t_index = -2 | |||
) |
Set field of a table. Does the equivalent to t[k] = v, where t is the value at the given valid index and v is the value at the top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.
key | key of the field to set | |
t_index | index of the table on the stack, defaults to the element just below the value to set (-2, second last element on the stack). |
Definition at line 1088 of file context.cpp.
void fawkes::LuaContext::set_global | ( | const char * | name | ) |
Set a global value. Sets the global variable with the given name to the value currently on top of the stack. No check whatsoever regarding the name is done.
name | name of the variable to assign |
Definition at line 1100 of file context.cpp.
void fawkes::LuaContext::set_integer | ( | const char * | name, | |
lua_Integer | value | |||
) |
Assign integer to global variable.
name | name of global variable to assign the value to | |
value | value to assign |
Definition at line 800 of file context.cpp.
void fawkes::LuaContext::set_number | ( | const char * | name, | |
lua_Number | value | |||
) |
Assign number to global variable.
name | name of global variable to assign the value to | |
value | value to assign |
Definition at line 781 of file context.cpp.
void fawkes::LuaContext::set_start_script | ( | const char * | start_script | ) |
Set start script. The script will be executed once immediately in this method, make sure you call this after all other init-relevant routines like add_* if you need to access these in the start script!
start_script | script to execute now and on restart(). If the string is the path and name of an accessible file it is loaded via do_file(), otherwise it is considered to be the name of a module and loaded via Lua's require(). Note however, that if you use a module, special care has to be taken to correctly modify the global environment! |
Definition at line 253 of file context.cpp.
void fawkes::LuaContext::set_string | ( | const char * | name, | |
const char * | value | |||
) |
Assign string to global variable.
name | name of global variable to assign the value to | |
value | value to assign |
Definition at line 743 of file context.cpp.
void fawkes::LuaContext::set_table | ( | int | t_index = -3 |
) |
Set value of a table. Sets value t[k] = v. t is the table at the given index, by default it is the third-last entry (index is -3). v is the value at the top of the stack, k is the element just below the top.
t_index | index of the table on the stack |
Definition at line 1073 of file context.cpp.
void fawkes::LuaContext::set_usertype | ( | const char * | name, | |
void * | data, | |||
const char * | type_name, | |||
const char * | name_space = 0 | |||
) |
Assign usertype to global variable.
name | name of global variable to assign the value to | |
data | usertype data | |
type_name | type name of the data | |
name_space | C++ namespace of type, prepended to type_name |
Definition at line 718 of file context.cpp.
void fawkes::LuaContext::setfenv | ( | int | idx = -2 |
) |
Set function environment. Sets the table on top of the stack as environment of the function at the given stack index.
idx | stack index of function |
Definition at line 1374 of file context.cpp.
int fawkes::LuaContext::stack_size | ( | ) |
Get size of stack.
Definition at line 1049 of file context.cpp.
bool fawkes::LuaContext::to_boolean | ( | int | idx | ) |
Retrieve stack value as boolean.
idx | stack index of value |
Definition at line 1241 of file context.cpp.
lua_Integer fawkes::LuaContext::to_integer | ( | int | idx | ) |
Retrieve stack value as integer.
idx | stack index of value |
Definition at line 1230 of file context.cpp.
lua_Number fawkes::LuaContext::to_number | ( | int | idx | ) |
Retrieve stack value as number.
idx | stack index of value |
Definition at line 1219 of file context.cpp.
const char * fawkes::LuaContext::to_string | ( | int | idx | ) |
Retrieve stack value as string.
idx | stack index of value |
Definition at line 1252 of file context.cpp.
bool fawkes::LuaContext::try_lock | ( | ) |
Try to lock the Lua state.
Definition at line 436 of file context.cpp.
void fawkes::LuaContext::unlock | ( | ) |
Unlock Lua state.
Definition at line 444 of file context.cpp.
std::map<std::string, bool> fawkes::LuaContext::__booleans [private] |
std::map<std::string, bool>::iterator fawkes::LuaContext::__booleans_it [private] |
std::map<std::string, lua_CFunction> fawkes::LuaContext::__cfunctions [private] |
std::map<std::string, lua_CFunction>::iterator fawkes::LuaContext::__cfunctions_it [private] |
std::list<std::string> fawkes::LuaContext::__cpackage_dirs [private] |
bool fawkes::LuaContext::__enable_tracebacks [private] |
FileAlterationMonitor* fawkes::LuaContext::__fam [private] |
std::map<std::string, lua_Integer> fawkes::LuaContext::__integers [private] |
std::map<std::string, lua_Integer>::iterator fawkes::LuaContext::__integers_it [private] |
lua_State* fawkes::LuaContext::__L [private] |
Mutex* fawkes::LuaContext::__lua_mutex [private] |
std::map<std::string, lua_Number> fawkes::LuaContext::__numbers [private] |
std::map<std::string, lua_Number>::iterator fawkes::LuaContext::__numbers_it [private] |
bool fawkes::LuaContext::__owns_L [private] |
std::list<std::string> fawkes::LuaContext::__package_dirs [private] |
std::list<std::string> fawkes::LuaContext::__packages [private] |
std::list<std::string>::iterator fawkes::LuaContext::__slit [private] |
char* fawkes::LuaContext::__start_script [private] |
std::map<std::string, std::string> fawkes::LuaContext::__strings [private] |
std::map<std::string, std::string>::iterator fawkes::LuaContext::__strings_it [private] |
std::map<std::string, std::pair<void *, std::string> > fawkes::LuaContext::__usertypes [private] |
std::map<std::string, std::pair<void *, std::string> >::iterator fawkes::LuaContext::__utit [private] |
LockList<LuaContextWatcher *> fawkes::LuaContext::__watchers [private] |