File: lua.h

package info (click to toggle)
ptlib 2.10.4~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 58,836 kB
  • sloc: cpp: 135,080; ansic: 8,534; yacc: 3,059; sh: 2,776; makefile: 1,082; lex: 390
file content (129 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * lua.h
 *
 * Interface library for Lua interpreter
 *
 * Portable Tools Library]
 *
 * Copyright (C) 2010 by Post Increment
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Portable Tools Library.
 *
 * The Initial Developer of the Original Code is Post Increment
 *
 * Contributor(s): Craig Southeren
 *
 * $Revision: 26015 $
 * $Author: rjongbloed $
 * $Date: 2011-06-14 02:31:10 -0500 (Tue, 14 Jun 2011) $
 */

#ifndef PTLIB_LUA_H
#define PTLIB_LUA_H

#ifdef P_USE_PRAGMA
#pragma interface
#endif

#include <ptlib.h>
#include <ptbuildopts.h>

#if P_LUA

struct lua_State;


//////////////////////////////////////////////////////////////

class PLua
{
  public:
    PLua();
    ~PLua();

    virtual bool LoadString(const char * text);

    virtual bool LoadFile(const char * filename);

    virtual bool Run(const char * program = NULL);

    virtual void OnError(int code, const PString & str);

    operator lua_State * () { return m_lua; }

    virtual void SetValue(const char * name, const char * value);

    typedef int (*CFunction)(lua_State *L);
    virtual void SetFunction(const char * name, CFunction func);

    bool CallLuaFunction(const char * name);
    bool CallLuaFunction(const char * name, const char * sig, ...);

    static int TraceFunction(lua_State * L);

    PString GetLastErrorText() const 
    { return m_lastErrorText; }

    void BindToInstanceStart(const char * instanceName);
    void BindToInstanceFunc(const char * lua_name, void * obj, CFunction func);
    void BindToInstanceEnd(const char * instanceName);

    static void * GetInstance(lua_State * L);

  protected:
    lua_State * m_lua;
    PString m_lastErrorText;
};

#define PLUA_BINDING_START(class_type) \
  typedef class_type PLua_InstanceType; \
  void UnbindFromInstance(PLua &, const char *) { } \
  void BindToInstance(PLua & lua, const char * instanceName) \
  { \
    lua.BindToInstanceStart(instanceName);

#define PLUA_BINDING2(cpp_name, lua_name) \
    lua.BindToInstanceFunc(lua_name, (void *)this, &PLua_InstanceType::cpp_name##_callback);

#define PLUA_BINDING(fn_name) \
  PLUA_BINDING2(fn_name, #fn_name)

#define PLUA_BINDING_END() \
    lua.BindToInstanceEnd(instanceName); \
  }

#define PLUA_FUNCTION_DECL(fn_name) \
  static int fn_name##_callback(lua_State * L) \
  { \
    return ((PLua_InstanceType *)PLua::GetInstance(L))->fn_name(L); \
  }

#define PLUA_FUNCTION(fn_name) \
  PLUA_FUNCTION_DECL(fn_name) \
  int fn_name(lua_State * L) \

#define PLUA_FUNCTION_NOARGS(fn_name) \
  PLUA_FUNCTION_DECL(fn_name) \
  int fn_name(lua_State *) \

#define PLUA_DECLARE_FUNCTION(fn_name) \
  PLUA_FUNCTION_DECL(fn_name) \
  int fn_name(lua_State * L); \


//////////////////////////////////////////////////////////////

#endif // P_LUA

#endif  // PTLIB_LUA_H