-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnorderedMap.h
51 lines (41 loc) · 1.24 KB
/
UnorderedMap.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
// https://github.com/vinniefalco/LuaBridge
// Copyright 2019, Dmitry Tarakanov
// SPDX-License-Identifier: MIT
#pragma once
#include <LuaBridge/detail/Stack.h>
#include <unordered_map>
namespace luabridge {
template<class K, class V>
struct Stack<std::unordered_map<K, V>>
{
typedef std::unordered_map<K, V> Map;
static void push(lua_State* L, const Map& map)
{
lua_createtable(L, 0, static_cast<int>(map.size()));
typedef typename Map::const_iterator ConstIter;
for (ConstIter i = map.begin(); i != map.end(); ++i)
{
Stack<K>::push(L, i->first);
Stack<V>::push(L, i->second);
lua_settable(L, -3);
}
}
static Map get(lua_State* L, int index)
{
if (!lua_istable(L, index))
{
luaL_error(L, "#%d argument must be a table", index);
}
Map map;
int const absindex = lua_absindex(L, index);
lua_pushnil(L);
while (lua_next(L, absindex) != 0)
{
map.emplace(Stack<K>::get(L, -2), Stack<V>::get(L, -1));
lua_pop(L, 1);
}
return map;
}
static bool isInstance(lua_State* L, int index) { return lua_istable(L, index); }
};
} // namespace luabridge