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 <utils/system/fam.h> 00029 00030 #include <lua.hpp> 00031 00032 #include <map> 00033 #include <utility> 00034 #include <list> 00035 #include <string> 00036 00037 namespace fawkes { 00038 #if 0 /* just to make Emacs auto-indent happy */ 00039 } 00040 #endif 00041 00042 class LuaContextWatcher; 00043 class Mutex; 00044 00045 class LuaContext : public FamListener 00046 { 00047 public: 00048 LuaContext(bool watch_dirs = true, bool enable_tracebacks = true); 00049 LuaContext(lua_State *L); 00050 ~LuaContext(); 00051 00052 void set_start_script(const char *start_script); 00053 00054 void restart(); 00055 00056 void add_package_dir(const char *path); 00057 void add_cpackage_dir(const char *path); 00058 void add_package(const char *package); 00059 00060 lua_State * get_lua_state(); 00061 00062 void lock(); 00063 bool try_lock(); 00064 void unlock(); 00065 00066 void do_file(const char *filename); 00067 void do_string(const char *format, ...); 00068 00069 void load_string(const char *s); 00070 void pcall(int nargs = 0, int nresults = 0, int errfunc = 0); 00071 00072 void set_usertype(const char *name, void *data, const char *type_name, 00073 const char *name_space = 0); 00074 void set_string(const char *name, const char *value); 00075 void set_number(const char *name, lua_Number value); 00076 void set_boolean(const char *name, bool value); 00077 void set_integer(const char *name, lua_Integer value); 00078 void remove_global(const char *name); 00079 void set_global(const char *name); 00080 00081 void push_boolean(bool value); 00082 void push_fstring(const char *format, ...); 00083 void push_integer(lua_Integer value); 00084 void push_light_user_data(void *p); 00085 void push_lstring(const char *s, size_t len); 00086 void push_nil(); 00087 void push_number(lua_Number value); 00088 void push_string(const char *value); 00089 void push_thread(); 00090 void push_value(int idx); 00091 void push_vfstring(const char *format, va_list arg); 00092 void push_usertype(void *data, const char *type_name, const char *name_space = 0); 00093 00094 void pop(int n); 00095 void remove(int idx); 00096 int stack_size(); 00097 00098 void create_table(int narr = 0, int nrec = 0); 00099 void set_table(int t_index = -3); 00100 void set_field(const char *key, int t_index = -2); 00101 00102 void get_table(int idx); 00103 void get_field(int idx, const char *k); 00104 void get_global(const char *name); 00105 00106 void raw_set(int idx); 00107 void raw_seti(int idx, int n); 00108 void raw_get(int idx); 00109 void raw_geti(int idx, int n); 00110 00111 lua_Number to_number(int idx); 00112 lua_Integer to_integer(int idx); 00113 bool to_boolean(int idx); 00114 const char * to_string(int idx); 00115 00116 bool is_boolean(int idx); 00117 bool is_cfunction(int idx); 00118 bool is_function(int idx); 00119 bool is_light_user_data(int idx); 00120 bool is_nil(int idx); 00121 bool is_number(int idx); 00122 bool is_string(int idx); 00123 bool is_table(int idx); 00124 bool is_thread(int idx); 00125 00126 size_t objlen(int idx); 00127 void setfenv(int idx = -2); 00128 00129 void add_watcher(LuaContextWatcher *watcher); 00130 void remove_watcher(LuaContextWatcher *watcher); 00131 00132 /* from FamListener */ 00133 virtual void fam_event(const char *filename, unsigned int mask); 00134 void process_fam_events(); 00135 00136 00137 private: 00138 lua_State * init_state(); 00139 void do_string(lua_State *L, const char *format, ...); 00140 void do_file(lua_State *L, const char *s); 00141 void assert_unique_name(const char *name, std::string type); 00142 00143 private: 00144 lua_State *__L; 00145 bool __owns_L; 00146 bool __enable_tracebacks; 00147 00148 Mutex *__lua_mutex; 00149 char *__start_script; 00150 00151 std::list<std::string> __package_dirs; 00152 std::list<std::string> __cpackage_dirs; 00153 std::list<std::string> __packages; 00154 std::list<std::string>::iterator __slit; 00155 00156 std::map<std::string, std::pair<void *, std::string> > __usertypes; 00157 std::map<std::string, std::pair<void *, std::string> >::iterator __utit; 00158 std::map<std::string, std::string> __strings; 00159 std::map<std::string, std::string>::iterator __strings_it; 00160 std::map<std::string, bool> __booleans; 00161 std::map<std::string, bool>::iterator __booleans_it; 00162 std::map<std::string, lua_Number> __numbers; 00163 std::map<std::string, lua_Number>::iterator __numbers_it; 00164 std::map<std::string, lua_Integer> __integers; 00165 std::map<std::string, lua_Integer>::iterator __integers_it; 00166 00167 FileAlterationMonitor *__fam; 00168 00169 LockList<LuaContextWatcher *> __watchers; 00170 00171 }; 00172 00173 } // end of namespace fawkes 00174 00175 #endif