From e2b59449bb325c166126fefeb5613af5b70edebd Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 4 Feb 2025 01:15:58 +0000 Subject: [PATCH] Codechange: Make Squirrel handle ConvertibleThroughBase. (#13453) This removes the need for a Money specialisation. TileIndex specialisation remains due to unsigned/signed connivance. --- src/script/squirrel_helper.hpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index 51ce3bf9cb..8870e4c9fc 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -11,10 +11,9 @@ #define SQUIRREL_HELPER_HPP #include "squirrel.hpp" -#include "../core/alloc_func.hpp" -#include "../economy_type.h" #include "../string_func.h" #include "../tile_type.h" +#include "../core/convertible_through_base.hpp" #include "squirrel_helper_type.hpp" template const char *GetClassName(); @@ -36,7 +35,6 @@ namespace SQConvert { template <> struct Return { static inline int Set(HSQUIRRELVM vm, int16_t res) { sq_pushinteger(vm, res); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, int32_t res) { sq_pushinteger(vm, res); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, int64_t res) { sq_pushinteger(vm, res); return 1; } }; - template <> struct Return { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32_t)res.base()); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } }; template <> struct Return { /* Do not use char *, use std::optional instead. */ }; @@ -52,6 +50,14 @@ namespace SQConvert { } }; + template struct Return { + static inline int Set(HSQUIRRELVM vm, T res) + { + sq_pushinteger(vm, res.base()); + return 1; + } + }; + template <> struct Return> { static inline int Set(HSQUIRRELVM vm, std::optional res) { @@ -78,7 +84,6 @@ namespace SQConvert { template <> struct Param { static inline int32_t Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param { static inline int64_t Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param { static inline TileIndex Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32_t)(int32_t)tmp); } }; - template <> struct Param { static inline Money Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param { static inline bool Get(HSQUIRRELVM vm, int index) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } }; template <> struct Param { /* Do not use const char *, use std::string& instead. */ }; template <> struct Param { static inline void *Get(HSQUIRRELVM vm, int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } }; @@ -92,6 +97,15 @@ namespace SQConvert { } }; + template struct Param { + static inline T Get(HSQUIRRELVM vm, int index) + { + SQInteger tmp; + sq_getinteger(vm, index, &tmp); + return T{static_cast(tmp)}; + } + }; + template <> struct Param { static inline const std::string Get(HSQUIRRELVM vm, int index) {