From 67e8fa650f76f5f8c63944f6759061aa58c911d3 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Wed, 24 Aug 2016 23:16:53 +0300 Subject: [PATCH] Update the AABB type to use the new dynamic dispatching system for metamethods. --- source/Base/AABB.cpp | 17 ++++++++++------- source/Base/AABB.hpp | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/source/Base/AABB.cpp b/source/Base/AABB.cpp index 86c0c4ac..8f1ac0ad 100644 --- a/source/Base/AABB.cpp +++ b/source/Base/AABB.cpp @@ -2,6 +2,7 @@ #include "Base/AABB.hpp" #include "Base/Vector4.hpp" #include "Base/Shared.hpp" +#include "Base/DynArg.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { @@ -38,7 +39,8 @@ AABB::AABB(Value sv) // ------------------------------------------------------------------------------------------------ AABB::AABB(Value xv, Value yv, Value zv) - : min(-xv, -yv, -zv), max(std::fabs(xv), std::fabs(yv), std::fabs(zv)) + : min(-std::fabs(xv), -std::fabs(yv), -std::fabs(zv)) + , max(std::fabs(xv), std::fabs(yv), std::fabs(zv)) { /* ... */ } @@ -493,13 +495,14 @@ void Register_AABB(HSQUIRRELVM vm) // Core Meta-methods .Func(_SC("_tostring"), &AABB::ToString) .SquirrelFunc(_SC("_typename"), &AABB::Typename) - .Func(_SC("_cmp"), &AABB::Cmp) + // We cannot set _cmp for c++ classes so we use this instead + .SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) // Meta-methods - .Func< AABB (AABB::*)(const AABB &) const >(_SC("_add"), &AABB::operator +) - .Func< AABB (AABB::*)(const AABB &) const >(_SC("_sub"), &AABB::operator -) - .Func< AABB (AABB::*)(const AABB &) const >(_SC("_mul"), &AABB::operator *) - .Func< AABB (AABB::*)(const AABB &) const >(_SC("_div"), &AABB::operator /) - .Func< AABB (AABB::*)(const AABB &) const >(_SC("_modulo"), &AABB::operator %) + .SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) + .SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) + .SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) + .SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) + .SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >) .Func< AABB (AABB::*)(void) const >(_SC("_unm"), &AABB::operator -) // Properties .Prop(_SC("Abs"), &AABB::Abs) diff --git a/source/Base/AABB.hpp b/source/Base/AABB.hpp index 9c2ffc0b..2a496e36 100644 --- a/source/Base/AABB.hpp +++ b/source/Base/AABB.hpp @@ -267,6 +267,32 @@ struct AABB */ Int32 Cmp(const AABB & b) const; + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare an instance of this type with a scalar value. + */ + Int32 Cmp(Value s) const + { + return Cmp(AABB(s, s, s, s, s, s)); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare an instance of this type with a scalar value. + */ + Int32 Cmp(Int32 s) const + { + const Value v = static_cast< Value >(s); + return Cmp(AABB(v, v, v, v, v, v)); + } + + /* -------------------------------------------------------------------------------------------- + * Used by the script engine to compare an instance of this type with a scalar value. + */ + Int32 Cmp(std::nullptr_t) const + { + const Value v = static_cast< Value >(0); + return Cmp(AABB(v, v, v, v, v, v)); + } + /* -------------------------------------------------------------------------------------------- * Used by the script engine to convert an instance of this type to a string. */