File: LuaArgs.h

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (72 lines) | stat: -rw-r--r-- 1,692 bytes parent folder | download | duplicates (4)
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

#ifndef LUA_ARGS_H
#define LUA_ARGS_H
#pragma once

#include "LuaHeaders.h"
#include "LuaException.h"
#include "LuaConvert.h"

#include <type_traits>

namespace luacpp {
namespace args {
class ArgumentException: public LuaException {
 public:

	ArgumentException(const std::string& message = "Argument Error!") noexcept : LuaException(message) {
	}

	~ArgumentException() noexcept override = default;
};

class opt {};

extern opt optional;

inline int getArgsInternal(lua_State*  /*L*/, bool, int, int) {
	return 0;
}

template<typename T>
inline int popArgumentValue(lua_State* L, T& target, bool&  /*optionalOut*/, int& stackIndexOut) {
	convert::popValue(L, target, stackIndexOut, false);
	stackIndexOut = stackIndexOut + 1;

	return 1;
}

template<>
inline int popArgumentValue<opt>(lua_State*  /*L*/, opt&  /*target*/, bool& optionalOut, int&  /*stackIndexOut*/) {
	optionalOut = true;

	return 0;
}

template<typename T, typename ...Args>
inline int getArgsInternal(lua_State* L, bool optional_arg, int stackIndex, int numArgs, T& target, Args& ... args) {
	if (!std::is_same<T, opt>::value) {
		if (stackIndex > numArgs) {
			// No more arguments there
			if (optional_arg) {
				// Everything is fine, this argument is optional
				return 0;
			} else {
				// No good, no more arguments but no optional
				throw ArgumentException("Not enough arguments!");
			}
		}
	}

	return popArgumentValue(L, target, optional_arg, stackIndex) +
		getArgsInternal(L, optional_arg, stackIndex, numArgs, args...);
}

template<typename T, typename ...Args>
int getArgs(lua_State* L, T& target, Args& ... args) {
	return getArgsInternal(L, false, 1, lua_gettop(L), target, args...);
}
}
}

#endif