00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef PTLIB_LUA_H
00032 #define PTLIB_LUA_H
00033
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037
00038 #include <ptlib.h>
00039 #include <ptbuildopts.h>
00040
00041 #if P_LUA
00042
00043 struct lua_State;
00044
00045
00047
00048 class PLua
00049 {
00050 public:
00051 PLua();
00052 ~PLua();
00053
00054 virtual bool LoadString(const char * text);
00055
00056 virtual bool LoadFile(const char * filename);
00057
00058 virtual bool Run(const char * program = NULL);
00059
00060 virtual void OnError(int code, const PString & str);
00061
00062 operator lua_State * () { return m_lua; }
00063
00064 virtual void SetValue(const char * name, const char * value);
00065 virtual PString GetValue(const char * name);
00066
00067 typedef int (*CFunction)(lua_State *L);
00068 virtual void SetFunction(const char * name, CFunction func);
00069
00070 bool CallLuaFunction(const char * name);
00071 bool CallLuaFunction(const char * name, const char * sig, ...);
00072
00073 static int TraceFunction(lua_State * L);
00074
00075 PString GetLastErrorText() const
00076 { return m_lastErrorText; }
00077
00078 void BindToInstanceStart(const char * instanceName);
00079 void BindToInstanceFunc(const char * lua_name, void * obj, CFunction func);
00080 void BindToInstanceEnd(const char * instanceName);
00081
00082 static void * GetInstance(lua_State * L);
00083
00084 protected:
00085 lua_State * m_lua;
00086 PString m_lastErrorText;
00087 };
00088
00089 #define PLUA_BINDING_START(class_type) \
00090 typedef class_type PLua_InstanceType; \
00091 void UnbindFromInstance(PLua &, const char *) { } \
00092 void BindToInstance(PLua & lua, const char * instanceName) \
00093 { \
00094 lua.BindToInstanceStart(instanceName);
00095
00096 #define PLUA_BINDING2(cpp_name, lua_name) \
00097 lua.BindToInstanceFunc(lua_name, (void *)this, &PLua_InstanceType::cpp_name##_callback);
00098
00099 #define PLUA_BINDING(fn_name) \
00100 PLUA_BINDING2(fn_name, #fn_name)
00101
00102 #define PLUA_BINDING_END() \
00103 lua.BindToInstanceEnd(instanceName); \
00104 }
00105
00106 #define PLUA_FUNCTION_DECL(fn_name) \
00107 static int fn_name##_callback(lua_State * L) \
00108 { \
00109 return ((PLua_InstanceType *)PLua::GetInstance(L))->fn_name(L); \
00110 }
00111
00112 #define PLUA_FUNCTION(fn_name) \
00113 PLUA_FUNCTION_DECL(fn_name) \
00114 int fn_name(lua_State * L) \
00115
00116 #define PLUA_FUNCTION_NOARGS(fn_name) \
00117 PLUA_FUNCTION_DECL(fn_name) \
00118 int fn_name(lua_State *) \
00119
00120 #define PLUA_DECLARE_FUNCTION(fn_name) \
00121 PLUA_FUNCTION_DECL(fn_name) \
00122 int fn_name(lua_State * L); \
00123
00124
00126
00127 #endif // P_LUA
00128
00129 #endif // PTLIB_LUA_H
00130