lua.h

Go to the documentation of this file.
00001 /*
00002  * lua.h
00003  *
00004  * Interface library for Lua interpreter
00005  *
00006  * Portable Tools Library]
00007  *
00008  * Copyright (C) 2010 by Post Increment
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Tools Library.
00021  *
00022  * The Initial Developer of the Original Code is Post Increment
00023  *
00024  * Contributor(s): Craig Southeren
00025  *
00026  * $Revision$
00027  * $Author$
00028  * $Date$
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 
00066     typedef int (*CFunction)(lua_State *L);
00067     virtual void SetFunction(const char * name, CFunction func);
00068 
00069     bool CallLuaFunction(const char * name);
00070     bool CallLuaFunction(const char * name, const char * sig, ...);
00071 
00072     static int TraceFunction(lua_State * L);
00073 
00074     PString GetLastErrorText() const 
00075     { return m_lastErrorText; }
00076 
00077     void BindToInstanceStart(const char * instanceName);
00078     void BindToInstanceFunc(const char * lua_name, void * obj, CFunction func);
00079     void BindToInstanceEnd(const char * instanceName);
00080 
00081     static void * GetInstance(lua_State * L);
00082 
00083   protected:
00084     lua_State * m_lua;
00085     PString m_lastErrorText;
00086 };
00087 
00088 #define PLUA_BINDING_START(class_type) \
00089   typedef class_type PLua_InstanceType; \
00090   void UnbindFromInstance(PLua &, const char *) { } \
00091   void BindToInstance(PLua & lua, const char * instanceName) \
00092   { \
00093     lua.BindToInstanceStart(instanceName);
00094 
00095 #define PLUA_BINDING2(cpp_name, lua_name) \
00096     lua.BindToInstanceFunc(lua_name, (void *)this, &PLua_InstanceType::cpp_name##_callback);
00097 
00098 #define PLUA_BINDING(fn_name) \
00099   PLUA_BINDING2(fn_name, #fn_name)
00100 
00101 #define PLUA_BINDING_END() \
00102     lua.BindToInstanceEnd(instanceName); \
00103   }
00104 
00105 #define PLUA_FUNCTION_DECL(fn_name) \
00106   static int fn_name##_callback(lua_State * L) \
00107   { \
00108     return ((PLua_InstanceType *)PLua::GetInstance(L))->fn_name(L); \
00109   }
00110 
00111 #define PLUA_FUNCTION(fn_name) \
00112   PLUA_FUNCTION_DECL(fn_name) \
00113   int fn_name(lua_State * L) \
00114 
00115 #define PLUA_FUNCTION_NOARGS(fn_name) \
00116   PLUA_FUNCTION_DECL(fn_name) \
00117   int fn_name(lua_State *) \
00118 
00119 #define PLUA_DECLARE_FUNCTION(fn_name) \
00120   PLUA_FUNCTION_DECL(fn_name) \
00121   int fn_name(lua_State * L); \
00122 
00123 
00125 
00126 #endif // P_LUA
00127 
00128 #endif  // PTLIB_LUA_H
00129 

Generated on Fri Oct 14 01:44:09 2011 for PTLib by  doxygen 1.4.7