31 template <>
struct Return<uint8_t> {
static inline int Set(HSQUIRRELVM vm, uint8_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
32 template <>
struct Return<uint16_t> {
static inline int Set(HSQUIRRELVM vm, uint16_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
33 template <>
struct Return<uint32_t> {
static inline int Set(HSQUIRRELVM vm, uint32_t res) { sq_pushinteger(vm, (int32_t)res);
return 1; } };
34 template <>
struct Return<int8_t> {
static inline int Set(HSQUIRRELVM vm, int8_t res) { sq_pushinteger(vm, res);
return 1; } };
35 template <>
struct Return<int16_t> {
static inline int Set(HSQUIRRELVM vm, int16_t res) { sq_pushinteger(vm, res);
return 1; } };
36 template <>
struct Return<int32_t> {
static inline int Set(HSQUIRRELVM vm, int32_t res) { sq_pushinteger(vm, res);
return 1; } };
37 template <>
struct Return<int64_t> {
static inline int Set(HSQUIRRELVM vm, int64_t res) { sq_pushinteger(vm, res);
return 1; } };
38 template <>
struct Return<
TileIndex> {
static inline int Set(HSQUIRRELVM vm,
TileIndex res) { sq_pushinteger(vm, (int32_t)res.base());
return 1; } };
39 template <>
struct Return<bool> {
static inline int Set(HSQUIRRELVM vm,
bool res) { sq_pushbool (vm, res);
return 1; } };
40 template <>
struct Return<char *> { };
41 template <>
struct Return<const char *> { };
42 template <>
struct Return<HSQOBJECT> {
static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res);
return 1; } };
44 template <
typename T>
requires std::is_enum_v<T>
struct Return<T> {
45 static inline int Set(HSQUIRRELVM vm, T res)
52 template <ConvertibleThroughBase T>
struct Return<T> {
53 static inline int Set(HSQUIRRELVM vm, T res)
55 sq_pushinteger(vm, res.base());
60 template <>
struct Return<std::optional<std::string>> {
61 static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res)
63 if (res.has_value()) {
64 sq_pushstring(vm, res.value(), -1);
76 template <
typename T>
struct Param;
78 template <>
struct Param<uint8_t> {
static inline uint8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
79 template <>
struct Param<uint16_t> {
static inline uint16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
80 template <>
struct Param<uint32_t> {
static inline uint32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
81 template <>
struct Param<int8_t> {
static inline int8_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
82 template <>
struct Param<int16_t> {
static inline int16_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
83 template <>
struct Param<int32_t> {
static inline int32_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
84 template <>
struct Param<int64_t> {
static inline int64_t Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return tmp; } };
85 template <>
struct Param<
TileIndex> {
static inline TileIndex Get(HSQUIRRELVM vm,
int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp);
return TileIndex((uint32_t)(int32_t)tmp); } };
86 template <>
struct Param<bool> {
static inline bool Get(HSQUIRRELVM vm,
int index) { SQBool tmp; sq_getbool (vm, index, &tmp);
return tmp != 0; } };
87 template <>
struct Param<const char *> { };
89 template <
typename T>
requires std::is_enum_v<T>
struct Param<T> {
90 static inline T Get(HSQUIRRELVM vm,
int index)
93 sq_getinteger(vm, index, &tmp);
94 return static_cast<T
>(tmp);
98 template <ConvertibleThroughBase T>
struct Param<T> {
99 static inline T Get(HSQUIRRELVM vm,
int index)
102 sq_getinteger(vm, index, &tmp);
103 return T{
static_cast<T::BaseType
>(tmp)};
107 template <>
struct Param<const std::string &> {
108 static inline const std::string Get(HSQUIRRELVM vm,
int index)
111 sq_tostring(vm, index);
114 sq_getstring(vm, -1, &tmp);
121 template <
typename Titem>
123 static inline Array<Titem> Get(HSQUIRRELVM vm,
int index)
126 if (sq_getsize(vm, index) > UINT16_MAX)
throw sq_throwerror(vm,
"an array used as parameter to a function is too large");
129 sq_getstackobj(vm, index, &obj);
130 sq_pushobject(vm, obj);
135 while (SQ_SUCCEEDED(sq_next(vm, -2))) {
155 template <
typename Tretval,
typename... Targs>
157 static int SQCall(
void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm)
159 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
163 template <
size_t... i>
164 static int SQCall(
void *, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
166 if constexpr (std::is_void_v<Tretval>) {
172 Tretval ret = (*func)(
183 template <
class Tcls,
typename Tretval,
typename... Targs>
185 static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
187 return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
190 static Tcls *SQConstruct(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
192 return SQConstruct(instance, func, vm, std::index_sequence_for<Targs...>{});
196 template <
size_t... i>
197 static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
199 if constexpr (std::is_void_v<Tretval>) {
205 Tretval ret = (instance->*func)(
212 template <
size_t... i>
213 static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
215 Tcls *inst =
new Tcls(
229 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
233 int nparam = sq_gettop(vm);
234 SQUserPointer ptr =
nullptr;
235 SQUserPointer real_instance =
nullptr;
242 sq_pushroottable(vm);
243 PushClassName<Tcls, Ttype>(vm);
245 sq_pushobject(vm, instance);
246 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
250 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
252 sq_getuserdata(vm, nparam, &ptr,
nullptr);
253 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
259 auto cls_instance =
static_cast<Tcls *
>(real_instance);
260 auto method = *
static_cast<Tmethod *
>(ptr);
262 }
catch (SQInteger &e) {
272 template <
typename Tcls,
typename Tmethod, ScriptType Ttype>
276 int nparam = sq_gettop(vm);
277 SQUserPointer ptr =
nullptr;
278 SQUserPointer real_instance =
nullptr;
285 sq_pushroottable(vm);
286 PushClassName<Tcls, Ttype>(vm);
288 sq_pushobject(vm, instance);
289 if (sq_instanceof(vm) != SQTrue)
return sq_throwerror(vm,
"class method is non-static");
293 sq_getinstanceup(vm, 1, &real_instance,
nullptr);
295 sq_getuserdata(vm, nparam, &ptr,
nullptr);
296 if (real_instance ==
nullptr)
return sq_throwerror(vm,
"couldn't detect real instance of class for non-static call");
302 auto cls_instance =
static_cast<Tcls *
>(real_instance);
303 auto method = *
static_cast<Tmethod *
>(ptr);
304 return static_cast<SQInteger
>((cls_instance->*method)(vm));
305 }
catch (SQInteger &e) {
315 template <
typename Tcls,
typename Tmethod>
319 int nparam = sq_gettop(vm);
320 SQUserPointer ptr =
nullptr;
323 sq_getuserdata(vm, nparam, &ptr,
nullptr);
327 auto cls_instance =
static_cast<Tcls *
>(
nullptr);
328 auto method = *
static_cast<Tmethod *
>(ptr);
330 }
catch (SQInteger &e) {
341 template <
typename Tcls,
typename Tmethod>
345 int nparam = sq_gettop(vm);
346 SQUserPointer ptr =
nullptr;
349 sq_getuserdata(vm, nparam, &ptr,
nullptr);
355 auto method = *
static_cast<Tmethod *
>(ptr);
356 return static_cast<SQInteger
>((*method)(vm));
357 }
catch (SQInteger &e) {
366 template <
typename Tcls>
370 if (p !=
nullptr) ((Tcls *)p)->Release();
379 template <
typename Tcls,
typename Tmethod,
int Tnparam>
385 sq_setinstanceup(vm, -Tnparam, instance);
386 sq_setreleasehook(vm, -Tnparam, DefSQDestructorCallback<Tcls>);
389 }
catch (SQInteger &e) {
398 template <
typename Tcls>
403 int nparam = sq_gettop(vm);
406 Tcls *instance =
new Tcls(vm);
407 sq_setinstanceup(vm, -nparam, instance);
408 sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
411 }
catch (SQInteger &e) {