Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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);
55  RefPtr<FileAlterationMonitor> get_fam() const;
56 
57  void set_start_script(const char *start_script);
58 
59  void restart();
60 
61  void add_package_dir(const char *path);
62  void add_cpackage_dir(const char *path);
63  void add_package(const char *package);
64 
65  lua_State * get_lua_state();
66 
67  void lock();
68  bool try_lock();
69  void unlock();
70 
71  void do_file(const char *filename);
72  void do_string(const char *format, ...);
73 
74  void load_string(const char *s);
75  void pcall(int nargs = 0, int nresults = 0, int errfunc = 0);
76 
77  void set_usertype(const char *name, void *data, const char *type_name,
78  const char *name_space = 0);
79  void set_string(const char *name, const char *value);
80  void set_number(const char *name, lua_Number value);
81  void set_boolean(const char *name, bool value);
82  void set_integer(const char *name, lua_Integer value);
83  void set_cfunction(const char *name, lua_CFunction f);
84  void remove_global(const char *name);
85  void set_global(const char *name);
86 
87  void push_boolean(bool value);
88  void push_fstring(const char *format, ...);
89  void push_integer(lua_Integer value);
90  void push_light_user_data(void *p);
91  void push_lstring(const char *s, size_t len);
92  void push_nil();
93  void push_number(lua_Number value);
94  void push_string(const char *value);
95  void push_thread();
96  void push_value(int idx);
97  void push_vfstring(const char *format, va_list arg);
98  void push_usertype(void *data, const char *type_name,
99  const char *name_space = 0);
100  void push_cfunction(lua_CFunction f);
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  void raw_set(int idx);
115  void raw_seti(int idx, int n);
116  void raw_get(int idx);
117  void raw_geti(int idx, int n);
118 
119  lua_Number to_number(int idx);
120  lua_Integer to_integer(int idx);
121  bool to_boolean(int idx);
122  const char * to_string(int idx);
123 
124  bool is_boolean(int idx);
125  bool is_cfunction(int idx);
126  bool is_function(int idx);
127  bool is_light_user_data(int idx);
128  bool is_nil(int idx);
129  bool is_number(int idx);
130  bool is_string(int idx);
131  bool is_table(int idx);
132  bool is_thread(int idx);
133 
134  size_t objlen(int idx);
135  void setfenv(int idx = -2);
136 
137  void add_watcher(LuaContextWatcher *watcher);
138  void remove_watcher(LuaContextWatcher *watcher);
139 
140  /* from FamListener */
141  virtual void fam_event(const char *filename, unsigned int mask);
142  void process_fam_events();
143 
144 
145  private:
146  lua_State * init_state();
147  void do_string(lua_State *L, const char *format, ...);
148  void do_file(lua_State *L, const char *s);
149  void assert_unique_name(const char *name, std::string type);
150 
151  private:
152  lua_State *__L;
153  bool __owns_L;
154  bool __enable_tracebacks;
155 
156  Mutex *__lua_mutex;
157  char *__start_script;
158 
159  std::list<std::string> __package_dirs;
160  std::list<std::string> __cpackage_dirs;
161  std::list<std::string> __packages;
162  std::list<std::string>::iterator __slit;
163 
164  std::map<std::string, std::pair<void *, std::string> > __usertypes;
165  std::map<std::string, std::pair<void *, std::string> >::iterator __utit;
166  std::map<std::string, std::string> __strings;
167  std::map<std::string, std::string>::iterator __strings_it;
168  std::map<std::string, bool> __booleans;
169  std::map<std::string, bool>::iterator __booleans_it;
170  std::map<std::string, lua_Number> __numbers;
171  std::map<std::string, lua_Number>::iterator __numbers_it;
172  std::map<std::string, lua_Integer> __integers;
173  std::map<std::string, lua_Integer>::iterator __integers_it;
174  std::map<std::string, lua_CFunction> __cfuncs;
175  std::map<std::string, lua_CFunction>::iterator __cfuncs_it;
176 
178  FamThread *__fam_thread;
179 
181 
182 };
183 
184 } // end of namespace fawkes
185 
186 #endif
Lua context watcher.
List with a lock.
Definition: thread.h:40
Lua C++ wrapper.
Definition: context.h:47
File Alteration Monitor Listener.
Definition: fam.h:35
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
FileAlterationMonitor thread wrapper.
Definition: fam_thread.h:35
Mutex mutual exclusion lock.
Definition: mutex.h:32