2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file squirrel_helper.hpp declarations and parts of the implementation of the class for convert code */
|
|
|
|
|
|
|
|
#ifndef SQUIRREL_HELPER_HPP
|
|
|
|
#define SQUIRREL_HELPER_HPP
|
|
|
|
|
2010-08-26 22:01:16 +00:00
|
|
|
#include "squirrel.hpp"
|
2023-04-11 07:40:50 +00:00
|
|
|
#include "../core/alloc_func.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../economy_type.h"
|
2009-02-02 13:46:26 +00:00
|
|
|
#include "../string_func.h"
|
2021-11-06 22:11:22 +00:00
|
|
|
#include "../tile_type.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "squirrel_helper_type.hpp"
|
|
|
|
|
2011-11-29 22:23:33 +00:00
|
|
|
template <class CL, ScriptType ST> const char *GetClassName();
|
2011-11-13 20:52:39 +00:00
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* The Squirrel convert routines
|
|
|
|
*/
|
|
|
|
namespace SQConvert {
|
|
|
|
/**
|
2022-01-16 23:54:59 +00:00
|
|
|
* 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.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2022-01-16 23:54:59 +00:00
|
|
|
template <typename T> struct Return;
|
|
|
|
|
|
|
|
template <> struct Return<uint8> { static inline int Set(HSQUIRRELVM vm, uint8 res) { sq_pushinteger(vm, (int32)res); return 1; } };
|
|
|
|
template <> struct Return<uint16> { static inline int Set(HSQUIRRELVM vm, uint16 res) { sq_pushinteger(vm, (int32)res); return 1; } };
|
|
|
|
template <> struct Return<uint32> { static inline int Set(HSQUIRRELVM vm, uint32 res) { sq_pushinteger(vm, (int32)res); return 1; } };
|
|
|
|
template <> struct Return<int8> { static inline int Set(HSQUIRRELVM vm, int8 res) { sq_pushinteger(vm, res); return 1; } };
|
|
|
|
template <> struct Return<int16> { static inline int Set(HSQUIRRELVM vm, int16 res) { sq_pushinteger(vm, res); return 1; } };
|
|
|
|
template <> struct Return<int32> { static inline int Set(HSQUIRRELVM vm, int32 res) { sq_pushinteger(vm, res); return 1; } };
|
|
|
|
template <> struct Return<int64> { static inline int Set(HSQUIRRELVM vm, int64 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)res.value); return 1; } };
|
|
|
|
template <> struct Return<bool> { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } };
|
2023-05-05 21:19:35 +00:00
|
|
|
template <> struct Return<char *> { /* Do not use char *, use std::optional<std::string> instead. */ };
|
|
|
|
template <> struct Return<const char *> { /* Do not use const char *, use std::optional<std::string> instead. */ };
|
2022-01-16 23:54:59 +00:00
|
|
|
template <> struct Return<void *> { static inline int Set(HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; } };
|
|
|
|
template <> struct Return<HSQOBJECT> { static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res); return 1; } };
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2023-05-05 21:19:35 +00:00
|
|
|
template <> struct Return<std::optional<std::string>> {
|
|
|
|
static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res) {
|
|
|
|
if (res.has_value()) {
|
|
|
|
sq_pushstring(vm, res.value(), -1);
|
|
|
|
} else {
|
|
|
|
sq_pushnull(vm);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
2022-01-16 23:54:59 +00:00
|
|
|
* To get a param from 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.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2022-01-16 23:54:59 +00:00
|
|
|
template <typename T> struct Param;
|
|
|
|
|
2023-05-06 08:24:31 +00:00
|
|
|
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) { 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) { 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) { 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) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)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; } };
|
2023-05-06 08:07:54 +00:00
|
|
|
template <> struct Param<const char *> { /* Do not use const char *, use std::string& instead. */ };
|
2023-05-06 08:24:31 +00:00
|
|
|
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
|
2022-01-16 23:54:59 +00:00
|
|
|
|
2023-05-05 17:31:06 +00:00
|
|
|
template <> struct Param<const std::string &> {
|
2023-05-06 08:24:31 +00:00
|
|
|
static inline const std::string Get(HSQUIRRELVM vm, int index)
|
2023-05-05 17:31:06 +00:00
|
|
|
{
|
|
|
|
/* Convert what-ever there is as parameter to a string */
|
|
|
|
sq_tostring(vm, index);
|
|
|
|
|
|
|
|
const SQChar *tmp;
|
|
|
|
sq_getstring(vm, -1, &tmp);
|
|
|
|
std::string result = StrMakeValid(tmp);
|
|
|
|
sq_poptop(vm);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-23 23:49:57 +00:00
|
|
|
template <typename Titem>
|
2023-05-06 08:24:31 +00:00
|
|
|
struct Param<Array<Titem> &&> {
|
|
|
|
static inline Array<Titem> Get(HSQUIRRELVM vm, int index)
|
2022-01-16 23:54:59 +00:00
|
|
|
{
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
SQObject obj;
|
|
|
|
sq_getstackobj(vm, index, &obj);
|
|
|
|
sq_pushobject(vm, obj);
|
|
|
|
sq_pushnull(vm);
|
|
|
|
|
2023-02-23 23:49:57 +00:00
|
|
|
Array<Titem> data;
|
2022-01-16 23:54:59 +00:00
|
|
|
|
|
|
|
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
2023-05-06 08:24:31 +00:00
|
|
|
data.emplace_back(Param<Titem>::Get(vm, -1));
|
2022-01-16 23:54:59 +00:00
|
|
|
sq_pop(vm, 2);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
sq_pop(vm, 2);
|
|
|
|
|
2023-02-23 23:49:57 +00:00
|
|
|
return data;
|
2022-01-16 23:54:59 +00:00
|
|
|
}
|
|
|
|
};
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-03 17:30:08 +00:00
|
|
|
* Helper class to recognize the function type (retval type, args) and use the proper specialization
|
|
|
|
* for SQ callback. The partial specializations for the second arg (Tis_void_retval) are not possible
|
|
|
|
* on the function. Therefore the class is used instead.
|
|
|
|
*/
|
2021-12-19 14:17:10 +00:00
|
|
|
template <typename Tfunc> struct HelperT;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-19 14:17:10 +00:00
|
|
|
* The real C++ caller for functions.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2021-12-19 14:17:10 +00:00
|
|
|
template <typename Tretval, typename... Targs>
|
|
|
|
struct HelperT<Tretval (*)(Targs...)> {
|
|
|
|
static int SQCall(void *instance, Tretval(*func)(Targs...), HSQUIRRELVM vm)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2021-12-19 14:17:10 +00:00
|
|
|
return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 14:17:10 +00:00
|
|
|
private:
|
|
|
|
template <size_t... i>
|
|
|
|
static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2021-12-19 14:17:10 +00:00
|
|
|
if constexpr (std::is_void_v<Tretval>) {
|
|
|
|
(*func)(
|
2023-05-06 08:24:31 +00:00
|
|
|
Param<Targs>::Get(vm, 2 + i)...
|
2021-12-19 14:17:10 +00:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
Tretval ret = (*func)(
|
2023-05-06 08:24:31 +00:00
|
|
|
Param<Targs>::Get(vm, 2 + i)...
|
2021-12-19 14:17:10 +00:00
|
|
|
);
|
2022-01-16 23:54:59 +00:00
|
|
|
return Return<Tretval>::Set(vm, ret);
|
2021-12-19 14:17:10 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-12-19 14:17:10 +00:00
|
|
|
* The real C++ caller for methods.
|
2009-01-12 17:11:45 +00:00
|
|
|
*/
|
2021-12-19 14:17:10 +00:00
|
|
|
template <class Tcls, typename Tretval, typename... Targs>
|
|
|
|
struct HelperT<Tretval(Tcls:: *)(Targs...)> {
|
|
|
|
static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2021-12-19 14:17:10 +00:00
|
|
|
return SQCall(instance, func, vm, std::index_sequence_for<Targs...>{});
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 14:17:10 +00:00
|
|
|
static Tcls *SQConstruct(Tcls *instance, Tretval(Tcls:: *func)(Targs...), HSQUIRRELVM vm)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2021-12-19 14:17:10 +00:00
|
|
|
return SQConstruct(instance, func, vm, std::index_sequence_for<Targs...>{});
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 14:17:10 +00:00
|
|
|
private:
|
|
|
|
template <size_t... i>
|
|
|
|
static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2021-12-19 14:17:10 +00:00
|
|
|
if constexpr (std::is_void_v<Tretval>) {
|
|
|
|
(instance->*func)(
|
2023-05-06 08:24:31 +00:00
|
|
|
Param<Targs>::Get(vm, 2 + i)...
|
2021-12-19 14:17:10 +00:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
Tretval ret = (instance->*func)(
|
2023-05-06 08:24:31 +00:00
|
|
|
Param<Targs>::Get(vm, 2 + i)...
|
2021-12-19 14:17:10 +00:00
|
|
|
);
|
2022-01-16 23:54:59 +00:00
|
|
|
return Return<Tretval>::Set(vm, ret);
|
2021-12-19 14:17:10 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 14:17:10 +00:00
|
|
|
template <size_t... i>
|
|
|
|
static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
Tcls *inst = new Tcls(
|
2023-05-06 08:24:31 +00:00
|
|
|
Param<Targs>::Get(vm, 2 + i)...
|
2009-01-12 17:11:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A general template for all non-static method callbacks from Squirrel.
|
|
|
|
* In here the function_proc is recovered, and the SQCall is called that
|
|
|
|
* can handle this exact amount of params.
|
|
|
|
*/
|
2011-11-29 22:23:33 +00:00
|
|
|
template <typename Tcls, typename Tmethod, ScriptType Ttype>
|
2009-01-12 17:11:45 +00:00
|
|
|
inline SQInteger DefSQNonStaticCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
/* Find the amount of params we got */
|
|
|
|
int nparam = sq_gettop(vm);
|
2019-04-10 21:07:06 +00:00
|
|
|
SQUserPointer ptr = nullptr;
|
|
|
|
SQUserPointer real_instance = nullptr;
|
2009-01-12 17:11:45 +00:00
|
|
|
HSQOBJECT instance;
|
|
|
|
|
|
|
|
/* Get the 'SQ' instance of this class */
|
|
|
|
Squirrel::GetInstance(vm, &instance);
|
|
|
|
|
|
|
|
/* Protect against calls to a non-static method in a static way */
|
|
|
|
sq_pushroottable(vm);
|
2011-11-29 22:23:33 +00:00
|
|
|
const char *className = GetClassName<Tcls, Ttype>();
|
2014-09-06 17:30:33 +00:00
|
|
|
sq_pushstring(vm, className, -1);
|
2009-01-12 17:11:45 +00:00
|
|
|
sq_get(vm, -2);
|
|
|
|
sq_pushobject(vm, instance);
|
2014-09-06 17:46:56 +00:00
|
|
|
if (sq_instanceof(vm) != SQTrue) return sq_throwerror(vm, "class method is non-static");
|
2009-01-12 17:11:45 +00:00
|
|
|
sq_pop(vm, 3);
|
|
|
|
|
|
|
|
/* Get the 'real' instance of this class */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getinstanceup(vm, 1, &real_instance, nullptr);
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Get the real function pointer */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getuserdata(vm, nparam, &ptr, nullptr);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (real_instance == nullptr) return sq_throwerror(vm, "couldn't detect real instance of class for non-static call");
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Remove the userdata from the stack */
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
|
|
|
|
try {
|
|
|
|
/* Delegate it to a template that can handle this specific function */
|
|
|
|
return HelperT<Tmethod>::SQCall((Tcls *)real_instance, *(Tmethod *)ptr, vm);
|
2019-05-15 19:14:13 +00:00
|
|
|
} catch (SQInteger &e) {
|
2009-01-12 17:11:45 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A general template for all non-static advanced method callbacks from Squirrel.
|
|
|
|
* In here the function_proc is recovered, and the SQCall is called that
|
|
|
|
* can handle this exact amount of params.
|
|
|
|
*/
|
2011-11-29 22:23:33 +00:00
|
|
|
template <typename Tcls, typename Tmethod, ScriptType Ttype>
|
2009-01-12 17:11:45 +00:00
|
|
|
inline SQInteger DefSQAdvancedNonStaticCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
/* Find the amount of params we got */
|
|
|
|
int nparam = sq_gettop(vm);
|
2019-04-10 21:07:06 +00:00
|
|
|
SQUserPointer ptr = nullptr;
|
|
|
|
SQUserPointer real_instance = nullptr;
|
2009-01-12 17:11:45 +00:00
|
|
|
HSQOBJECT instance;
|
|
|
|
|
|
|
|
/* Get the 'SQ' instance of this class */
|
|
|
|
Squirrel::GetInstance(vm, &instance);
|
|
|
|
|
|
|
|
/* Protect against calls to a non-static method in a static way */
|
|
|
|
sq_pushroottable(vm);
|
2011-11-29 22:23:33 +00:00
|
|
|
const char *className = GetClassName<Tcls, Ttype>();
|
2014-09-06 17:30:33 +00:00
|
|
|
sq_pushstring(vm, className, -1);
|
2009-01-12 17:11:45 +00:00
|
|
|
sq_get(vm, -2);
|
|
|
|
sq_pushobject(vm, instance);
|
2014-09-06 17:46:56 +00:00
|
|
|
if (sq_instanceof(vm) != SQTrue) return sq_throwerror(vm, "class method is non-static");
|
2009-01-12 17:11:45 +00:00
|
|
|
sq_pop(vm, 3);
|
|
|
|
|
|
|
|
/* Get the 'real' instance of this class */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getinstanceup(vm, 1, &real_instance, nullptr);
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Get the real function pointer */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getuserdata(vm, nparam, &ptr, nullptr);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (real_instance == nullptr) return sq_throwerror(vm, "couldn't detect real instance of class for non-static call");
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Remove the userdata from the stack */
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
|
|
|
|
/* Call the function, which its only param is always the VM */
|
|
|
|
return (SQInteger)(((Tcls *)real_instance)->*(*(Tmethod *)ptr))(vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A general template for all function/static method callbacks from Squirrel.
|
|
|
|
* In here the function_proc is recovered, and the SQCall is called that
|
|
|
|
* can handle this exact amount of params.
|
|
|
|
*/
|
|
|
|
template <typename Tcls, typename Tmethod>
|
|
|
|
inline SQInteger DefSQStaticCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
/* Find the amount of params we got */
|
|
|
|
int nparam = sq_gettop(vm);
|
2019-04-10 21:07:06 +00:00
|
|
|
SQUserPointer ptr = nullptr;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* Get the real function pointer */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getuserdata(vm, nparam, &ptr, nullptr);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
/* Delegate it to a template that can handle this specific function */
|
2019-04-10 21:07:06 +00:00
|
|
|
return HelperT<Tmethod>::SQCall((Tcls *)nullptr, *(Tmethod *)ptr, vm);
|
2019-05-15 19:14:13 +00:00
|
|
|
} catch (SQInteger &e) {
|
2009-01-12 17:11:45 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 21:00:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A general template for all static advanced method callbacks from Squirrel.
|
|
|
|
* In here the function_proc is recovered, and the SQCall is called that
|
|
|
|
* can handle this exact amount of params.
|
|
|
|
*/
|
|
|
|
template <typename Tcls, typename Tmethod>
|
|
|
|
inline SQInteger DefSQAdvancedStaticCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
/* Find the amount of params we got */
|
|
|
|
int nparam = sq_gettop(vm);
|
2019-04-10 21:07:06 +00:00
|
|
|
SQUserPointer ptr = nullptr;
|
2011-12-19 21:00:32 +00:00
|
|
|
|
|
|
|
/* Get the real function pointer */
|
2021-06-16 19:10:41 +00:00
|
|
|
sq_getuserdata(vm, nparam, &ptr, nullptr);
|
2011-12-19 21:00:32 +00:00
|
|
|
/* Remove the userdata from the stack */
|
|
|
|
sq_pop(vm, 1);
|
|
|
|
|
|
|
|
/* Call the function, which its only param is always the VM */
|
|
|
|
return (SQInteger)(*(*(Tmethod *)ptr))(vm);
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/**
|
|
|
|
* A general template for the destructor of SQ instances. This is needed
|
|
|
|
* here as it has to be in the same scope as DefSQConstructorCallback.
|
|
|
|
*/
|
|
|
|
template <typename Tcls>
|
|
|
|
static SQInteger DefSQDestructorCallback(SQUserPointer p, SQInteger size)
|
|
|
|
{
|
|
|
|
/* Remove the real instance too */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (p != nullptr) ((Tcls *)p)->Release();
|
2009-01-12 17:11:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A general template to handle creating of instance with any amount of
|
|
|
|
* params. It creates the instance in C++, and it sets all the needed
|
|
|
|
* settings in SQ to register the instance.
|
|
|
|
*/
|
|
|
|
template <typename Tcls, typename Tmethod, int Tnparam>
|
|
|
|
inline SQInteger DefSQConstructorCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
/* Create the real instance */
|
2019-04-10 21:07:06 +00:00
|
|
|
Tcls *instance = HelperT<Tmethod>::SQConstruct((Tcls *)nullptr, (Tmethod)nullptr, vm);
|
2009-01-12 17:11:45 +00:00
|
|
|
sq_setinstanceup(vm, -Tnparam, instance);
|
|
|
|
sq_setreleasehook(vm, -Tnparam, DefSQDestructorCallback<Tcls>);
|
|
|
|
instance->AddRef();
|
|
|
|
return 0;
|
2019-05-15 19:14:13 +00:00
|
|
|
} catch (SQInteger &e) {
|
2009-01-12 17:11:45 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-21 14:55:28 +00:00
|
|
|
/**
|
|
|
|
* A general template to handle creating of an instance with a complex
|
|
|
|
* constructor.
|
|
|
|
*/
|
|
|
|
template <typename Tcls>
|
|
|
|
inline SQInteger DefSQAdvancedConstructorCallback(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
/* Find the amount of params we got */
|
|
|
|
int nparam = sq_gettop(vm);
|
|
|
|
|
|
|
|
/* Create the real instance */
|
|
|
|
Tcls *instance = new Tcls(vm);
|
|
|
|
sq_setinstanceup(vm, -nparam, instance);
|
|
|
|
sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
|
|
|
|
instance->AddRef();
|
|
|
|
return 0;
|
2019-05-15 19:14:13 +00:00
|
|
|
} catch (SQInteger &e) {
|
2011-12-21 14:55:28 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-23 22:37:18 +00:00
|
|
|
} // namespace SQConvert
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
#endif /* SQUIRREL_HELPER_HPP */
|