From 29fe82f04d6301ce90ee7764a84071002ac926e3 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 21 Sep 2021 23:14:26 +0200 Subject: [PATCH] More using the getter macro, adds support for non const getter functions. --- .../Battling/RegisterBattleClass.cpp | 30 +++++++----------- .../AngelScript/TypeRegistry/HelperFile.hpp | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp index df5117e..4ecb143 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterBattleClass.cpp @@ -48,7 +48,6 @@ void RegisterBattleClass::RegisterBattleRandom(asIScriptEngine* engine) { Ensure(r >= 0); } -BORROWED_PTR_GETTER_FUNC(PkmnLib::Battling::Battle, CreatureLib::Battling::ChoiceQueue, GetCurrentTurnQueue); CreatureLib::Battling::BattleSide* GetBattleSideWrapper(PkmnLib::Battling::Battle* battle, u8 index) { return battle->GetSides()[index]; } @@ -98,24 +97,19 @@ void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) { } void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { - int r = engine->RegisterObjectMethod("Battle", "const BattleLibrary@ get_Library() const property", - asMETHOD(PkmnLib::Battling::Battle, GetLibrary), asCALL_THISCALL); - Ensure(r >= 0); - r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)", - asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL); - Ensure(r >= 0); - r = engine->RegisterObjectMethod("Battle", "bool get_CanFlee() const property", - asMETHOD(PkmnLib::Battling::Battle, CanFlee), asCALL_THISCALL); - Ensure(r >= 0); - r = engine->RegisterObjectMethod("Battle", "uint get_CurrentTurn() const property", - asMETHOD(PkmnLib::Battling::Battle, GetCurrentTurn), asCALL_THISCALL); - Ensure(r >= 0); + REGISTER_GETTER("Battle", "const BattleLibrary@ get_Library() const property", CreatureLib::Battling::Battle, + GetLibrary); + REGISTER_GETTER("Battle", "bool get_CanFlee() const property", CreatureLib::Battling::Battle, + CanFlee); + REGISTER_GETTER("Battle", "uint get_CurrentTurn() const property", CreatureLib::Battling::Battle, + GetCurrentTurn); + REGISTER_GETTER("Battle", "BattleRandom@ get_Random() const property", CreatureLib::Battling::Battle, + GetRandom); + REGISTER_GETTER("Battle", "ChoiceQueue@ get_TurnQueue() const property", CreatureLib::Battling::Battle, + GetCurrentTurnQueue); - r = engine->RegisterObjectMethod("Battle", "BattleRandom@ get_Random() const property", - asMETHOD(PkmnLib::Battling::Battle, GetRandom), asCALL_THISCALL); - Ensure(r >= 0); - r = engine->RegisterObjectMethod("Battle", "ChoiceQueue@ get_TurnQueue() const property", - asFUNCTION(GetCurrentTurnQueueWrapper), asCALL_CDECL_OBJFIRST); + auto r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)", + asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL); Ensure(r >= 0); r = engine->RegisterObjectMethod("Battle", "ref@ AddVolatile(const constString &in name)", asFUNCTION(AddVolatileWrapper), asCALL_CDECL_OBJFIRST); diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/HelperFile.hpp b/src/ScriptResolving/AngelScript/TypeRegistry/HelperFile.hpp index 046de31..ef9b547 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/HelperFile.hpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/HelperFile.hpp @@ -63,5 +63,36 @@ void RegisterGetter(asIScriptEngine* engine, const char* asType, const char* asD } } +template +void RegisterGetter(asIScriptEngine* engine, const char* asType, const char* asDef) { + if constexpr (std::is_enum()) { + auto l = [](T* p) { return (i32)(p->*Method)(); }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), i32), asCALL_CDECL_OBJFIRST) >= 0); + } else if constexpr (is_specialization::value) { + auto l = [](T* p) { return (void*)(p->*Method)().GetRaw(); }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), void*), asCALL_CDECL_OBJFIRST) >= 0); + } else if constexpr (is_specialization::value) { + auto l = [](T* p) { return (void*)(p->*Method)().get(); }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), void*), asCALL_CDECL_OBJFIRST) >= 0); + } else if constexpr (is_specialization::value) { + auto l = [](const T* p) { + auto v = (p->*Method)(); + if (v.HasValue()) { + return (void*)v.GetValue(); + } + return (void*)nullptr; + }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), void*), asCALL_CDECL_OBJFIRST) >= 0); + } else if constexpr (std::is_reference::value) { + static_assert(!std::is_enum()); + auto l = [](T* p) { return (void*)&(p->*Method)(); }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), void*), asCALL_CDECL_OBJFIRST) >= 0); + } else { + static_assert(!std::is_enum()); + auto l = [](T* p) { return (p->*Method)(); }; + Ensure(engine->RegisterObjectMethod(asType, asDef, asFUNCTIONPR(l, (T*), R), asCALL_CDECL_OBJFIRST) >= 0); + } +} + #define REGISTER_GETTER(asType, asDef, cType, cFunc) \ RegisterGetter::type, &cType::cFunc>(engine, asType, asDef);