PTLib  Version 2.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lua.h
Go to the documentation of this file.
1 /*
2  * lua.h
3  *
4  * Interface library for Lua interpreter
5  *
6  * Portable Tools Library]
7  *
8  * Copyright (C) 2010 by Post Increment
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * The Initial Developer of the Original Code is Post Increment
23  *
24  * Contributor(s): Craig Southeren
25  *
26  * $Revision: 26015 $
27  * $Author: rjongbloed $
28  * $Date: 2011-06-14 02:31:10 -0500 (Tue, 14 Jun 2011) $
29  */
30 
31 #ifndef PTLIB_LUA_H
32 #define PTLIB_LUA_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include <ptlib.h>
39 #include <ptbuildopts.h>
40 
41 #if P_LUA
42 
43 struct lua_State;
44 
45 
47 
48 class PLua
49 {
50  public:
51  PLua();
52  ~PLua();
53 
54  virtual bool LoadString(const char * text);
55 
56  virtual bool LoadFile(const char * filename);
57 
58  virtual bool Run(const char * program = NULL);
59 
60  virtual void OnError(int code, const PString & str);
61 
62  operator lua_State * () { return m_lua; }
63 
64  virtual void SetValue(const char * name, const char * value);
65 
66  typedef int (*CFunction)(lua_State *L);
67  virtual void SetFunction(const char * name, CFunction func);
68 
69  bool CallLuaFunction(const char * name);
70  bool CallLuaFunction(const char * name, const char * sig, ...);
71 
72  static int TraceFunction(lua_State * L);
73 
74  PString GetLastErrorText() const
75  { return m_lastErrorText; }
76 
77  void BindToInstanceStart(const char * instanceName);
78  void BindToInstanceFunc(const char * lua_name, void * obj, CFunction func);
79  void BindToInstanceEnd(const char * instanceName);
80 
81  static void * GetInstance(lua_State * L);
82 
83  protected:
84  lua_State * m_lua;
85  PString m_lastErrorText;
86 };
87 
88 #define PLUA_BINDING_START(class_type) \
89  typedef class_type PLua_InstanceType; \
90  void UnbindFromInstance(PLua &, const char *) { } \
91  void BindToInstance(PLua & lua, const char * instanceName) \
92  { \
93  lua.BindToInstanceStart(instanceName);
94 
95 #define PLUA_BINDING2(cpp_name, lua_name) \
96  lua.BindToInstanceFunc(lua_name, (void *)this, &PLua_InstanceType::cpp_name##_callback);
97 
98 #define PLUA_BINDING(fn_name) \
99  PLUA_BINDING2(fn_name, #fn_name)
100 
101 #define PLUA_BINDING_END() \
102  lua.BindToInstanceEnd(instanceName); \
103  }
104 
105 #define PLUA_FUNCTION_DECL(fn_name) \
106  static int fn_name##_callback(lua_State * L) \
107  { \
108  return ((PLua_InstanceType *)PLua::GetInstance(L))->fn_name(L); \
109  }
110 
111 #define PLUA_FUNCTION(fn_name) \
112  PLUA_FUNCTION_DECL(fn_name) \
113  int fn_name(lua_State * L) \
114 
115 #define PLUA_FUNCTION_NOARGS(fn_name) \
116  PLUA_FUNCTION_DECL(fn_name) \
117  int fn_name(lua_State *) \
118 
119 #define PLUA_DECLARE_FUNCTION(fn_name) \
120  PLUA_FUNCTION_DECL(fn_name) \
121  int fn_name(lua_State * L); \
122 
123 
125 
126 #endif // P_LUA
127 
128 #endif // PTLIB_LUA_H
129