2015-11-01 09:06:54 +01:00
|
|
|
//
|
|
|
|
// SqratOverloadMethods: Overload Methods
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copyright (c) 2009 Brandon Jones
|
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would be
|
|
|
|
// appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
// misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if !defined(_SQRAT_OVERLOAD_METHODS_H_)
|
|
|
|
#define _SQRAT_OVERLOAD_METHODS_H_
|
|
|
|
|
2016-02-23 16:48:30 +01:00
|
|
|
#ifdef SQMOD_PLUGIN_API
|
2016-02-27 10:57:10 +01:00
|
|
|
#include <SqAPI.h>
|
2016-02-23 16:48:30 +01:00
|
|
|
#else
|
2019-04-24 21:33:42 +02:00
|
|
|
#include <squirrelex.h>
|
2016-02-23 16:48:30 +01:00
|
|
|
#endif // SQMOD_PLUGIN_API
|
|
|
|
|
2015-11-01 09:06:54 +01:00
|
|
|
#include "sqratTypes.h"
|
|
|
|
#include "sqratUtil.h"
|
|
|
|
#include "sqratGlobalMethods.h"
|
|
|
|
#include "sqratMemberMethods.h"
|
|
|
|
|
|
|
|
namespace Sqrat {
|
|
|
|
|
|
|
|
/// @cond DEV
|
|
|
|
|
|
|
|
//
|
|
|
|
// Overload name generator
|
|
|
|
//
|
|
|
|
|
|
|
|
class SqOverloadName {
|
|
|
|
public:
|
|
|
|
|
2018-10-26 20:56:06 +02:00
|
|
|
static void Get(const SQChar* name, int args, string & out) {
|
|
|
|
SQChar buf[16] = {'_', 'o'};
|
2019-02-09 18:49:04 +01:00
|
|
|
unsigned l = I32ToA(args, &buf[2]);
|
2019-02-17 16:09:32 +01:00
|
|
|
out.append(buf, l+2);
|
2018-10-26 20:56:06 +02:00
|
|
|
out.push_back('_');
|
|
|
|
out.append(name);
|
|
|
|
}
|
2015-11-01 09:06:54 +01:00
|
|
|
|
2018-10-26 20:56:06 +02:00
|
|
|
static string Get(const SQChar* name, int args) {
|
|
|
|
string out;
|
|
|
|
out.reserve(15);
|
|
|
|
Get(name, args, out);
|
|
|
|
return out;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-10-26 20:56:06 +02:00
|
|
|
//
|
|
|
|
// Don't include the overloaded call forwarder into the templated class
|
|
|
|
// to avoid duplicating code that doesn't need to be specialized.
|
|
|
|
//
|
|
|
|
inline SQInteger OverloadExecutionForwarder(HSQUIRRELVM vm) {
|
2018-10-28 16:59:41 +01:00
|
|
|
const SQInteger top = sq_gettop(vm);
|
2018-10-26 20:56:06 +02:00
|
|
|
// Get the argument count
|
2018-10-28 16:59:41 +01:00
|
|
|
const int argCount = static_cast<int>(top - 2);
|
2018-10-26 20:56:06 +02:00
|
|
|
// Subtract environment and base name in free variable^
|
|
|
|
const SQChar* funcName;
|
|
|
|
SQInteger funcNameSize;
|
|
|
|
// Get the un-mangled function name (free variable)
|
|
|
|
sq_getstringandsize(vm, -1, &funcName, &funcNameSize);
|
|
|
|
// Generate the overload mangled name
|
|
|
|
string overloadName;
|
|
|
|
overloadName.reserve(funcNameSize+5);
|
|
|
|
SqOverloadName::Get(funcName, argCount, overloadName);
|
|
|
|
// Pop the un-mangled closure name from the stack so we can replace it later
|
|
|
|
sq_poptop(vm); // `funcName` becomes invalid after this
|
|
|
|
// Push the overload mangled name on the stack
|
|
|
|
sq_pushstring(vm, overloadName.c_str(), static_cast<SQInteger>(overloadName.size()));
|
|
|
|
// Lookup the proper overload and get it on the stack
|
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
|
|
|
if (SQ_FAILED(sq_get(vm, 1))) {
|
2018-10-28 16:59:41 +01:00
|
|
|
sq_settop(vm, top); // keep the stack size intact
|
2018-10-26 20:56:06 +02:00
|
|
|
return sq_throwerror(vm, _SC("wrong number of parameters"));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
sq_get(vm, 1);
|
|
|
|
#endif
|
|
|
|
SQFUNCTION f = nullptr;
|
|
|
|
// Get the native closure pointer that we must invoke
|
|
|
|
SQRESULT res = sq_getnativeclosurepointer(vm, -1, &f);
|
2018-10-28 16:59:41 +01:00
|
|
|
if (SQ_FAILED(res)) {
|
|
|
|
return res;
|
2018-10-26 20:56:06 +02:00
|
|
|
// Make sure a native closure pointer is available
|
2018-10-28 16:59:41 +01:00
|
|
|
} else if (!f) {
|
2018-10-26 20:56:06 +02:00
|
|
|
return sq_throwerror(vm, _SC("unable to acquire the proper overload closure"));
|
|
|
|
}
|
|
|
|
// Attempt to get the free variable containing the native closure pointer on the stack
|
2018-10-31 16:34:23 +01:00
|
|
|
sq_getonefreevariable(vm, 0);
|
2018-10-26 20:56:06 +02:00
|
|
|
// This is simply a hack to implement a direct call and gain some performance
|
|
|
|
// Since both closures expect a free variable we simply replace the free variable
|
|
|
|
// containing closure name with the free variable containing the closure pointer
|
2018-10-28 16:59:41 +01:00
|
|
|
sq_settop(vm, top); // keep the stack size intact before invoking the overload
|
|
|
|
// Perform a direct call and return the result back to the caller
|
|
|
|
return f(vm);
|
2018-10-26 20:56:06 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 16:09:32 +01:00
|
|
|
//
|
|
|
|
// Don't include the overloaded call forwarder into the templated class
|
|
|
|
// to avoid duplicating code that doesn't need to be specialized.
|
|
|
|
//
|
|
|
|
inline SQInteger OverloadConstructionForwarder(HSQUIRRELVM vm) {
|
|
|
|
const SQInteger top = sq_gettop(vm);
|
|
|
|
// Get the argument count
|
|
|
|
const int argCount = static_cast<int>(top - 2);
|
|
|
|
// Subtract environment and base name in free variable^
|
|
|
|
const SQChar* funcName;
|
|
|
|
SQInteger funcNameSize;
|
|
|
|
// Get the un-mangled function name (free variable)
|
|
|
|
sq_getstringandsize(vm, -1, &funcName, &funcNameSize);
|
|
|
|
// Generate the overload mangled name
|
|
|
|
string overloadName;
|
|
|
|
overloadName.reserve(funcNameSize+5);
|
|
|
|
SqOverloadName::Get(funcName, argCount, overloadName);
|
|
|
|
// Push the overload mangled name on the stack
|
|
|
|
sq_pushstring(vm, overloadName.c_str(), static_cast<SQInteger>(overloadName.size()));
|
|
|
|
// Lookup the proper overload and get it on the stack
|
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
|
|
|
if (SQ_FAILED(sq_get(vm, 1))) {
|
|
|
|
sq_settop(vm, top); // keep the stack size intact
|
2019-04-30 21:08:54 +02:00
|
|
|
return sq_throwerrorf(vm, _SC("wrong number of parameters. no such constructor overload: `%s`"), overloadName.data());
|
2019-02-17 16:09:32 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
sq_get(vm, 1);
|
|
|
|
#endif
|
|
|
|
SQFUNCTION f = nullptr;
|
|
|
|
// Get the native closure pointer that we must invoke
|
|
|
|
SQRESULT res = sq_getnativeclosurepointer(vm, -1, &f);
|
|
|
|
if (SQ_FAILED(res)) {
|
|
|
|
return res;
|
|
|
|
// Make sure a native closure pointer is available
|
|
|
|
} else if (!f) {
|
|
|
|
return sq_throwerror(vm, _SC("unable to acquire the proper overload closure"));
|
|
|
|
}
|
|
|
|
// Restore the stack size by removing the overload closure object
|
|
|
|
sq_poptop(vm);
|
|
|
|
// Pop the un-mangled closure name from the stack
|
|
|
|
// The constructor doesn't expect any free variables
|
|
|
|
//sq_poptop(vm); // `funcName` becomes invalid after this
|
|
|
|
// Perform a direct call and store the result
|
|
|
|
SQRESULT r = f(vm);
|
|
|
|
// Keep the stack size intact before leaving
|
|
|
|
sq_settop(vm, top);
|
|
|
|
// Return the result back to the caller
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-11-01 09:06:54 +01:00
|
|
|
//
|
|
|
|
// Squirrel Overload Functions
|
|
|
|
//
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class SqOverload {
|
|
|
|
public:
|
|
|
|
|
|
|
|
static SQInteger Func(HSQUIRRELVM vm) {
|
2018-10-26 20:56:06 +02:00
|
|
|
return OverloadExecutionForwarder(vm);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// void return specialization
|
|
|
|
//
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class SqOverload<void> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
static SQInteger Func(HSQUIRRELVM vm) {
|
2018-10-26 20:56:06 +02:00
|
|
|
return OverloadExecutionForwarder(vm);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-29 18:28:46 +02:00
|
|
|
// Global Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class... A> SQFUNCTION SqGlobalOverloadedFunc(R (* /*method*/)(A...)) {
|
2018-07-29 18:28:46 +02:00
|
|
|
return SqGlobal<R>::template GetProxy<2, true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 18:28:46 +02:00
|
|
|
// Global Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class... A> SQFUNCTION SqGlobalOverloadedFunc(R& (* /*method*/)(A...)) {
|
2018-07-29 18:28:46 +02:00
|
|
|
return SqGlobal<R&>::template GetProxy<2, true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 18:28:46 +02:00
|
|
|
// Member Global Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class T,class... A> SQFUNCTION SqMemberGlobalOverloadedFunc(R (* /*method*/)(T, A...)) {
|
2018-07-29 18:28:46 +02:00
|
|
|
return SqGlobal<R>::template GetProxy<1, true, T, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 18:28:46 +02:00
|
|
|
// Member Global Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class T,class... A> SQFUNCTION SqMemberGlobalOverloadedFunc(R& (* /*method*/)(T, A...)) {
|
2018-07-29 18:28:46 +02:00
|
|
|
return SqGlobal<R&>::template GetProxy<1, true, T, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class C, class R,class... A> SQFUNCTION SqMemberOverloadedFunc(R (C::* /*method*/)(A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqMember<C,R>::template GetProxy<true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class C, class R,class... A> SQFUNCTION SqMemberOverloadedFunc(R (C::* /*method*/)(A...) const) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqMember<C,R>::template GetProxyC<true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class C, class R,class... A> SQFUNCTION SqMemberOverloadedFunc(R& (C::* /*method*/)(A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqMember<C,R&>::template GetProxy<true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Overloaded Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class C, class R,class... A> SQFUNCTION SqMemberOverloadedFunc(R& (C::* /*method*/)(A...) const) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqMember<C,R&>::template GetProxyC<true, A...>();
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Overload handler resolver
|
|
|
|
//
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
inline SQFUNCTION SqOverloadFunc(R (* /*method*/)) {
|
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:09:32 +01:00
|
|
|
template <class R, class... A>
|
|
|
|
inline SQFUNCTION SqOverloadFunc(R (* /*method*/)(A...)) {
|
2015-11-01 09:06:54 +01:00
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class C, class R>
|
2019-02-17 16:09:32 +01:00
|
|
|
inline SQFUNCTION SqOverloadFunc(R (C::* /*method*/)) {
|
2015-11-01 09:06:54 +01:00
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:09:32 +01:00
|
|
|
template <class C, class R>
|
|
|
|
inline SQFUNCTION SqOverloadFunc(R (C::* /*method*/)() const ) {
|
2015-11-01 09:06:54 +01:00
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:09:32 +01:00
|
|
|
template <class C, class R, class... A>
|
|
|
|
inline SQFUNCTION SqOverloadFunc(R (C::* /*method*/)(A...) ) {
|
2015-11-01 09:06:54 +01:00
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:09:32 +01:00
|
|
|
template <class C, class R, class... A>
|
|
|
|
inline SQFUNCTION SqOverloadFunc(R (C::* /*method*/)(A...) const ) {
|
2015-11-01 09:06:54 +01:00
|
|
|
return &SqOverload<R>::Func;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Query argument count
|
|
|
|
//
|
2018-07-29 13:41:23 +02:00
|
|
|
template <class R, class... Args>
|
|
|
|
inline int SqGetArgCount(R (* /*method*/)(Args...)) {
|
|
|
|
return sizeof...(Args);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Query member function argument count
|
|
|
|
//
|
2018-07-29 13:41:23 +02:00
|
|
|
template <class C, class R, class... Args>
|
|
|
|
inline int SqGetArgCount(R (C::* /*method*/)(Args...)) {
|
|
|
|
return sizeof...(Args);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Query const member function argument count
|
|
|
|
//
|
2018-07-29 13:41:23 +02:00
|
|
|
template <class C, class R, class... Args>
|
|
|
|
inline int SqGetArgCount(R (C::* /*method*/)(Args...) const) {
|
|
|
|
return sizeof...(Args);
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|