PTLib  Version 2.10.10
 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: 27536 $
27  * $Author: rjongbloed $
28  * $Date: 2012-04-26 02:52:42 -0500 (Thu, 26 Apr 2012) $
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  virtual PString GetValue(const char * name);
66 
67  typedef int (*CFunction)(lua_State *L);
68  virtual void SetFunction(const char * name, CFunction func);
69 
70  bool CallLuaFunction(const char * name);
71  bool CallLuaFunction(const char * name, const char * sig, ...);
72 
73  static int TraceFunction(lua_State * L);
74 
75  PString GetLastErrorText() const
76  { return m_lastErrorText; }
77 
78  void BindToInstanceStart(const char * instanceName);
79  void BindToInstanceFunc(const char * lua_name, void * obj, CFunction func);
80  void BindToInstanceEnd(const char * instanceName);
81 
82  static void * GetInstance(lua_State * L);
83 
84  protected:
85  lua_State * m_lua;
86  PString m_lastErrorText;
87 };
88 
89 #define PLUA_BINDING_START(class_type) \
90  typedef class_type PLua_InstanceType; \
91  void UnbindFromInstance(PLua &, const char *) { } \
92  void BindToInstance(PLua & lua, const char * instanceName) \
93  { \
94  lua.BindToInstanceStart(instanceName);
95 
96 #define PLUA_BINDING2(cpp_name, lua_name) \
97  lua.BindToInstanceFunc(lua_name, (void *)this, &PLua_InstanceType::cpp_name##_callback);
98 
99 #define PLUA_BINDING(fn_name) \
100  PLUA_BINDING2(fn_name, #fn_name)
101 
102 #define PLUA_BINDING_END() \
103  lua.BindToInstanceEnd(instanceName); \
104  }
105 
106 #define PLUA_FUNCTION_DECL(fn_name) \
107  static int fn_name##_callback(lua_State * L) \
108  { \
109  return ((PLua_InstanceType *)PLua::GetInstance(L))->fn_name(L); \
110  }
111 
112 #define PLUA_FUNCTION(fn_name) \
113  PLUA_FUNCTION_DECL(fn_name) \
114  int fn_name(lua_State * L) \
115 
116 #define PLUA_FUNCTION_NOARGS(fn_name) \
117  PLUA_FUNCTION_DECL(fn_name) \
118  int fn_name(lua_State *) \
119 
120 #define PLUA_DECLARE_FUNCTION(fn_name) \
121  PLUA_FUNCTION_DECL(fn_name) \
122  int fn_name(lua_State * L); \
123 
124 
126 
127 #endif // P_LUA
128 
129 #endif // PTLIB_LUA_H
130 
The character string class.
Definition: pstring.h:108