1
0
Fork 0

Cleanup: the need for SQAutoFreePointers has gone

pull/10801/head
Rubidium 2023-05-06 10:24:31 +02:00 committed by rubidium42
parent d9e93edc8b
commit f9b5bc7ee6
4 changed files with 31 additions and 47 deletions

View File

@ -28,21 +28,21 @@ endmacro()
macro(dump_class_templates NAME) macro(dump_class_templates NAME)
string(REGEX REPLACE "^Script" "" REALNAME ${NAME}) string(REGEX REPLACE "^Script" "" REALNAME ${NAME})
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${NAME} *> { static inline ${NAME} *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return (${NAME} *)instance; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${NAME} *> { static inline ${NAME} *Get(HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return (${NAME} *)instance; } };")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${NAME} &> { static inline ${NAME} &Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${NAME} &> { static inline ${NAME} &Get(HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<const ${NAME} *> { static inline const ${NAME} *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return (${NAME} *)instance; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<const ${NAME} *> { static inline const ${NAME} *Get(HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return (${NAME} *)instance; } };")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<const ${NAME} &> { static inline const ${NAME} &Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<const ${NAME} &> { static inline const ${NAME} &Get(HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };")
if("${NAME}" STREQUAL "ScriptEvent") if("${NAME}" STREQUAL "ScriptEvent")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Return<${NAME} *> { static inline int Set(HSQUIRRELVM vm, ${NAME} *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } Squirrel::CreateClassInstanceVM(vm, \"${REALNAME}\", res, nullptr, DefSQDestructorCallback<${NAME}>, true); return 1; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Return<${NAME} *> { static inline int Set(HSQUIRRELVM vm, ${NAME} *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } Squirrel::CreateClassInstanceVM(vm, \"${REALNAME}\", res, nullptr, DefSQDestructorCallback<${NAME}>, true); return 1; } };")
elseif("${NAME}" STREQUAL "ScriptText") elseif("${NAME}" STREQUAL "ScriptText")
string(APPEND SQUIRREL_EXPORT "\n") string(APPEND SQUIRREL_EXPORT "\n")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<Text *> {") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<Text *> {")
string(APPEND SQUIRREL_EXPORT "\n static inline Text *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) {") string(APPEND SQUIRREL_EXPORT "\n static inline Text *Get(HSQUIRRELVM vm, int index) {")
string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_INSTANCE) {") string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_INSTANCE) {")
string(APPEND SQUIRREL_EXPORT "\n return Param<ScriptText *>::Get(vm, index, ptr);") string(APPEND SQUIRREL_EXPORT "\n return Param<ScriptText *>::Get(vm, index);")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")
string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_STRING) {") string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_STRING) {")
string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param<const std::string &>::Get(vm, index, ptr));") string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param<const std::string &>::Get(vm, index));")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")
string(APPEND SQUIRREL_EXPORT "\n return nullptr;") string(APPEND SQUIRREL_EXPORT "\n return nullptr;")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")
@ -299,7 +299,7 @@ foreach(LINE IN LISTS SOURCE_LINES)
endif() endif()
string(APPEND SQUIRREL_EXPORT "\n /* Allow enums to be used as Squirrel parameters */") string(APPEND SQUIRREL_EXPORT "\n /* Allow enums to be used as Squirrel parameters */")
foreach(ENUM IN LISTS ENUMS) foreach(ENUM IN LISTS ENUMS)
string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${ENUM}> { static inline ${ENUM} Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (${ENUM})tmp; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Param<${ENUM}> { static inline ${ENUM} Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (${ENUM})tmp; } };")
string(APPEND SQUIRREL_EXPORT "\n template <> struct Return<${ENUM}> { static inline int Set(HSQUIRRELVM vm, ${ENUM} res) { sq_pushinteger(vm, res); return 1; } };") string(APPEND SQUIRREL_EXPORT "\n template <> struct Return<${ENUM}> { static inline int Set(HSQUIRRELVM vm, ${ENUM} res) { sq_pushinteger(vm, res); return 1; } };")
endforeach() endforeach()
endif() endif()

View File

@ -379,7 +379,7 @@ static bool NormaliseTileOffset(int32 *tile)
return false; return false;
} }
/* static */ SQInteger ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::Slope slope_, Array<> existing, TileIndex start_, TileIndex end_) /* static */ SQInteger ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::Slope slope_, Array<> &&existing, TileIndex start_, TileIndex end_)
{ {
::Slope slope = (::Slope)slope_; ::Slope slope = (::Slope)slope_;
int32 start = start_; int32 start = start_;
@ -420,7 +420,7 @@ static bool NormaliseTileOffset(int32 *tile)
if (HasBit(rb, i)) existing.emplace_back(neighbours[i]); if (HasBit(rb, i)) existing.emplace_back(neighbours[i]);
} }
return ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::GetSlope(tile), existing, start - tile, end - tile); return ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::GetSlope(tile), std::move(existing), start - tile, end - tile);
} }
/** /**

View File

@ -246,7 +246,7 @@ public:
* they are build or 2 when building the first part automatically * they are build or 2 when building the first part automatically
* builds the second part. -1 means the preconditions are not met. * builds the second part. -1 means the preconditions are not met.
*/ */
static SQInteger CanBuildConnectedRoadParts(ScriptTile::Slope slope, Array<> existing, TileIndex start, TileIndex end); static SQInteger CanBuildConnectedRoadParts(ScriptTile::Slope slope, Array<> &&existing, TileIndex start, TileIndex end);
/** /**
* Lookup function for building road parts independent of whether the * Lookup function for building road parts independent of whether the

View File

@ -23,19 +23,6 @@ template <class CL, ScriptType ST> const char *GetClassName();
* The Squirrel convert routines * The Squirrel convert routines
*/ */
namespace SQConvert { namespace SQConvert {
/**
* Pointers assigned to this class will be free'd when this instance
* comes out of scope. Useful to make sure you can use stredup(),
* without leaking memory.
*/
struct SQAutoFreePointers : std::vector<void *> {
~SQAutoFreePointers()
{
for (void * p : *this) free(p);
}
};
/** /**
* To return a value to squirrel, we use this helper class. It converts to the right format. * To return a value to squirrel, we use this helper class. It converts to the right format.
* We use a class instead of a plain function to allow us to use partial template specializations. * We use a class instead of a plain function to allow us to use partial template specializations.
@ -74,21 +61,21 @@ namespace SQConvert {
*/ */
template <typename T> struct Param; template <typename T> struct Param;
template <> struct Param<uint8> { static inline uint8 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<uint8> { static inline uint8 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<uint16> { static inline uint16 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<uint16> { static inline uint16 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<uint32> { static inline uint32 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<uint32> { static inline uint32 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<int8> { static inline int8 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<int8> { static inline int8 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<int16> { static inline int16 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<int16> { static inline int16 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<int32> { static inline int32 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<int32> { static inline int32 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<int64> { static inline int64 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<int64> { static inline int64 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, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)tmp); } }; template <> struct Param<TileIndex> { static inline TileIndex Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)tmp); } };
template <> struct Param<Money> { static inline Money Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return 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, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } }; 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<const char *> { /* Do not use const char *, use std::string& instead. */ };
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } }; template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
template <> struct Param<const std::string &> { template <> struct Param<const std::string &> {
static inline const std::string Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) static inline const std::string Get(HSQUIRRELVM vm, int index)
{ {
/* Convert what-ever there is as parameter to a string */ /* Convert what-ever there is as parameter to a string */
sq_tostring(vm, index); sq_tostring(vm, index);
@ -102,8 +89,8 @@ namespace SQConvert {
}; };
template <typename Titem> template <typename Titem>
struct Param<Array<Titem>> { struct Param<Array<Titem> &&> {
static inline Array<Titem> Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) static inline Array<Titem> Get(HSQUIRRELVM vm, int index)
{ {
/* Sanity check of the size. */ /* Sanity check of the size. */
if (sq_getsize(vm, index) > UINT16_MAX) throw sq_throwerror(vm, "an array used as parameter to a function is too large"); if (sq_getsize(vm, index) > UINT16_MAX) throw sq_throwerror(vm, "an array used as parameter to a function is too large");
@ -116,7 +103,7 @@ namespace SQConvert {
Array<Titem> data; Array<Titem> data;
while (SQ_SUCCEEDED(sq_next(vm, -2))) { while (SQ_SUCCEEDED(sq_next(vm, -2))) {
data.emplace_back(Param<Titem>::Get(vm, -1, ptr)); data.emplace_back(Param<Titem>::Get(vm, -1));
sq_pop(vm, 2); sq_pop(vm, 2);
} }
sq_pop(vm, 2); sq_pop(vm, 2);
@ -146,15 +133,14 @@ namespace SQConvert {
template <size_t... i> template <size_t... i>
static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
{ {
[[maybe_unused]] SQAutoFreePointers ptr;
if constexpr (std::is_void_v<Tretval>) { if constexpr (std::is_void_v<Tretval>) {
(*func)( (*func)(
Param<Targs>::Get(vm, 2 + i, &ptr)... Param<Targs>::Get(vm, 2 + i)...
); );
return 0; return 0;
} else { } else {
Tretval ret = (*func)( Tretval ret = (*func)(
Param<Targs>::Get(vm, 2 + i, &ptr)... Param<Targs>::Get(vm, 2 + i)...
); );
return Return<Tretval>::Set(vm, ret); return Return<Tretval>::Set(vm, ret);
} }
@ -180,15 +166,14 @@ namespace SQConvert {
template <size_t... i> template <size_t... i>
static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
{ {
[[maybe_unused]] SQAutoFreePointers ptr;
if constexpr (std::is_void_v<Tretval>) { if constexpr (std::is_void_v<Tretval>) {
(instance->*func)( (instance->*func)(
Param<Targs>::Get(vm, 2 + i, &ptr)... Param<Targs>::Get(vm, 2 + i)...
); );
return 0; return 0;
} else { } else {
Tretval ret = (instance->*func)( Tretval ret = (instance->*func)(
Param<Targs>::Get(vm, 2 + i, &ptr)... Param<Targs>::Get(vm, 2 + i)...
); );
return Return<Tretval>::Set(vm, ret); return Return<Tretval>::Set(vm, ret);
} }
@ -197,9 +182,8 @@ namespace SQConvert {
template <size_t... i> template <size_t... i>
static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>) static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
{ {
[[maybe_unused]] SQAutoFreePointers ptr;
Tcls *inst = new Tcls( Tcls *inst = new Tcls(
Param<Targs>::Get(vm, 2 + i, &ptr)... Param<Targs>::Get(vm, 2 + i)...
); );
return inst; return inst;