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 <lua/exceptions.h>
27 #include <core/utils/lock_list.h>
28 #include <core/utils/refptr.h>
29 #include <utils/system/fam.h>
30 #include <utils/system/fam_thread.h>
31 
32 #include <lua.hpp>
33 
34 #include <map>
35 #include <utility>
36 #include <list>
37 #include <string>
38 
39 namespace fawkes {
40 #if 0 /* just to make Emacs auto-indent happy */
41 }
42 #endif
43 
44 class LuaContextWatcher;
45 class Mutex;
46 
47 class LuaContext : public FamListener
48 {
49  public:
50  LuaContext(bool enable_tracebacks = true);
51  LuaContext(lua_State *L);
52  ~LuaContext();
53 
54  void setup_fam(bool auto_restart, bool conc_thread);
56 
57  void set_start_script(const char *start_script);
58  void set_finalization_calls(std::string finalize,
59  std::string finalize_prepare,
60  std::string finalize_cancel);
61 
62  void restart();
63 
64  void add_package_dir(const char *path, bool prefix = false);
65  void add_cpackage_dir(const char *path, bool prefix = false);
66  void add_package(const char *package);
67 
68  lua_State * get_lua_state();
69 
70  void lock();
71  bool try_lock();
72  void unlock();
73 
74  void do_file(const char *filename);
75  void do_string(const char *format, ...);
76 
77  void load_string(const char *s);
78  void pcall(int nargs = 0, int nresults = 0, int errfunc = 0);
79 
80  void set_usertype(const char *name, void *data, const char *type_name,
81  const char *name_space = 0);
82  void set_string(const char *name, const char *value);
83  void set_number(const char *name, lua_Number value);
84  void set_boolean(const char *name, bool value);
85  void set_integer(const char *name, lua_Integer value);
86  void set_cfunction(const char *name, lua_CFunction f);
87  void remove_global(const char *name);
88  void set_global(const char *name);
89 
90  void push_boolean(bool value);
91  void push_fstring(const char *format, ...);
92  void push_integer(lua_Integer value);
93  void push_light_user_data(void *p);
94  void push_lstring(const char *s, size_t len);
95  void push_nil();
96  void push_number(lua_Number value);
97  void push_string(const char *value);
98  void push_thread();
99  void push_value(int idx);
100  void push_vfstring(const char *format, va_list arg);
101  void push_usertype(void *data, const char *type_name,
102  const char *name_space = 0);
103  void push_cfunction(lua_CFunction f);
104 
105  std::string type_name(int idx);
106 
107  void pop(int n);
108  void remove(int idx);
109  int stack_size();
110 
111  void create_table(int narr = 0, int nrec = 0);
112  void set_table(int t_index = -3);
113  void set_field(const char *key, int t_index = -2);
114 
115  void get_table(int idx);
116  void get_field(int idx, const char *k);
117  void get_global(const char *name);
118 
119  bool table_next(int idx);
120 
121  void raw_set(int idx);
122  void raw_seti(int idx, int n);
123  void raw_get(int idx);
124  void raw_geti(int idx, int n);
125 
126  lua_Number to_number(int idx);
127  lua_Integer to_integer(int idx);
128  bool to_boolean(int idx);
129  const char * to_string(int idx);
130  void * to_userdata(int idx);
131  void * to_pointer(int idx);
132  void * to_usertype(int idx);
133 
134  bool is_boolean(int idx);
135  bool is_cfunction(int idx);
136  bool is_function(int idx);
137  bool is_light_user_data(int idx);
138  bool is_nil(int idx);
139  bool is_number(int idx);
140  bool is_string(int idx);
141  bool is_table(int idx);
142  bool is_thread(int idx);
143 
144  size_t objlen(int idx);
145  void setfenv(int idx = -2);
146 
147  void add_watcher(LuaContextWatcher *watcher);
148  void remove_watcher(LuaContextWatcher *watcher);
149 
150  /* from FamListener */
151  virtual void fam_event(const char *filename, unsigned int mask);
152  void process_fam_events();
153 
154 
155  private:
156  lua_State * init_state();
157  void do_string(lua_State *L, const char *format, ...);
158  void do_file(lua_State *L, const char *s);
159  void assert_unique_name(const char *name, std::string type);
160 
161 
162  private:
163  lua_State *__L;
164  bool __owns_L;
165  bool __enable_tracebacks;
166 
167  Mutex *__lua_mutex;
168  char *__start_script;
169 
170  std::list<std::string> __package_dirs;
171  std::list<std::string> __cpackage_dirs;
172  std::list<std::string> __packages;
173  std::list<std::string>::iterator __slit;
174 
175  std::map<std::string, std::pair<void *, std::string> > __usertypes;
176  std::map<std::string, std::pair<void *, std::string> >::iterator __utit;
177  std::map<std::string, std::string> __strings;
178  std::map<std::string, std::string>::iterator __strings_it;
179  std::map<std::string, bool> __booleans;
180  std::map<std::string, bool>::iterator __booleans_it;
181  std::map<std::string, lua_Number> __numbers;
182  std::map<std::string, lua_Number>::iterator __numbers_it;
183  std::map<std::string, lua_Integer> __integers;
184  std::map<std::string, lua_Integer>::iterator __integers_it;
185  std::map<std::string, lua_CFunction> __cfuncs;
186  std::map<std::string, lua_CFunction>::iterator __cfuncs_it;
187 
188  std::string __finalize_call;
189  std::string __finalize_prepare_call;
190  std::string __finalize_cancel_call;
191 
193  FamThread *__fam_thread;
194 
196 
197 };
198 
199 } // end of namespace fawkes
200 
201 #endif
void restart()
Restart Lua.
Definition: context.cpp:288
void push_thread()
Push thread on top of stack.
Definition: context.cpp:888
void push_light_user_data(void *p)
Push light user data on top of stack.
Definition: context.cpp:834
void setup_fam(bool auto_restart, bool conc_thread)
Setup file alteration monitor.
Definition: context.cpp:128
void push_vfstring(const char *format, va_list arg)
Push formatted string on top of stack.
Definition: context.cpp:912
bool is_light_user_data(int idx)
Check if stack value is light user data.
Definition: context.cpp:1280
Lua context watcher.
bool is_number(int idx)
Check if stack value is a number.
Definition: context.cpp:1302
void push_integer(lua_Integer value)
Push integer on top of stack.
Definition: context.cpp:823
void raw_geti(int idx, int n)
Get indexed value without invoking meta methods.
Definition: context.cpp:1116
Fawkes library namespace.
bool is_thread(int idx)
Check if stack value is a thread.
Definition: context.cpp:1335
void pop(int n)
Pop value(s) from stack.
Definition: context.cpp:964
void push_lstring(const char *s, size_t len)
Push substring on top of stack.
Definition: context.cpp:846
void set_cfunction(const char *name, lua_CFunction f)
Assign cfunction to global variable.
Definition: context.cpp:781
void raw_seti(int idx, int n)
Set indexed value without invoking meta methods.
Definition: context.cpp:1093
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: context.cpp:1443
void load_string(const char *s)
Load Lua string.
Definition: context.cpp:601
void add_package_dir(const char *path, bool prefix=false)
Add a Lua package directory.
Definition: context.cpp:331
void remove_watcher(LuaContextWatcher *watcher)
Remove a context watcher.
Definition: context.cpp:1406
bool is_cfunction(int idx)
Check if stack value is a C function.
Definition: context.cpp:1258
bool try_lock()
Try to lock the Lua state.
Definition: context.cpp:418
void set_field(const char *key, int t_index=-2)
Set field of a table.
Definition: context.cpp:1030
void set_table(int t_index=-3)
Set value of a table.
Definition: context.cpp:1015
void add_package(const char *package)
Add a default package.
Definition: context.cpp:383
void unlock()
Unlock Lua state.
Definition: context.cpp:426
void push_value(int idx)
Push a copy of the element at the given index on top of the stack.
Definition: context.cpp:899
void push_nil()
Push nil on top of stack.
Definition: context.cpp:856
int stack_size()
Get size of stack.
Definition: context.cpp:991
void pcall(int nargs=0, int nresults=0, int errfunc=0)
Protected call.
Definition: context.cpp:629
bool is_nil(int idx)
Check if stack value is nil.
Definition: context.cpp:1291
lua_Number to_number(int idx)
Retrieve stack value as number.
Definition: context.cpp:1171
void push_cfunction(lua_CFunction f)
Push C function on top of stack.
Definition: context.cpp:943
void do_string(const char *format,...)
Execute string.
Definition: context.cpp:548
void do_file(const char *filename)
Execute file.
Definition: context.cpp:436
void set_number(const char *name, lua_Number value)
Assign number to global variable.
Definition: context.cpp:747
LuaContext(bool enable_tracebacks=true)
Constructor.
Definition: context.cpp:66
void setfenv(int idx=-2)
Set function environment.
Definition: context.cpp:1358
List with a lock.
Definition: thread.h:40
Lua C++ wrapper.
Definition: context.h:47
void get_field(int idx, const char *k)
Get named value from table.
Definition: context.cpp:1069
File Alteration Monitor Listener.
Definition: fam.h:35
void set_integer(const char *name, lua_Integer value)
Assign integer to global variable.
Definition: context.cpp:764
lua_Integer to_integer(int idx)
Retrieve stack value as integer.
Definition: context.cpp:1182
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:689
void push_usertype(void *data, const char *type_name, const char *name_space=0)
Push usertype on top of stack.
Definition: context.cpp:925
void set_string(const char *name, const char *value)
Assign string to global variable.
Definition: context.cpp:713
void push_string(const char *value)
Push string on top of stack.
Definition: context.cpp:878
bool is_table(int idx)
Check if stack value is a table.
Definition: context.cpp:1324
void set_finalization_calls(std::string finalize, std::string finalize_prepare, std::string finalize_cancel)
Set code to execute during finalization.
Definition: context.cpp:1423
void process_fam_events()
Process FAM events.
Definition: context.cpp:1436
void set_boolean(const char *name, bool value)
Assign boolean to global variable.
Definition: context.cpp:730
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
bool is_string(int idx)
Check if stack value is a string.
Definition: context.cpp:1313
void get_global(const char *name)
Get global variable.
Definition: context.cpp:1126
void push_boolean(bool value)
Push boolean on top of stack.
Definition: context.cpp:797
void add_cpackage_dir(const char *path, bool prefix=false)
Add a Lua C package directory.
Definition: context.cpp:360
void set_start_script(const char *start_script)
Set start script.
Definition: context.cpp:266
bool is_function(int idx)
Check if stack value is a function.
Definition: context.cpp:1269
bool to_boolean(int idx)
Retrieve stack value as boolean.
Definition: context.cpp:1193
bool table_next(int idx)
Iterate to next entry of table.
Definition: context.cpp:1160
lua_State * get_lua_state()
Get Lua state.
Definition: context.cpp:400
void get_table(int idx)
Get value from table.
Definition: context.cpp:1056
FileAlterationMonitor thread wrapper.
Definition: fam_thread.h:35
~LuaContext()
Destructor.
Definition: context.cpp:98
void lock()
Lock Lua state.
Definition: context.cpp:408
void remove_global(const char *name)
Remove global variable.
Definition: context.cpp:1138
Mutex mutual exclusion lock.
Definition: mutex.h:32
void set_global(const char *name)
Set a global value.
Definition: context.cpp:1042
void * to_pointer(int idx)
Retrieve stack value as pointer.
Definition: context.cpp:1225
void create_table(int narr=0, int nrec=0)
Create a table on top of the stack.
Definition: context.cpp:1002
void push_number(lua_Number value)
Push number on top of stack.
Definition: context.cpp:867
void * to_userdata(int idx)
Retrieve stack value as userdata.
Definition: context.cpp:1214
void * to_usertype(int idx)
Retrieve stack value as a tolua++ user type.
Definition: context.cpp:1236
const char * to_string(int idx)
Retrieve stack value as string.
Definition: context.cpp:1204
void raw_get(int idx)
Get value without invoking meta methods.
Definition: context.cpp:1104
size_t objlen(int idx)
Get object length.
Definition: context.cpp:1346
std::string type_name(int idx)
Get name of type of value at a given index.
Definition: context.cpp:955
RefPtr< FileAlterationMonitor > get_fam() const
Get file alteration monitor.
Definition: context.cpp:147
void add_watcher(LuaContextWatcher *watcher)
Add a context watcher.
Definition: context.cpp:1396
bool is_boolean(int idx)
Check if stack value is a boolean.
Definition: context.cpp:1247
void push_fstring(const char *format,...)
Push formatted string on top of stack.
Definition: context.cpp:809
void raw_set(int idx)
Set value without invoking meta methods.
Definition: context.cpp:1080