forked from pioneerspacesim/pioneer
/
LuaPushPull.h
109 lines (101 loc) · 3.78 KB
/
LuaPushPull.h
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
// Copyright © 2008-2013 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#ifndef _LUAPUSHPULL_H
#define _LUAPUSHPULL_H
#include "lua/lua.hpp"
#include "LuaObject.h"
#include "LuaVector.h"
#include "Lua.h"
#include <string>
inline void pi_lua_generic_push(lua_State * l, bool value) { lua_pushboolean(l, value); }
inline void pi_lua_generic_push(lua_State * l, int value) { lua_pushinteger(l, value); }
inline void pi_lua_generic_push(lua_State * l, unsigned int value) { lua_pushinteger(l, value); }
inline void pi_lua_generic_push(lua_State * l, double value) { lua_pushnumber(l, value); }
inline void pi_lua_generic_push(lua_State * l, const char * value) { lua_pushstring(l, value); }
inline void pi_lua_generic_push(lua_State * l, const std::string & value) {
lua_pushlstring(l, value.c_str(), value.size());
}
template <class T> void pi_lua_generic_push(lua_State * l, T* value) {
assert(l == Lua::manager->GetLuaState());
if (value)
LuaObject<T>::PushToLua(value);
else
lua_pushnil(l);
}
inline void pi_lua_generic_push(lua_State * l, const vector3d & value) { LuaVector::PushToLua(l, value); }
inline void pi_lua_generic_pull(lua_State * l, int index, bool & out) { out = lua_toboolean(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, int & out) { out = luaL_checkinteger(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, unsigned int & out) { out = luaL_checkunsigned(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, float & out) { out = luaL_checknumber(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, double & out) { out = luaL_checknumber(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, const char * & out) { out = luaL_checkstring(l, index); }
inline void pi_lua_generic_pull(lua_State * l, int index, std::string & out) {
size_t len;
const char *buf = luaL_checklstring(l, index, &len);
std::string(buf, len).swap(out);
}
template <class T> void pi_lua_generic_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::CheckFromLua(index);
}
inline void pi_lua_generic_pull(lua_State * l, int index, vector3d& out) {
out = *LuaVector::CheckFromLua(l, index);
}
inline bool pi_lua_strict_pull(lua_State * l, int index, bool & out) {
if (lua_type(l, index) == LUA_TBOOLEAN) {
out = lua_toboolean(l, index);
return true;
}
return false;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, int & out) {
if (lua_type(l, index) == LUA_TNUMBER) {
out = lua_tointeger(l, index);
return true;
}
return false;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, float & out) {
if (lua_type(l, index) == LUA_TNUMBER) {
out = lua_tonumber(l, index);
return true;
}
return false;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, double & out) {
if (lua_type(l, index) == LUA_TNUMBER) {
out = lua_tonumber(l, index);
return true;
}
return false;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, const char * & out) {
if (lua_type(l, index) == LUA_TSTRING) {
out = lua_tostring(l, index);
return true;
}
return false;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, std::string & out) {
if (lua_type(l, index) == LUA_TSTRING) {
size_t len;
const char *buf = lua_tolstring(l, index, &len);
std::string(buf, len).swap(out);
return true;
}
return false;
}
template <class T> bool pi_lua_strict_pull(lua_State * l, int index, T* & out) {
assert(l == Lua::manager->GetLuaState());
out = LuaObject<T>::GetFromLua(index);
return out != 0;
}
inline bool pi_lua_strict_pull(lua_State * l, int index, vector3d & out) {
const vector3d* tmp = LuaVector::GetFromLua(l, index);
if (tmp) {
out = *tmp;
return true;
}
return false;
}
#endif