|
5 | 5 |
|
6 | 6 | #include <cmath> |
7 | 7 | #include <stdexcept> |
| 8 | +#include <utility> |
8 | 9 |
|
9 | 10 | namespace Misc |
10 | 11 | { |
11 | | - struct FiniteDouble |
| 12 | + template <class T> |
| 13 | + struct FiniteNumber |
12 | 14 | { |
13 | | - double mValue; |
14 | | - FiniteDouble(double v) |
15 | | - { |
16 | | - if (!std::isfinite(v)) |
17 | | - throw std::invalid_argument("Value must be a finite number"); |
18 | | - mValue = v; |
19 | | - } |
20 | | - operator double() const { return mValue; } |
21 | | - }; |
| 15 | + T mValue; |
22 | 16 |
|
23 | | - struct FiniteFloat |
24 | | - { |
25 | | - float mValue; |
26 | | - FiniteFloat(float v) |
| 17 | + FiniteNumber(T v) |
27 | 18 | { |
28 | 19 | if (!std::isfinite(v)) |
29 | 20 | throw std::invalid_argument("Value must be a finite number"); |
30 | 21 | mValue = v; |
31 | 22 | } |
32 | | - operator float() const { return mValue; } |
| 23 | + |
| 24 | + operator T() const { return mValue; } |
33 | 25 | }; |
| 26 | + |
| 27 | + using FiniteDouble = FiniteNumber<double>; |
| 28 | + |
| 29 | + using FiniteFloat = FiniteNumber<float>; |
34 | 30 | } |
35 | 31 |
|
36 | 32 | namespace sol |
37 | 33 | { |
38 | | - using FiniteDouble = Misc::FiniteDouble; |
39 | | - using FiniteFloat = Misc::FiniteFloat; |
40 | | - |
41 | | - template <typename Handler> |
42 | | - bool sol_lua_check( |
43 | | - sol::types<FiniteDouble>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking) |
44 | | - { |
45 | | - bool success = sol::stack::check<double>(L, lua_absindex(L, index), handler); |
46 | | - tracking.use(1); |
47 | | - return success; |
48 | | - } |
49 | | - |
50 | | - static FiniteDouble sol_lua_get(sol::types<FiniteDouble>, lua_State* L, int index, sol::stack::record& tracking) |
51 | | - { |
52 | | - double val = sol::stack::get<double>(L, lua_absindex(L, index)); |
53 | | - tracking.use(1); |
54 | | - return FiniteDouble(val); |
55 | | - } |
56 | | - |
57 | | - template <typename Handler> |
| 34 | + template <class Handler, class T> |
58 | 35 | bool sol_lua_check( |
59 | | - sol::types<FiniteFloat>, lua_State* L, int index, Handler&& handler, sol::stack::record& tracking) |
| 36 | + types<Misc::FiniteNumber<T>>, lua_State* state, int index, Handler&& handler, stack::record& tracking) |
60 | 37 | { |
61 | | - bool success = sol::stack::check<float>(L, lua_absindex(L, index), handler); |
| 38 | + bool success = stack::check<T>(state, lua_absindex(state, index), std::forward<Handler>(handler)); |
62 | 39 | tracking.use(1); |
63 | 40 | return success; |
64 | 41 | } |
65 | 42 |
|
66 | | - static FiniteFloat sol_lua_get(sol::types<FiniteFloat>, lua_State* L, int index, sol::stack::record& tracking) |
| 43 | + template <class T> |
| 44 | + static Misc::FiniteNumber<T> sol_lua_get( |
| 45 | + types<Misc::FiniteNumber<T>>, lua_State* state, int index, stack::record& tracking) |
67 | 46 | { |
68 | | - float val = sol::stack::get<float>(L, lua_absindex(L, index)); |
| 47 | + T value = stack::get<T>(state, lua_absindex(state, index)); |
69 | 48 | tracking.use(1); |
70 | | - return FiniteFloat(val); |
| 49 | + return value; |
71 | 50 | } |
72 | 51 | } |
73 | 52 |
|
|
0 commit comments