Fawkes API Fawkes Development Version
context.h
1
2/***************************************************************************
3 * context.h - Fawkes Lua Context
4 *
5 * Created: Fri May 23 11:29:01 2008
6 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#ifndef _LUA_CONTEXT_H_
24#define _LUA_CONTEXT_H_
25
26#include <core/utils/lock_list.h>
27#include <core/utils/refptr.h>
28#include <lua/exceptions.h>
29#include <utils/system/fam.h>
30#include <utils/system/fam_thread.h>
31
32#include <list>
33#include <lua.hpp>
34#include <map>
35#include <string>
36#include <utility>
37
38namespace fawkes {
39
40class LuaContextWatcher;
41class Mutex;
42
43class LuaContext : public FamListener
44{
45public:
46 LuaContext(bool enable_tracebacks = true);
47 LuaContext(lua_State *L);
49
50 void setup_fam(bool auto_restart, bool conc_thread);
52
53 void set_start_script(const char *start_script);
54 void set_finalization_calls(std::string finalize,
55 std::string finalize_prepare,
56 std::string finalize_cancel);
57
58 void restart();
59
60 void add_package_dir(const char *path, bool prefix = false);
61 void add_cpackage_dir(const char *path, bool prefix = false);
62 void add_package(const char *package);
63
64 lua_State *get_lua_state();
65
66 void lock();
67 bool try_lock();
68 void unlock();
69
70 void do_file(const char *filename);
71 void do_string(const char *format, ...);
72
73 void load_string(const char *s);
74 void pcall(int nargs = 0, int nresults = 0, int errfunc = 0);
75
76 void
77 set_usertype(const char *name, void *data, const char *type_name, const char *name_space = 0);
78 void set_string(const char *name, const char *value);
79 void set_number(const char *name, lua_Number value);
80 void set_boolean(const char *name, bool value);
81 void set_integer(const char *name, lua_Integer value);
82 void set_cfunction(const char *name, lua_CFunction f);
83 void remove_global(const char *name);
84 void set_global(const char *name);
85
86 void push_boolean(bool value);
87 void push_fstring(const char *format, ...);
88 void push_integer(lua_Integer value);
89 void push_light_user_data(void *p);
90 void push_lstring(const char *s, size_t len);
91 void push_nil();
92 void push_number(lua_Number value);
93 void push_string(const char *value);
94 void push_thread();
95 void push_value(int idx);
96 void push_vfstring(const char *format, va_list arg);
97 void push_usertype(void *data, const char *type_name, const char *name_space = 0);
98 void push_cfunction(lua_CFunction f);
99
100 std::string type_name(int idx);
101
102 void pop(int n);
103 void remove(int idx);
104 int stack_size();
105
106 void create_table(int narr = 0, int nrec = 0);
107 void set_table(int t_index = -3);
108 void set_field(const char *key, int t_index = -2);
109
110 void get_table(int idx);
111 void get_field(int idx, const char *k);
112 void get_global(const char *name);
113
114 bool table_next(int idx);
115
116 void raw_set(int idx);
117 void raw_seti(int idx, int n);
118 void raw_get(int idx);
119 void raw_geti(int idx, int n);
120
121 lua_Number to_number(int idx);
122 lua_Integer to_integer(int idx);
123 bool to_boolean(int idx);
124 const char *to_string(int idx);
125 void * to_userdata(int idx);
126 void * to_pointer(int idx);
127 void * to_usertype(int idx);
128
129 bool is_boolean(int idx);
130 bool is_cfunction(int idx);
131 bool is_function(int idx);
132 bool is_light_user_data(int idx);
133 bool is_nil(int idx);
134 bool is_number(int idx);
135 bool is_string(int idx);
136 bool is_table(int idx);
137 bool is_thread(int idx);
138
139 size_t objlen(int idx);
140 void setfenv(int idx = -2);
141
142 void add_watcher(LuaContextWatcher *watcher);
143 void remove_watcher(LuaContextWatcher *watcher);
144
145 /* from FamListener */
146 virtual void fam_event(const char *filename, unsigned int mask);
147 void process_fam_events();
148
149private:
150 lua_State *init_state();
151 void do_string(lua_State *L, const char *format, ...);
152 void do_file(lua_State *L, const char *s);
153 void assert_unique_name(const char *name, std::string type);
154
155private:
156 lua_State *L_;
157 bool owns_L_;
158 bool enable_tracebacks_;
159
160 Mutex *lua_mutex_;
161 char * start_script_;
162
163 std::list<std::string> package_dirs_;
164 std::list<std::string> cpackage_dirs_;
165 std::list<std::string> packages_;
166 std::list<std::string>::iterator slit_;
167
168 std::map<std::string, std::pair<void *, std::string>> usertypes_;
169 std::map<std::string, std::pair<void *, std::string>>::iterator utit_;
170 std::map<std::string, std::string> strings_;
171 std::map<std::string, std::string>::iterator strings_it_;
172 std::map<std::string, bool> booleans_;
173 std::map<std::string, bool>::iterator booleans_it_;
174 std::map<std::string, lua_Number> numbers_;
175 std::map<std::string, lua_Number>::iterator numbers_it_;
176 std::map<std::string, lua_Integer> integers_;
177 std::map<std::string, lua_Integer>::iterator integers_it_;
178 std::map<std::string, lua_CFunction> cfuncs_;
179 std::map<std::string, lua_CFunction>::iterator cfuncs_it_;
180
181 std::string finalize_call_;
182 std::string finalize_prepare_call_;
183 std::string finalize_cancel_call_;
184
186 FamThread * fam_thread_;
187
189};
190
191} // end of namespace fawkes
192
193#endif
File Alteration Monitor Listener.
Definition: fam.h:36
FileAlterationMonitor thread wrapper.
Definition: fam_thread.h:33
List with a lock.
Definition: lock_list.h:45
Lua context watcher.
Lua C++ wrapper.
Definition: context.h:44
bool is_nil(int idx)
Check if stack value is nil.
Definition: context.cpp:1220
bool is_light_user_data(int idx)
Check if stack value is light user data.
Definition: context.cpp:1210
bool is_function(int idx)
Check if stack value is a function.
Definition: context.cpp:1200
bool is_number(int idx)
Check if stack value is a number.
Definition: context.cpp:1230
void push_string(const char *value)
Push string on top of stack.
Definition: context.cpp:839
void push_value(int idx)
Push a copy of the element at the given index on top of the stack.
Definition: context.cpp:858
lua_Number to_number(int idx)
Retrieve stack value as number.
Definition: context.cpp:1110
std::string type_name(int idx)
Get name of type of value at a given index.
Definition: context.cpp:909
void push_vfstring(const char *format, va_list arg)
Push formatted string on top of stack.
Definition: context.cpp:870
void remove_global(const char *name)
Remove global variable.
Definition: context.cpp:1079
void set_global(const char *name)
Set a global value.
Definition: context.cpp:991
bool is_thread(int idx)
Check if stack value is a thread.
Definition: context.cpp:1260
void * to_usertype(int idx)
Retrieve stack value as a tolua++ user type.
Definition: context.cpp:1170
void setfenv(int idx=-2)
Set function environment.
Definition: context.cpp:1281
void push_nil()
Push nil on top of stack.
Definition: context.cpp:819
void setup_fam(bool auto_restart, bool conc_thread)
Setup file alteration monitor.
Definition: context.cpp:125
void get_global(const char *name)
Get global variable.
Definition: context.cpp:1068
void remove(int idx)
Remove value from stack.
Definition: context.cpp:931
bool to_boolean(int idx)
Retrieve stack value as boolean.
Definition: context.cpp:1130
void set_integer(const char *name, lua_Integer value)
Assign integer to global variable.
Definition: context.cpp:734
void lock()
Lock Lua state.
Definition: context.cpp:409
void restart()
Restart Lua.
Definition: context.cpp:285
void process_fam_events()
Process FAM events.
Definition: context.cpp:1355
void add_cpackage_dir(const char *path, bool prefix=false)
Add a Lua C package directory.
Definition: context.cpp:363
void set_start_script(const char *start_script)
Set start script.
Definition: context.cpp:263
void remove_watcher(LuaContextWatcher *watcher)
Remove a context watcher.
Definition: context.cpp:1328
void raw_set(int idx)
Set value without invoking meta methods.
Definition: context.cpp:1026
void set_field(const char *key, int t_index=-2)
Set field of a table.
Definition: context.cpp:980
void push_lstring(const char *s, size_t len)
Push substring on top of stack.
Definition: context.cpp:810
void push_light_user_data(void *p)
Push light user data on top of stack.
Definition: context.cpp:799
const char * to_string(int idx)
Retrieve stack value as string.
Definition: context.cpp:1140
void push_usertype(void *data, const char *type_name, const char *name_space=0)
Push usertype on top of stack.
Definition: context.cpp:882
RefPtr< FileAlterationMonitor > get_fam() const
Get file alteration monitor.
Definition: context.cpp:143
void push_thread()
Push thread on top of stack.
Definition: context.cpp:848
lua_Integer to_integer(int idx)
Retrieve stack value as integer.
Definition: context.cpp:1120
bool is_cfunction(int idx)
Check if stack value is a C function.
Definition: context.cpp:1190
lua_State * get_lua_state()
Get Lua state.
Definition: context.cpp:402
void set_usertype(const char *name, void *data, const char *type_name, const char *name_space=0)
Assign usertype to global variable.
Definition: context.cpp:661
void raw_geti(int idx, int n)
Get indexed value without invoking meta methods.
Definition: context.cpp:1059
void push_boolean(bool value)
Push boolean on top of stack.
Definition: context.cpp:765
void * to_userdata(int idx)
Retrieve stack value as userdata.
Definition: context.cpp:1150
void push_integer(lua_Integer value)
Push integer on top of stack.
Definition: context.cpp:789
void raw_seti(int idx, int n)
Set indexed value without invoking meta methods.
Definition: context.cpp:1038
void add_package_dir(const char *path, bool prefix=false)
Add a Lua package directory.
Definition: context.cpp:330
void set_cfunction(const char *name, lua_CFunction f)
Assign cfunction to global variable.
Definition: context.cpp:750
void do_file(const char *filename)
Execute file.
Definition: context.cpp:434
void push_number(lua_Number value)
Push number on top of stack.
Definition: context.cpp:829
bool is_boolean(int idx)
Check if stack value is a boolean.
Definition: context.cpp:1180
void create_table(int narr=0, int nrec=0)
Create a table on top of the stack.
Definition: context.cpp:954
void pop(int n)
Pop value(s) from stack.
Definition: context.cpp:918
void set_number(const char *name, lua_Number value)
Assign number to global variable.
Definition: context.cpp:718
void set_boolean(const char *name, bool value)
Assign boolean to global variable.
Definition: context.cpp:702
void unlock()
Unlock Lua state.
Definition: context.cpp:425
~LuaContext()
Destructor.
Definition: context.cpp:95
LuaContext(bool enable_tracebacks=true)
Constructor.
Definition: context.cpp:63
void * to_pointer(int idx)
Retrieve stack value as pointer.
Definition: context.cpp:1160
void add_watcher(LuaContextWatcher *watcher)
Add a context watcher.
Definition: context.cpp:1319
void pcall(int nargs=0, int nresults=0, int errfunc=0)
Protected call.
Definition: context.cpp:605
void do_string(const char *format,...)
Execute string.
Definition: context.cpp:532
bool is_string(int idx)
Check if stack value is a string.
Definition: context.cpp:1240
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: context.cpp:1362
void set_string(const char *name, const char *value)
Assign string to global variable.
Definition: context.cpp:686
bool try_lock()
Try to lock the Lua state.
Definition: context.cpp:418
void push_fstring(const char *format,...)
Push formatted string on top of stack.
Definition: context.cpp:776
void set_table(int t_index=-3)
Set value of a table.
Definition: context.cpp:966
void set_finalization_calls(std::string finalize, std::string finalize_prepare, std::string finalize_cancel)
Set code to execute during finalization.
Definition: context.cpp:1344
void add_package(const char *package)
Add a default package.
Definition: context.cpp:386
void raw_get(int idx)
Get value without invoking meta methods.
Definition: context.cpp:1048
bool table_next(int idx)
Iterate to next entry of table.
Definition: context.cpp:1100
size_t objlen(int idx)
Get object length.
Definition: context.cpp:1270
void load_string(const char *s)
Load Lua string.
Definition: context.cpp:580
int stack_size()
Get size of stack.
Definition: context.cpp:944
void get_table(int idx)
Get value from table.
Definition: context.cpp:1004
bool is_table(int idx)
Check if stack value is a table.
Definition: context.cpp:1250
void push_cfunction(lua_CFunction f)
Push C function on top of stack.
Definition: context.cpp:898
void get_field(int idx, const char *k)
Get named value from table.
Definition: context.cpp:1016
Mutex mutual exclusion lock.
Definition: mutex.h:33
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.