Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * context.h - Fawkes Lua Context 00004 * 00005 * Created: Fri May 23 11:29:01 2008 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #ifndef __LUA_CONTEXT_H_ 00024 #define __LUA_CONTEXT_H_ 00025 00026 #include <lua/exceptions.h> 00027 #include <core/utils/lock_list.h> 00028 #include <core/utils/refptr.h> 00029 #include <utils/system/fam.h> 00030 #include <utils/system/fam_thread.h> 00031 00032 #include <lua.hpp> 00033 00034 #include <map> 00035 #include <utility> 00036 #include <list> 00037 #include <string> 00038 00039 namespace fawkes { 00040 #if 0 /* just to make Emacs auto-indent happy */ 00041 } 00042 #endif 00043 00044 class LuaContextWatcher; 00045 class Mutex; 00046 00047 class LuaContext : public FamListener 00048 { 00049 public: 00050 LuaContext(bool enable_tracebacks = true); 00051 LuaContext(lua_State *L); 00052 ~LuaContext(); 00053 00054 void setup_fam(bool auto_restart, bool conc_thread); 00055 RefPtr<FileAlterationMonitor> get_fam() const; 00056 00057 void set_start_script(const char *start_script); 00058 00059 void restart(); 00060 00061 void add_package_dir(const char *path); 00062 void add_cpackage_dir(const char *path); 00063 void add_package(const char *package); 00064 00065 lua_State * get_lua_state(); 00066 00067 void lock(); 00068 bool try_lock(); 00069 void unlock(); 00070 00071 void do_file(const char *filename); 00072 void do_string(const char *format, ...); 00073 00074 void load_string(const char *s); 00075 void pcall(int nargs = 0, int nresults = 0, int errfunc = 0); 00076 00077 void set_usertype(const char *name, void *data, const char *type_name, 00078 const char *name_space = 0); 00079 void set_string(const char *name, const char *value); 00080 void set_number(const char *name, lua_Number value); 00081 void set_boolean(const char *name, bool value); 00082 void set_integer(const char *name, lua_Integer value); 00083 void set_cfunction(const char *name, lua_CFunction f); 00084 void remove_global(const char *name); 00085 void set_global(const char *name); 00086 00087 void push_boolean(bool value); 00088 void push_fstring(const char *format, ...); 00089 void push_integer(lua_Integer value); 00090 void push_light_user_data(void *p); 00091 void push_lstring(const char *s, size_t len); 00092 void push_nil(); 00093 void push_number(lua_Number value); 00094 void push_string(const char *value); 00095 void push_thread(); 00096 void push_value(int idx); 00097 void push_vfstring(const char *format, va_list arg); 00098 void push_usertype(void *data, const char *type_name, 00099 const char *name_space = 0); 00100 void push_cfunction(lua_CFunction f); 00101 00102 void pop(int n); 00103 void remove(int idx); 00104 int stack_size(); 00105 00106 void create_table(int narr = 0, int nrec = 0); 00107 void set_table(int t_index = -3); 00108 void set_field(const char *key, int t_index = -2); 00109 00110 void get_table(int idx); 00111 void get_field(int idx, const char *k); 00112 void get_global(const char *name); 00113 00114 void raw_set(int idx); 00115 void raw_seti(int idx, int n); 00116 void raw_get(int idx); 00117 void raw_geti(int idx, int n); 00118 00119 lua_Number to_number(int idx); 00120 lua_Integer to_integer(int idx); 00121 bool to_boolean(int idx); 00122 const char * to_string(int idx); 00123 00124 bool is_boolean(int idx); 00125 bool is_cfunction(int idx); 00126 bool is_function(int idx); 00127 bool is_light_user_data(int idx); 00128 bool is_nil(int idx); 00129 bool is_number(int idx); 00130 bool is_string(int idx); 00131 bool is_table(int idx); 00132 bool is_thread(int idx); 00133 00134 size_t objlen(int idx); 00135 void setfenv(int idx = -2); 00136 00137 void add_watcher(LuaContextWatcher *watcher); 00138 void remove_watcher(LuaContextWatcher *watcher); 00139 00140 /* from FamListener */ 00141 virtual void fam_event(const char *filename, unsigned int mask); 00142 void process_fam_events(); 00143 00144 00145 private: 00146 lua_State * init_state(); 00147 void do_string(lua_State *L, const char *format, ...); 00148 void do_file(lua_State *L, const char *s); 00149 void assert_unique_name(const char *name, std::string type); 00150 00151 private: 00152 lua_State *__L; 00153 bool __owns_L; 00154 bool __enable_tracebacks; 00155 00156 Mutex *__lua_mutex; 00157 char *__start_script; 00158 00159 std::list<std::string> __package_dirs; 00160 std::list<std::string> __cpackage_dirs; 00161 std::list<std::string> __packages; 00162 std::list<std::string>::iterator __slit; 00163 00164 std::map<std::string, std::pair<void *, std::string> > __usertypes; 00165 std::map<std::string, std::pair<void *, std::string> >::iterator __utit; 00166 std::map<std::string, std::string> __strings; 00167 std::map<std::string, std::string>::iterator __strings_it; 00168 std::map<std::string, bool> __booleans; 00169 std::map<std::string, bool>::iterator __booleans_it; 00170 std::map<std::string, lua_Number> __numbers; 00171 std::map<std::string, lua_Number>::iterator __numbers_it; 00172 std::map<std::string, lua_Integer> __integers; 00173 std::map<std::string, lua_Integer>::iterator __integers_it; 00174 std::map<std::string, lua_CFunction> __cfuncs; 00175 std::map<std::string, lua_CFunction>::iterator __cfuncs_it; 00176 00177 RefPtr<FileAlterationMonitor> __fam; 00178 FamThread *__fam_thread; 00179 00180 LockList<LuaContextWatcher *> __watchers; 00181 00182 }; 00183 00184 } // end of namespace fawkes 00185 00186 #endif