-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLuaTable.h
More file actions
62 lines (50 loc) · 1.43 KB
/
LuaTable.h
File metadata and controls
62 lines (50 loc) · 1.43 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
#ifndef __SCRIPTBIND_LUA_LUATABLE_H__
#define __SCRIPTBIND_LUA_LUATABLE_H__
#include "LuaObject.h"
namespace Aurora
{
struct LuaTable : public LuaObject
{
LuaTable();
LuaTable(lua_State* L);
LuaTable(lua_State* L, int index) : LuaObject(L, index) {}
LuaTable(lua_State* L, const char* name) : LuaObject(L, name) {}
template<typename K,typename V> inline void Set(K key, V value);
template<typename V, typename K> inline V Get(K key);
};
//////////////////////////////////////////////////////////////////////////
template<typename K,typename V>
inline void LuaTable::Set(K key, V value)
{
if ( IsValid() )
{
int oldTop=lua_gettop(lua_state_);
lua_rawgeti(lua_state_, LUA_REGISTRYINDEX, lua_ref_);
Push(lua_state_, key);
Push(lua_state_, value);
lua_settable(lua_state_, -3);
lua_settop(lua_state_, oldTop);
}
}
template<typename V, typename K>
inline V LuaTable::Get(K key)
{
int oldTop=lua_gettop(lua_state_);
if ( IsValid() )
{
lua_rawgeti(lua_state_, LUA_REGISTRYINDEX, lua_ref_);
Push(lua_state_, key);
lua_gettable(lua_state_, -2);
}
else
{
lua_pushnil(lua_state_);
}
V result = Pop<V>(lua_state_, -1);
lua_settop(lua_state_, oldTop);
return result;
}
template<> inline void Push(lua_State* L, LuaTable ret) { ret.PushLuaObject(L);};
template<> inline LuaTable Pop(lua_State* L, int index) { return LuaTable(L, index);};
}
#endif // __SCRIPTBIND_LUA_LUATABLE_H__