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