1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-21 20:27:13 +01:00

Use a standard implementation to check for type conversion.

This commit is contained in:
Sandu Liviu Catalin 2018-07-29 14:35:51 +03:00
parent 0ad290ca34
commit 2dcd9ee986

View File

@ -46,27 +46,6 @@
namespace Sqrat { namespace Sqrat {
/// @cond DEV
// copied from http://www.experts-exchange.com/Programming/Languages/CPP/A_223-Determing-if-a-C-type-is-convertable-to-another-at-compile-time.html
template <typename T1, typename T2>
struct is_convertible
{
private:
struct True_ { char x[2]; };
struct False_ { };
static True_ helper(T2 const &);
static False_ helper(...);
static T1* dummy;
public:
static bool const YES = (
sizeof(True_) == sizeof(is_convertible::helper(*dummy))
);
};
template <typename T, bool b> template <typename T, bool b>
struct popAsInt struct popAsInt
{ {
@ -171,20 +150,20 @@ struct Var {
if (ptr != NULL) { if (ptr != NULL) {
value = *ptr; value = *ptr;
#if !defined (SCRAT_NO_ERROR_CHECKING) #if !defined (SCRAT_NO_ERROR_CHECKING)
} else if (is_convertible<T, SQInteger>::YES) { /* value is likely of integral type like enums */ } else if (std::is_convertible<T, SQInteger>::value) { /* value is likely of integral type like enums */
SQCLEAR(vm); // clear the previous error SQCLEAR(vm); // clear the previous error
value = popAsInt<T, is_convertible<T, SQInteger>::YES>(vm, idx).value; value = popAsInt<T, std::is_convertible<T, SQInteger>::value>(vm, idx).value;
#endif #endif
} else { } else {
// initialize value to avoid warnings // initialize value to avoid warnings
value = popAsInt<T, is_convertible<T, SQInteger>::YES>(vm, idx).value; value = popAsInt<T, std::is_convertible<T, SQInteger>::value>(vm, idx).value;
} }
SQCATCH(vm) { SQCATCH(vm) {
#if defined (SCRAT_USE_EXCEPTIONS) #if defined (SCRAT_USE_EXCEPTIONS)
SQUNUSED(e); // avoid "unreferenced local variable" warning SQUNUSED(e); // avoid "unreferenced local variable" warning
#endif #endif
if (is_convertible<T, SQInteger>::YES) { /* value is likely of integral type like enums */ if (std::is_convertible<T, SQInteger>::value) { /* value is likely of integral type like enums */
value = popAsInt<T, is_convertible<T, SQInteger>::YES>(vm, idx).value; value = popAsInt<T, std::is_convertible<T, SQInteger>::value>(vm, idx).value;
} else { } else {
SQRETHROW(vm); SQRETHROW(vm);
} }
@ -202,7 +181,7 @@ struct Var {
if (ClassType<T>::hasClassData(vm)) if (ClassType<T>::hasClassData(vm))
ClassType<T>::PushInstanceCopy(vm, value); ClassType<T>::PushInstanceCopy(vm, value);
else /* try integral type */ else /* try integral type */
pushAsInt<T, is_convertible<T, SQInteger>::YES>().push(vm, value); pushAsInt<T, std::is_convertible<T, SQInteger>::value>().push(vm, value);
} }
private: private:
@ -258,7 +237,7 @@ struct Var<T&> {
if (ClassType<T>::hasClassData(vm)) if (ClassType<T>::hasClassData(vm))
ClassType<T>::PushInstance(vm, &value); ClassType<T>::PushInstance(vm, &value);
else /* try integral type */ else /* try integral type */
pushAsInt<T, is_convertible<T, SQInteger>::YES>().push(vm, value); pushAsInt<T, std::is_convertible<T, SQInteger>::value>().push(vm, value);
} }
private: private:
@ -1092,7 +1071,6 @@ SCRAT_MAKE_NONREFERENCABLE(signed __int64)
SCRAT_MAKE_NONREFERENCABLE(std::string) SCRAT_MAKE_NONREFERENCABLE(std::string)
#endif #endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Pushes a value on to a given VM's stack /// Pushes a value on to a given VM's stack
/// ///
@ -1112,7 +1090,6 @@ inline void PushVar(HSQUIRRELVM vm, T* value) {
Var<T*>::push(vm, value); Var<T*>::push(vm, value);
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Pushes a value on to a given VM's stack /// Pushes a value on to a given VM's stack
/// ///
@ -1132,6 +1109,22 @@ inline void PushVar(HSQUIRRELVM vm, const T& value) {
Var<T>::push(vm, value); Var<T>::push(vm, value);
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Pushes a pack of values on to a given VM's stack
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
inline void PushVars(HSQUIRRELVM vm, T value) {
PushVar(vm, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Pushes a pack of values on to a given VM's stack
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, class... Ts>
inline void PushVars(HSQUIRRELVM vm, T value, Ts &&... tail) {
PushVar(vm, value);
PushVars(vm, std::forward<Ts>(tail)...);
}
/// @cond DEV /// @cond DEV
template<class T, bool b> template<class T, bool b>