2015-11-01 09:06:54 +01:00
|
|
|
//
|
|
|
|
// SqratGlobalMethods: Global Methods
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copyright (c) 2009 Brandon Jones
|
|
|
|
// Copyirght 2011 Li-Cheng (Andy) Tai
|
|
|
|
//
|
|
|
|
// 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(_SCRAT_GLOBAL_METHODS_H_)
|
|
|
|
#define _SCRAT_GLOBAL_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
|
|
|
|
#include <squirrel.h>
|
|
|
|
#endif // SQMOD_PLUGIN_API
|
|
|
|
|
2015-11-01 09:06:54 +01:00
|
|
|
#include "sqratTypes.h"
|
|
|
|
|
|
|
|
namespace Sqrat {
|
|
|
|
|
|
|
|
/// @cond DEV
|
|
|
|
|
|
|
|
//
|
|
|
|
// Squirrel Global Functions
|
|
|
|
//
|
2018-07-29 23:58:27 +02:00
|
|
|
template <class R> struct SqGlobalProxy {
|
|
|
|
template <class... A> static SQInteger Run(HSQUIRRELVM vm, SQInteger idx) {
|
2018-10-31 19:20:09 +01:00
|
|
|
ArgFwd<A...>{}.Call(vm, idx, [](HSQUIRRELVM vm, A... a) {
|
2018-07-29 23:58:27 +02:00
|
|
|
typedef R(*M)(A...);
|
|
|
|
M* method;
|
|
|
|
sq_getuserdata(vm, -1, reinterpret_cast<SQUserPointer*>(&method), nullptr);
|
|
|
|
R ret = (*method)(a...);
|
|
|
|
PushVar(vm, ret);
|
|
|
|
});
|
2018-10-24 20:37:51 +02:00
|
|
|
return 1;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// reference return specialization
|
|
|
|
//
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
template <class R> struct SqGlobalProxy<R&> {
|
|
|
|
template <class... A> static SQInteger Run(HSQUIRRELVM vm, SQInteger idx) {
|
2018-10-31 19:20:09 +01:00
|
|
|
ArgFwd<A...>{}.Call(vm, idx, [](HSQUIRRELVM vm, A... a) {
|
2018-07-29 23:58:27 +02:00
|
|
|
typedef R&(*M)(A...);
|
|
|
|
M* method;
|
|
|
|
sq_getuserdata(vm, -1, reinterpret_cast<SQUserPointer*>(&method), nullptr);
|
|
|
|
R& ret = (*method)(a...);
|
|
|
|
PushVarR(vm, ret);
|
|
|
|
});
|
2018-10-24 20:37:51 +02:00
|
|
|
return 1;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// void return specialization
|
|
|
|
//
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
template <> struct SqGlobalProxy<void> {
|
|
|
|
template <class... A> static SQInteger Run(HSQUIRRELVM vm, SQInteger idx) {
|
2018-10-31 19:20:09 +01:00
|
|
|
ArgFwd<A...>{}.Call(vm, idx, [](HSQUIRRELVM vm, A... a) {
|
2018-07-29 23:58:27 +02:00
|
|
|
typedef void(*M)(A...);
|
|
|
|
M* method;
|
|
|
|
sq_getuserdata(vm, -1, reinterpret_cast<SQUserPointer*>(&method), nullptr);
|
|
|
|
(*method)(a...);
|
|
|
|
});
|
2018-10-24 20:37:51 +02:00
|
|
|
return 0;
|
2015-11-01 09:06:54 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
template<bool> struct SqGlobalParamCheck {
|
2018-10-23 20:29:28 +02:00
|
|
|
static inline bool Invalid(SQInteger top, SQInteger count) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return top != count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<> struct SqGlobalParamCheck<true> {
|
2018-10-23 20:29:28 +02:00
|
|
|
static inline bool Invalid(SQInteger top, SQInteger count) {
|
2018-07-30 19:40:52 +02:00
|
|
|
return top < (count - 1);
|
2018-07-29 23:58:27 +02:00
|
|
|
}
|
|
|
|
};
|
2015-11-01 09:06:54 +01:00
|
|
|
|
2016-11-16 10:54:07 +01:00
|
|
|
//
|
|
|
|
// Squirrel Global Functions
|
|
|
|
//
|
2018-07-29 23:58:27 +02:00
|
|
|
template <class R> struct SqGlobal {
|
2018-07-29 19:25:11 +02:00
|
|
|
// Function proxy
|
2018-10-23 20:29:28 +02:00
|
|
|
template <SQInteger startIdx, bool overloaded, class... A> static SQFUNCTION GetProxy() {
|
|
|
|
return +[](HSQUIRRELVM vm) -> SQInteger {
|
2016-11-16 10:54:07 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-07-29 23:58:27 +02:00
|
|
|
if (!SQRAT_CONST_CONDITION(overloaded) &&
|
2018-07-30 20:44:04 +02:00
|
|
|
SqGlobalParamCheck< ArgFwd<A...>::HASFMT >::Invalid(sq_gettop(vm), startIdx + sizeof...(A))) {
|
2018-07-29 19:25:11 +02:00
|
|
|
return sq_throwerror(vm, _SC("wrong number of parameters"));
|
|
|
|
}
|
2016-11-16 10:54:07 +01:00
|
|
|
#endif
|
2018-07-29 23:58:27 +02:00
|
|
|
try {
|
2018-07-30 00:51:02 +02:00
|
|
|
return SqGlobalProxy<R>::template Run<A...>(vm, startIdx);
|
2018-07-29 23:58:27 +02:00
|
|
|
} catch (const Exception& e) {
|
|
|
|
return sq_throwerror(vm, e.what());
|
2018-07-30 20:44:04 +02:00
|
|
|
} catch (...) {
|
|
|
|
return sq_throwerror(vm, _SC("unknown exception occured"));
|
2018-07-29 19:25:11 +02:00
|
|
|
}
|
2018-07-29 23:58:27 +02:00
|
|
|
SQ_UNREACHABLE
|
2018-07-29 19:25:11 +02:00
|
|
|
};
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// reference return specialization
|
|
|
|
//
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
template <class R> struct SqGlobal<R&> {
|
2018-07-29 19:25:11 +02:00
|
|
|
// Function proxy
|
2018-10-23 20:29:28 +02:00
|
|
|
template <SQInteger startIdx, bool overloaded, class... A> static SQFUNCTION GetProxy() {
|
|
|
|
return +[](HSQUIRRELVM vm) -> SQInteger {
|
2016-11-16 10:54:07 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-07-30 19:40:52 +02:00
|
|
|
if (!SQRAT_CONST_CONDITION(overloaded) &&
|
2018-07-30 20:44:04 +02:00
|
|
|
SqGlobalParamCheck< ArgFwd<A...>::HASFMT >::Invalid(sq_gettop(vm), startIdx + sizeof...(A))) {
|
2018-07-29 19:25:11 +02:00
|
|
|
return sq_throwerror(vm, _SC("wrong number of parameters"));
|
|
|
|
}
|
2016-11-16 10:54:07 +01:00
|
|
|
#endif
|
2018-07-29 23:58:27 +02:00
|
|
|
try {
|
2018-07-30 00:51:02 +02:00
|
|
|
return SqGlobalProxy<R&>::template Run<A...>(vm, startIdx);
|
2018-07-29 23:58:27 +02:00
|
|
|
} catch (const Exception& e) {
|
|
|
|
return sq_throwerror(vm, e.what());
|
2018-07-30 20:44:04 +02:00
|
|
|
} catch (...) {
|
|
|
|
return sq_throwerror(vm, _SC("unknown exception occured"));
|
2018-07-29 19:25:11 +02:00
|
|
|
}
|
2018-07-29 23:58:27 +02:00
|
|
|
SQ_UNREACHABLE
|
2018-07-29 19:25:11 +02:00
|
|
|
};
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// void return specialization
|
|
|
|
//
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
template <> struct SqGlobal<void> {
|
2018-07-29 19:25:11 +02:00
|
|
|
// Function proxy
|
2018-10-23 20:29:28 +02:00
|
|
|
template <SQInteger startIdx, bool overloaded, class... A> static SQFUNCTION GetProxy() {
|
|
|
|
return +[](HSQUIRRELVM vm) -> SQInteger {
|
2016-11-16 10:54:07 +01:00
|
|
|
#if !defined (SCRAT_NO_ERROR_CHECKING)
|
2018-07-30 19:40:52 +02:00
|
|
|
if (!SQRAT_CONST_CONDITION(overloaded) &&
|
2018-07-30 20:44:04 +02:00
|
|
|
SqGlobalParamCheck< ArgFwd<A...>::HASFMT >::Invalid(sq_gettop(vm), startIdx + sizeof...(A))) {
|
2018-07-29 19:25:11 +02:00
|
|
|
return sq_throwerror(vm, _SC("wrong number of parameters"));
|
|
|
|
}
|
2016-11-16 10:54:07 +01:00
|
|
|
#endif
|
2018-07-29 23:58:27 +02:00
|
|
|
try {
|
2018-07-30 00:51:02 +02:00
|
|
|
return SqGlobalProxy<void>::Run<A...>(vm, startIdx);
|
2018-07-29 23:58:27 +02:00
|
|
|
} catch (const Exception& e) {
|
|
|
|
return sq_throwerror(vm, e.what());
|
2018-07-30 20:44:04 +02:00
|
|
|
} catch (...) {
|
|
|
|
return sq_throwerror(vm, _SC("unknown exception occured"));
|
2018-07-29 19:25:11 +02:00
|
|
|
}
|
2018-07-29 23:58:27 +02:00
|
|
|
SQ_UNREACHABLE
|
2018-07-29 19:25:11 +02:00
|
|
|
};
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Global Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class... A> SQFUNCTION SqGlobalFunc(R (* /*method*/)(A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqGlobal<R>::template GetProxy<2, false, A...>();
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Global Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class... A> SQFUNCTION SqGlobalFunc(R& (* /*method*/)(A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqGlobal<R&>::template GetProxy<2, false, A...>();
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Global Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class T,class... A> SQFUNCTION SqMemberGlobalFunc(R (* /*method*/)(T, A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqGlobal<R>::template GetProxy<1, false, T, A...>();
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 23:58:27 +02:00
|
|
|
// Member Global Function Resolver
|
2018-10-23 20:29:28 +02:00
|
|
|
template <class R,class T,class... A> SQFUNCTION SqMemberGlobalFunc(R& (* /*method*/)(T, A...)) {
|
2018-07-29 23:58:27 +02:00
|
|
|
return SqGlobal<R&>::template GetProxy<1, false, T, A...>();
|
2016-11-16 10:54:07 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 09:06:54 +01:00
|
|
|
/// @endcond
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|