File: LuaValue.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 (268 lines) | stat: -rw-r--r-- 6,299 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

#ifndef LUAVALUE_H
#define LUAVALUE_H
#pragma once

#include "LuaConvert.h"
#include "LuaReference.h"
#include "LuaException.h"

#include "LuaHeaders.h"

#include <memory>

#include <cstdint>
#include <csetjmp>

namespace luacpp {
/**
 * @brief The values of a LuaValue
 */
enum class ValueType {
	NONE,
	NIL,
	BOOLEAN,
	LIGHTUSERDATA,
	STRING,
	NUMBER,
	TABLE,
	FUNCTION,
	USERDATA,
	THREAD,
};

/**
 * @brief Represents a Lua-value
 *
 * This class holds a reference to a lua value and provides type checking to ensure that the value is still the same.
 */
class LuaValue {
 public:
	/**
     * @brief Creates a LuaValue.
     * This is done by pushing the value onto the lua stack and creating a reference to it.
     *
     * @param state The lua state
     * @param value The value to be referenced
     * @return luacpp::LuaValue A new LuaValue instance which references the specified value
     */
	template<class ValueType>
	static LuaValue createValue(lua_State* state, ValueType&& value) {
		LuaValue retVal(state);

		convert::pushValue(state, std::forward<ValueType>(value));

		retVal.setReference(UniqueLuaReference::create(state));

		// Remove the value again
		lua_pop(state, 1);

		return retVal;
	}

	static LuaValue createNil(lua_State* L);

	/**
	 * @brief Default constructor, creates an invalid LuaValue
	 */
	LuaValue();

	/**
	 * @brief Initializes the lua value
	 *
	 * The instance does not point to a lua-value after the constructor has finished. To reference
	 * a value use #setReference(LuaReferencePtr)
	 *
	 * @param state The lua state
	 */
	LuaValue(lua_State* state);

	/**
	 * @brief Copy-constructor
	 * @param other The other LuaValue.
	 */
	LuaValue(const LuaValue& other);
	LuaValue& operator=(const LuaValue& other);

	/**
	 * @brief Move-constructor
	 * @param other The other LuaValue.
	 */
	LuaValue(LuaValue&& other) noexcept;
	LuaValue& operator=(LuaValue&& other) noexcept;

	/**
	 * @brief Releases the reference
	 */
	virtual ~LuaValue();

	/**
	 * @brief Sets a new LuaReference.
	 *
	 * @param ref The new lua reference.
	 */
	virtual void setReference(const LuaReference& ref);

	/**
     * @brief Gets the LuaReference.
     *
     * This reference is used to reference the actual lua value.
     *
     * @return The LuaReference instance.
     */
	virtual const LuaReference getReference() const;

	/**
     * @brief Gets the lua type of this value.
     * @return One of the LUA_T* defines.
     */
	virtual ValueType getValueType() const;

	/**
     * @brief Checks if the value is of the specified type.
     *
     * @return bool @c true when it is, @c false if it isn't.
     */
	inline bool is(ValueType check) const { return _luaType == check; }

	/**
     * @brief Sets a new value, possible changing the type
     *
     * @param value The new value
     */
	template<class Type>
	void setValue(const Type& value) {
		// Push the new value
		convert::pushValue(_luaState, value);

		// And create a reference for it
		setReference(UniqueLuaReference::create(_luaState));
	}

	/**
     * @brief Gets the value or throws an exception
     *
     * @return Type The value
     *
     * @exception LuaException Thrown when the conversion failed.
     */
	template<class Type>
	Type getValue() const {
		_reference->pushValue(_luaState);

		Type target;
		if (!convert::popValue(_luaState, target)) {
			lua_pop(_luaState, 1);
			throw LuaException("Failed to pop value");
		} else {
			return target;
		}
	}

	/**
	 * @brief Same as above but allows passing a reference to a value where to store the lua value
	 *
	 * @exception LuaException Thrown when the conversion failed.
	 */
	template<typename Type>
	void getValue(Type&& od) const {
		_reference->pushValue(_luaState);

		if (!convert::popValue(_luaState, std::forward<Type>(od))) {
			lua_pop(_luaState, 1);
			throw LuaException("Failed to pop value");
		}
	}

	/**
     * @brief Specifies if the lua value is valid.
     *
     * @return bool @c true if it can be used and have an underlying reference, @c false otherwise.
     */
	bool isValid() const;

	/**
	 * @brief Pushes this lua value onto the stack.
	 * @param thread The thread stack onto which this value should be pushed. May be nullptr for the default state of
	 * this value
	 * @param manualStackAllocation Set to true if you manually allocate sufficient stack size before calling this function. Keep false unless you know what you are doing.
	 */
	bool pushValue(lua_State* thread, bool manualStackAllocation = false) const;

	lua_State* getLuaState() const;

  protected:
	lua_State* _luaState{nullptr}; //!< The lua state of this value.

	LuaReference _reference;

	ValueType _luaType = ValueType::NONE;
};

/**
* @brief Checks for equality of the lua values.
* @param lhs The left value
* @param rhs The right lua value.
* @return `true` when the value are equal as specified by the lua "==" operator.
*/
template<typename Type>
bool operator==(const LuaValue& lhs, const Type& rhs) {
	lhs.pushValue(lhs.getLuaState());
	convert::pushValue(lhs.getLuaState(), rhs);

	bool result = lua_equal(lhs.getLuaState(), -2, -1) != 0;

	lua_pop(lhs.getLuaState(), 2);

	return result;
}

/**
* @brief Checks if the other value is bigger than this value.
* @param lhs The left lua value.
* @param rhs The right lua value.
* @return `true` when the second value is bigger than this value as specified
* 			by lua.
*/
template<typename Type>
bool operator<(const LuaValue& lhs, const Type& rhs) {
	lhs.pushValue(lhs.getLuaState());
	convert::pushValue(lhs.getLuaState(), rhs);

	bool result = lua_lessthan(lhs.getLuaState(), -2, -1) != 0;

	lua_pop(lhs.getLuaState(), 2);

	return result;
}

template<typename Type>
bool operator!=(const LuaValue& lhs, const Type& rhs) {
	return !(lhs == rhs);
}

template<typename Type>
bool operator>(const LuaValue& lhs, const Type& rhs) {
	return !(lhs <= rhs);
}

template<typename Type>
bool operator<=(const LuaValue& lhs, const Type& rhs) {
	return (lhs == rhs) || (lhs < rhs);
}

template<typename Type>
bool operator>=(const LuaValue& lhs, const Type& rhs) {
	return !(lhs < rhs);
}

namespace convert {

void pushValue(lua_State* luaState, const LuaValue& value);

bool popValue(lua_State* luaState, LuaValue& target, int stackposition = -1, bool remove = true);

}
}

#endif