-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLuaBase.cpp
More file actions
163 lines (151 loc) · 4 KB
/
LuaBase.cpp
File metadata and controls
163 lines (151 loc) · 4 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Author: Guillaume.Stordeur@gmail.com
// License: none, no restrictions, use at your own risk
// Date: 07/13/12
// Version 1.1
#include "LuaBase.h"
#include "LuaTable.h"
#include "LuaFunction.h"
#ifdef WIN32
#include <windows.h>
#endif
// Static error flag (I don't like exceptions)
static bool gLuaError = false;
// User supplied error callback function
static errorCB gErrorCBFunc = 0;
namespace LuaUtils {;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set error callback function that will be called when Lua errors occur
void LuaSetErrorCB(errorCB cbfunc)
{
gErrorCBFunc = cbfunc;
}
////////////////////////////////////////////////////////////////////////////////////
// Get and clear the error flag that can be set internally by some LuaUtils functions
bool LuaGetErrorFlag()
{
bool res = gLuaError;
gLuaError = false;
return res;
}
namespace detail {;
////////////////////////////////////////////////////////////////////////////////////
void _LuaLogError(const char *format, ...)
{
// Set the error flag
gLuaError = true;
// Dump the error string
char buf[512];
va_list args;
va_start(args, format);
vsnprintf(buf, 512, format, args);
va_end(args);
#ifdef WIN32
OutputDebugString(buf);
#endif
// Call user supplied function
if (gErrorCBFunc)
gErrorCBFunc(buf);
}
////////////////////////////////////////////////////////////////////////////////////
// luaPushValue overloads
//
void _LuaBase::luaPushValue(const std::string &s) const { lua_pushstring(mL.get(), s.c_str()); }
void _LuaBase::luaPushValue(int n) const { lua_pushinteger(mL.get(), (lua_Integer)n); }
void _LuaBase::luaPushValue(unsigned char n) const { lua_pushinteger(mL.get(), (lua_Integer)n); }
void _LuaBase::luaPushValue(double n) const { lua_pushnumber(mL.get(), n); }
void _LuaBase::luaPushValue(float n) const { lua_pushnumber(mL.get(), (double)n); }
void _LuaBase::luaPushValue(bool b) const { lua_pushboolean(mL.get(), b); }
void _LuaBase::luaPushValue(const char *s) const { lua_pushstring(mL.get(), s); }
void _LuaBase::luaPushValue(lua_CFunction f) const { lua_pushcfunction(mL.get(), f); }
void _LuaBase::luaPushValue(const LuaTable &t) const { t.push(); }
void _LuaBase::luaPushValue(const _LuaFunctionBase &f) const { f.push(); }
////////////////////////////////////////////////////////////////////////////////////
// luaPopValue overloads
//
bool _LuaBase::luaPopValue(int &res) const
{
bool ret = false;
if (lua_isnumber(mL.get(), -1))
{
res = (int)lua_tointeger(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(unsigned char &res) const
{
bool ret = false;
if (lua_isnumber(mL.get(), -1))
{
res = (unsigned char)lua_tointeger(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(double &res) const
{
bool ret = false;
if (lua_isnumber(mL.get(), -1))
{
res = lua_tonumber(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(float &res) const
{
bool ret = false;
if (lua_isnumber(mL.get(), -1))
{
res = (float)lua_tonumber(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(bool &res) const
{
bool ret = false;
if (lua_isboolean(mL.get(), -1))
{
res = lua_toboolean(mL.get(), -1) ? true : false;
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(std::string &res) const
{
bool ret = false;
if (lua_isstring(mL.get(), -1))
{
res = lua_tostring(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(lua_CFunction &res) const
{
bool ret = false;
if (lua_iscfunction(mL.get(), -1))
{
res = lua_tocfunction(mL.get(), -1);
ret = true;
}
lua_pop(mL.get(), 1);
return ret;
}
bool _LuaBase::luaPopValue(LuaTable &res) const
{
return res.init(mL, "<anon>", false);
}
bool _LuaBase::luaPopValue(_LuaFunctionBase &res) const
{
return res.initFromStack(mL, "<anon>");
}
} // detail
} // LuaUtils