mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Make Squirrel handle ConvertibleThroughBase. (#13453)
This removes the need for a Money specialisation. TileIndex specialisation remains due to unsigned/signed connivance.pull/13468/head
parent
08ed68bc85
commit
e2b59449bb
|
@ -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 <class CL, ScriptType ST> const char *GetClassName();
|
||||
|
@ -36,7 +35,6 @@ namespace SQConvert {
|
|||
template <> struct Return<int16_t> { static inline int Set(HSQUIRRELVM vm, int16_t res) { sq_pushinteger(vm, res); return 1; } };
|
||||
template <> struct Return<int32_t> { static inline int Set(HSQUIRRELVM vm, int32_t res) { sq_pushinteger(vm, res); return 1; } };
|
||||
template <> struct Return<int64_t> { static inline int Set(HSQUIRRELVM vm, int64_t res) { sq_pushinteger(vm, res); return 1; } };
|
||||
template <> struct Return<Money> { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } };
|
||||
template <> struct Return<TileIndex> { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32_t)res.base()); return 1; } };
|
||||
template <> struct Return<bool> { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } };
|
||||
template <> struct Return<char *> { /* Do not use char *, use std::optional<std::string> instead. */ };
|
||||
|
@ -52,6 +50,14 @@ namespace SQConvert {
|
|||
}
|
||||
};
|
||||
|
||||
template <ConvertibleThroughBase T> struct Return<T> {
|
||||
static inline int Set(HSQUIRRELVM vm, T res)
|
||||
{
|
||||
sq_pushinteger(vm, res.base());
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct Return<std::optional<std::string>> {
|
||||
static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res)
|
||||
{
|
||||
|
@ -78,7 +84,6 @@ namespace SQConvert {
|
|||
template <> struct Param<int32_t> { static inline int32_t Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int64_t> { static inline int64_t Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
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); } };
|
||||
template <> struct Param<Money> { static inline Money Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<bool> { static inline bool Get(HSQUIRRELVM vm, int index) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } };
|
||||
template <> struct Param<const char *> { /* Do not use const char *, use std::string& instead. */ };
|
||||
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
|
||||
|
@ -92,6 +97,15 @@ namespace SQConvert {
|
|||
}
|
||||
};
|
||||
|
||||
template <ConvertibleThroughBase T> struct Param<T> {
|
||||
static inline T Get(HSQUIRRELVM vm, int index)
|
||||
{
|
||||
SQInteger tmp;
|
||||
sq_getinteger(vm, index, &tmp);
|
||||
return T{static_cast<T::BaseType>(tmp)};
|
||||
}
|
||||
};
|
||||
|
||||
template <> struct Param<const std::string &> {
|
||||
static inline const std::string Get(HSQUIRRELVM vm, int index)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue