File: LuaUtil.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (97 lines) | stat: -rw-r--r-- 2,590 bytes parent folder | download | duplicates (2)
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

#ifndef LUA_UTIL_H
#define LUA_UTIL_H
#pragma once

#include "LuaValue.h"
#include "LuaTable.h"
#include "LuaConvert.h"

#include <type_traits> //for std::underlying_type


namespace luacpp {
class LuaTable;

namespace util {

/**
 * @brief Initializes this sublibrary in the specified lua state
 * @param L The lua state to initialize with. Must be the main thread.
 */
void initializeLuaSupportLib(lua_State* L);

/**
 * @brief From the specified state gets the main thread.
 * @param L The state to query
 */
lua_State* getMainThread(lua_State* L);

/**
 * @brief Fills the given container with all key-value pairs.
 * Each key-value pair is converted into the type given to the Container and then push_back is used to add it.
 *
 * @param table The table to be used
 * @param keyValueList The container which should be used
 * @return void
 *
 * @tparam Container Must be a container type with a push_back function and must have a value_type
 * 	which exposes a first_type and second_type typedef (for example std::pair).
 */
template<typename Container>
void tableListPairs(LuaTable& table, Container& keyValueList) {
	typedef typename Container::value_type::first_type key_type;
	typedef typename Container::value_type::second_type value_type;

	keyValueList.clear();
	lua_State* L = table.getLuaState();

	for (auto val : table) {
		val.first.pushValue(L);
		key_type key;
		if (!convert::popValue(L, key)) {
			throw LuaException("Failed to pop key!");
		}

		val.second.pushValue(L);
		value_type value;

		if (!convert::popValue(L, value)) {
			throw LuaException("Failed to pop value!");
		}

		keyValueList.push_back(std::make_pair(key, value));
	}
}

/**
 * @brief Fills the given list with all the values from the table.
 * This will fill the list with the same value as @c ipairs function in lua.
 *
 * @param table The table to be used
 * @param list The list into which the values should be inserted
 * @return void
 *
 * @tparam Container The type of the container into which the values should be inserted,
 * 	must have a value_type typedef so the lua values can be converted and a push_back function.
 *
 * @exception LuaException May be thrown if an element of the table is not convertible to
 * 	Container::value_type.
 */
template<typename Container>
void tableToList(LuaTable& table, Container& list) {
	list.clear();

	size_t length = table.getLength();

	// Lua arrays begin at 1
	for (size_t i = 1; i <= length; ++i) {
		list.push_back(table.getValue<typename Container::value_type>(i));
	}
}

const char* getValueName(ValueType type);
}
}

#endif // LUA_UTIL_H