@@ -32,6 +32,7 @@ distribution.
32
32
#include < type_traits>
33
33
#include < unordered_map>
34
34
#include < unordered_set>
35
+ #include < concepts>
35
36
36
37
#include " Core.h"
37
38
#include " ColorText.h"
@@ -60,7 +61,7 @@ namespace DFHack {
60
61
};
61
62
}
62
63
63
- namespace DFHack { namespace Lua {
64
+ namespace DFHack :: Lua {
64
65
/* *
65
66
* Create or initialize a lua interpreter with access to DFHack tools.
66
67
*/
@@ -140,34 +141,39 @@ namespace DFHack {namespace Lua {
140
141
DFHACK_EXPORT void CheckDFAssign (lua_State *state, type_identity *type,
141
142
void *target, int val_index, bool exact_type = false );
142
143
144
+ template <typename T> concept df_object = requires(T x)
145
+ {
146
+ { df::identity_traits<T>::get () } -> std::convertible_to<df::type_identity*>;
147
+ };
148
+
143
149
/* *
144
150
* Push the pointer onto the stack as a wrapped DF object of a specific type.
145
151
*/
146
- template <class T >
152
+ template <df_object T>
147
153
void PushDFObject (lua_State *state, T *ptr) {
148
154
PushDFObject (state, df::identity_traits<T>::get (), ptr);
149
155
}
150
156
151
157
/* *
152
158
* Check that the value is a wrapped DF object of the correct type, and if so return the pointer.
153
159
*/
154
- template <class T >
160
+ template <df_object T>
155
161
T *GetDFObject (lua_State *state, int val_index, bool exact_type = false ) {
156
162
return (T*)GetDFObject (state, df::identity_traits<T>::get (), val_index, exact_type);
157
163
}
158
164
159
165
/* *
160
166
* Check that the value is a wrapped DF object of the correct type, and if so return the pointer. Otherwise throw an argument type error.
161
167
*/
162
- template <class T >
168
+ template <df_object T>
163
169
T *CheckDFObject (lua_State *state, int val_index, bool exact_type = false ) {
164
170
return (T*)CheckDFObject (state, df::identity_traits<T>::get (), val_index, exact_type);
165
171
}
166
172
167
173
/* *
168
174
* Assign the value at val_index to the target using df.assign().
169
175
*/
170
- template <class T >
176
+ template <df_object T>
171
177
bool AssignDFObject (color_ostream &out, lua_State *state, T *target,
172
178
int val_index, bool exact_type = false , bool perr = true ) {
173
179
return AssignDFObject (out, state, df::identity_traits<T>::get (),
@@ -178,7 +184,7 @@ namespace DFHack {namespace Lua {
178
184
* Assign the value at val_index to the target using df.assign().
179
185
* Throws in case of an error.
180
186
*/
181
- template <class T >
187
+ template <df_object T>
182
188
void CheckDFAssign (lua_State *state, T *target, int val_index, bool exact_type = false ) {
183
189
CheckDFAssign (state, df::identity_traits<T>::get (), target, val_index, exact_type);
184
190
}
@@ -298,42 +304,33 @@ namespace DFHack {namespace Lua {
298
304
/* *
299
305
* Push utility functions
300
306
*/
301
- #if 0
302
- #define NUMBER_PUSH(type) inline void Push(lua_State *state, type value) { lua_pushnumber(state, value); }
303
- NUMBER_PUSH(char)
304
- NUMBER_PUSH(int8_t) NUMBER_PUSH(uint8_t)
305
- NUMBER_PUSH(int16_t) NUMBER_PUSH(uint16_t)
306
- NUMBER_PUSH(int32_t) NUMBER_PUSH(uint32_t)
307
- NUMBER_PUSH(int64_t) NUMBER_PUSH(uint64_t)
308
- NUMBER_PUSH(float) NUMBER_PUSH(double)
309
- #undef NUMBER_PUSH
310
- #else
311
- template <class T >
312
- inline typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value>::type
313
- Push (lua_State *state, T value) {
307
+ template <typename T> concept lua_integral = (std::is_integral_v<T> || std::is_enum_v<T>);
308
+
309
+ inline void Push (lua_State *state, lua_integral auto value) {
314
310
lua_pushinteger (state, value);
315
311
}
316
- template <class T >
317
- inline typename std::enable_if<std::is_floating_point<T>::value>::type
318
- Push (lua_State *state, T value) {
312
+ inline void Push (lua_State* state, std::floating_point auto value) {
319
313
lua_pushnumber (state, lua_Number (value));
320
314
}
321
- #endif
322
315
inline void Push (lua_State *state, bool value) {
323
316
lua_pushboolean (state, value);
324
317
}
325
- inline void Push (lua_State *state, const char *str) {
326
- lua_pushstring (state, str);
327
- }
328
- inline void Push (lua_State *state, const std::string &str) {
329
- lua_pushlstring (state, str.data (), str.size ());
318
+
319
+ template <typename T> concept lua_string = (std::convertible_to<T, std::string_view>);
320
+
321
+ inline void Push (lua_State *state, lua_string auto str) {
322
+ std::string_view sv{ str };
323
+ lua_pushlstring (state, sv.data (), sv.size ());
330
324
}
325
+
331
326
DFHACK_EXPORT void Push (lua_State *state, const df::coord &obj);
332
327
DFHACK_EXPORT void Push (lua_State *state, const df::coord2d &obj);
333
328
void Push (lua_State *state, const Units::NoblePosition &pos);
334
329
DFHACK_EXPORT void Push (lua_State *state, const MaterialInfo &info);
335
330
DFHACK_EXPORT void Push (lua_State *state, const Screen::Pen &info);
336
- template <class T > inline void Push (lua_State *state, T *ptr) {
331
+
332
+ template <df_object T> inline void Push (lua_State *state, T *ptr)
333
+ {
337
334
PushDFObject (state, ptr);
338
335
}
339
336
@@ -472,10 +469,6 @@ namespace DFHack {namespace Lua {
472
469
* All accesses must be done under CoreSuspender.
473
470
*/
474
471
namespace Core {
475
- // DFHACK_EXPORT extern lua_State *State;
476
-
477
- // Not exported; for use by the Core class
478
- lua_State* Init (color_ostream &out);
479
472
DFHACK_EXPORT void Reset (color_ostream &out, const char *where);
480
473
481
474
// Events signalled by the core
@@ -559,7 +552,7 @@ namespace DFHack {namespace Lua {
559
552
Notification (function_identity_base *handler = NULL )
560
553
: state(NULL ), key(NULL ), handler(handler), count(0 ) {}
561
554
562
- int get_listener_count () { return count; }
555
+ int get_listener_count () const { return count; }
563
556
lua_State *get_state () { return state; }
564
557
function_identity_base *get_handler () { return handler; }
565
558
@@ -572,7 +565,7 @@ namespace DFHack {namespace Lua {
572
565
void bind (lua_State *state, const char *name);
573
566
void bind (lua_State *state, void *key);
574
567
};
575
- }}
568
+ }
576
569
577
570
#define DEFINE_LUA_EVENT_0 (name, handler ) \
578
571
static DFHack::Lua::Notification name##_event(df::wrap_function(handler, true )); \
0 commit comments