# 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: 27536 $
    00027  * $Author: rjongbloed $
    00028  * $Date: 2012-04-26 02:52:42 -0500 (Thu, 26 Apr 2012) $
    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