comparison C++/LuaeTable.cpp @ 225:e01ee0c72c43

Luae: begin refactoring of Luae
author David Demelier <markand@malikania.fr>
date Fri, 09 May 2014 17:12:25 +0200
parents
children
comparison
equal deleted inserted replaced
224:ca69910b1407 225:e01ee0c72c43
1 /*
2 * LuaeTable.cpp -- Lua table helpers
3 *
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "LuaeTable.h"
20
21 void LuaeTable::create(lua_State *L, int nrec, int narr)
22 {
23 LUAE_STACK_CHECKBEGIN(L);
24
25 lua_createtable(L, nrec, narr);
26
27 LUAE_STACK_CHECKEND(L, - 1);
28 }
29
30 int LuaeTable::type(lua_State *L, int idx, const std::string &name)
31 {
32 int type;
33
34 LUAE_STACK_CHECKBEGIN(L);
35
36 lua_getfield(L, idx, name.c_str());
37 type = lua_type(L, -1);
38 lua_pop(L, 1);
39
40 LUAE_STACK_CHECKEQUALS(L);
41
42 return type;
43 }
44
45 void LuaeTable::read(lua_State *L, int idx, ReadFunction func)
46 {
47 LUAE_STACK_CHECKBEGIN(L);
48
49 lua_pushnil(L);
50
51 if (idx < 0)
52 --idx;
53
54 while (lua_next(L, idx)) {
55 func(L, lua_type(L, -2), lua_type(L, -1));
56 lua_pop(L, 1);
57 }
58
59 LUAE_STACK_CHECKEQUALS(L);
60 }
61
62 int LuaeTable::ref(lua_State *L, int idx, int type, const std::string &name)
63 {
64 int ref = LUA_REFNIL;
65
66 lua_getfield(L, idx, name.c_str());
67
68 if (lua_type(L, -1) == type) {
69 lua_pushvalue(L, -1);
70 ref = luaL_ref(L, LUA_REGISTRYINDEX);
71 }
72
73 lua_pop(L, 1);
74
75 return ref;
76 }