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 
38 namespace fawkes {
39 
40 class LuaContextWatcher;
41 class Mutex;
42 
43 class LuaContext : public FamListener
44 {
45 public:
46  LuaContext(bool enable_tracebacks = true);
47  LuaContext(lua_State *L);
48  ~LuaContext();
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 
149 private:
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 
155 private:
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
fawkes::LuaContext::objlen
size_t objlen(int idx)
Get object length.
Definition: context.cpp:1270
fawkes::LuaContext::push_value
void push_value(int idx)
Push a copy of the element at the given index on top of the stack.
Definition: context.cpp:858
fawkes::LuaContext::set_table
void set_table(int t_index=-3)
Set value of a table.
Definition: context.cpp:966
fawkes::LuaContext::LuaContext
LuaContext(bool enable_tracebacks=true)
Constructor.
Definition: context.cpp:63
fawkes::LuaContext::push_light_user_data
void push_light_user_data(void *p)
Push light user data on top of stack.
Definition: context.cpp:799
fawkes::LuaContext::is_number
bool is_number(int idx)
Check if stack value is a number.
Definition: context.cpp:1230
fawkes::LuaContext::push_usertype
void push_usertype(void *data, const char *type_name, const char *name_space=0)
Push usertype on top of stack.
Definition: context.cpp:882
fawkes::LuaContext
Lua C++ wrapper.
Definition: context.h:44
fawkes::LuaContext::push_boolean
void push_boolean(bool value)
Push boolean on top of stack.
Definition: context.cpp:765
fawkes::LuaContext::stack_size
int stack_size()
Get size of stack.
Definition: context.cpp:944
fawkes::Mutex
Mutex mutual exclusion lock.
Definition: mutex.h:33
fawkes::LuaContext::set_integer
void set_integer(const char *name, lua_Integer value)
Assign integer to global variable.
Definition: context.cpp:734
fawkes::LuaContext::do_string
void do_string(const char *format,...)
Execute string.
Definition: context.cpp:532
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
fawkes::LuaContext::raw_seti
void raw_seti(int idx, int n)
Set indexed value without invoking meta methods.
Definition: context.cpp:1038
fawkes::LuaContext::push_fstring
void push_fstring(const char *format,...)
Push formatted string on top of stack.
Definition: context.cpp:776
fawkes::LuaContext::set_cfunction
void set_cfunction(const char *name, lua_CFunction f)
Assign cfunction to global variable.
Definition: context.cpp:750
fawkes::LuaContext::set_field
void set_field(const char *key, int t_index=-2)
Set field of a table.
Definition: context.cpp:980
fawkes::FamListener
File Alteration Monitor Listener.
Definition: fam.h:36
fawkes::LuaContext::push_integer
void push_integer(lua_Integer value)
Push integer on top of stack.
Definition: context.cpp:789
fawkes::LuaContext::push_string
void push_string(const char *value)
Push string on top of stack.
Definition: context.cpp:839
fawkes::LuaContext::is_thread
bool is_thread(int idx)
Check if stack value is a thread.
Definition: context.cpp:1260
fawkes::LuaContext::get_global
void get_global(const char *name)
Get global variable.
Definition: context.cpp:1068
fawkes::LuaContext::create_table
void create_table(int narr=0, int nrec=0)
Create a table on top of the stack.
Definition: context.cpp:954
fawkes::LuaContext::is_string
bool is_string(int idx)
Check if stack value is a string.
Definition: context.cpp:1240
fawkes::LockList
List with a lock.
Definition: lock_list.h:45
fawkes::LuaContextWatcher
Lua context watcher.
Definition: context_watcher.h:31
fawkes::LuaContext::~LuaContext
~LuaContext()
Destructor.
Definition: context.cpp:95
fawkes::LuaContext::to_boolean
bool to_boolean(int idx)
Retrieve stack value as boolean.
Definition: context.cpp:1130
fawkes::LuaContext::fam_event
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
Definition: context.cpp:1362
fawkes::LuaContext::set_boolean
void set_boolean(const char *name, bool value)
Assign boolean to global variable.
Definition: context.cpp:702
fawkes::LuaContext::get_lua_state
lua_State * get_lua_state()
Get Lua state.
Definition: context.cpp:402
fawkes::LuaContext::to_pointer
void * to_pointer(int idx)
Retrieve stack value as pointer.
Definition: context.cpp:1160
fawkes::FamThread
FileAlterationMonitor thread wrapper.
Definition: fam_thread.h:33
fawkes::LuaContext::raw_set
void raw_set(int idx)
Set value without invoking meta methods.
Definition: context.cpp:1026
fawkes::LuaContext::to_usertype
void * to_usertype(int idx)
Retrieve stack value as a tolua++ user type.
Definition: context.cpp:1170
fawkes::LuaContext::get_field
void get_field(int idx, const char *k)
Get named value from table.
Definition: context.cpp:1016
fawkes::LuaContext::load_string
void load_string(const char *s)
Load Lua string.
Definition: context.cpp:580
fawkes::LuaContext::get_table
void get_table(int idx)
Get value from table.
Definition: context.cpp:1004
fawkes::LuaContext::push_thread
void push_thread()
Push thread on top of stack.
Definition: context.cpp:848
fawkes::LuaContext::type_name
std::string type_name(int idx)
Get name of type of value at a given index.
Definition: context.cpp:909
fawkes::LuaContext::is_cfunction
bool is_cfunction(int idx)
Check if stack value is a C function.
Definition: context.cpp:1190
fawkes::LuaContext::to_userdata
void * to_userdata(int idx)
Retrieve stack value as userdata.
Definition: context.cpp:1150
fawkes::LuaContext::get_fam
RefPtr< FileAlterationMonitor > get_fam() const
Get file alteration monitor.
Definition: context.cpp:143
fawkes::LuaContext::pcall
void pcall(int nargs=0, int nresults=0, int errfunc=0)
Protected call.
Definition: context.cpp:605
fawkes::LuaContext::to_string
const char * to_string(int idx)
Retrieve stack value as string.
Definition: context.cpp:1140
fawkes::LuaContext::restart
void restart()
Restart Lua.
Definition: context.cpp:285
fawkes::LuaContext::raw_get
void raw_get(int idx)
Get value without invoking meta methods.
Definition: context.cpp:1048
fawkes::LuaContext::push_vfstring
void push_vfstring(const char *format, va_list arg)
Push formatted string on top of stack.
Definition: context.cpp:870
fawkes::LuaContext::setfenv
void setfenv(int idx=-2)
Set function environment.
Definition: context.cpp:1281
fawkes::LuaContext::is_boolean
bool is_boolean(int idx)
Check if stack value is a boolean.
Definition: context.cpp:1180
fawkes
Fawkes library namespace.
fawkes::LuaContext::to_integer
lua_Integer to_integer(int idx)
Retrieve stack value as integer.
Definition: context.cpp:1120
fawkes::LuaContext::pop
void pop(int n)
Pop value(s) from stack.
Definition: context.cpp:918
fawkes::LuaContext::do_file
void do_file(const char *filename)
Execute file.
Definition: context.cpp:434
fawkes::LuaContext::remove
void remove(int idx)
Remove value from stack.
Definition: context.cpp:931
fawkes::LuaContext::is_table
bool is_table(int idx)
Check if stack value is a table.
Definition: context.cpp:1250
fawkes::LuaContext::process_fam_events
void process_fam_events()
Process FAM events.
Definition: context.cpp:1355
fawkes::LuaContext::to_number
lua_Number to_number(int idx)
Retrieve stack value as number.
Definition: context.cpp:1110
fawkes::LuaContext::setup_fam
void setup_fam(bool auto_restart, bool conc_thread)
Setup file alteration monitor.
Definition: context.cpp:125
fawkes::LuaContext::push_number
void push_number(lua_Number value)
Push number on top of stack.
Definition: context.cpp:829
fawkes::LuaContext::set_usertype
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
fawkes::LuaContext::is_light_user_data
bool is_light_user_data(int idx)
Check if stack value is light user data.
Definition: context.cpp:1210
fawkes::LuaContext::set_start_script
void set_start_script(const char *start_script)
Set start script.
Definition: context.cpp:263
fawkes::LuaContext::raw_geti
void raw_geti(int idx, int n)
Get indexed value without invoking meta methods.
Definition: context.cpp:1059
fawkes::LuaContext::set_finalization_calls
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
fawkes::LuaContext::add_package
void add_package(const char *package)
Add a default package.
Definition: context.cpp:386
fawkes::LuaContext::push_lstring
void push_lstring(const char *s, size_t len)
Push substring on top of stack.
Definition: context.cpp:810
fawkes::LuaContext::add_cpackage_dir
void add_cpackage_dir(const char *path, bool prefix=false)
Add a Lua C package directory.
Definition: context.cpp:363
fawkes::LuaContext::is_nil
bool is_nil(int idx)
Check if stack value is nil.
Definition: context.cpp:1220
fawkes::LuaContext::add_package_dir
void add_package_dir(const char *path, bool prefix=false)
Add a Lua package directory.
Definition: context.cpp:330
fawkes::LuaContext::set_string
void set_string(const char *name, const char *value)
Assign string to global variable.
Definition: context.cpp:686
fawkes::LuaContext::set_number
void set_number(const char *name, lua_Number value)
Assign number to global variable.
Definition: context.cpp:718
fawkes::LuaContext::add_watcher
void add_watcher(LuaContextWatcher *watcher)
Add a context watcher.
Definition: context.cpp:1319
fawkes::LuaContext::lock
void lock()
Lock Lua state.
Definition: context.cpp:409
fawkes::LuaContext::push_nil
void push_nil()
Push nil on top of stack.
Definition: context.cpp:819
fawkes::LuaContext::remove_global
void remove_global(const char *name)
Remove global variable.
Definition: context.cpp:1079
fawkes::LuaContext::push_cfunction
void push_cfunction(lua_CFunction f)
Push C function on top of stack.
Definition: context.cpp:898
fawkes::LuaContext::remove_watcher
void remove_watcher(LuaContextWatcher *watcher)
Remove a context watcher.
Definition: context.cpp:1328
fawkes::LuaContext::try_lock
bool try_lock()
Try to lock the Lua state.
Definition: context.cpp:418
fawkes::LuaContext::set_global
void set_global(const char *name)
Set a global value.
Definition: context.cpp:991
fawkes::LuaContext::unlock
void unlock()
Unlock Lua state.
Definition: context.cpp:425
fawkes::LuaContext::is_function
bool is_function(int idx)
Check if stack value is a function.
Definition: context.cpp:1200
fawkes::LuaContext::table_next
bool table_next(int idx)
Iterate to next entry of table.
Definition: context.cpp:1100