More using the getter macro, adds support for non const getter functions.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-09-21 23:14:26 +02:00
parent 2e8cf4379b
commit 29fe82f04d
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 43 additions and 18 deletions

View File

@ -48,7 +48,6 @@ void RegisterBattleClass::RegisterBattleRandom(asIScriptEngine* engine) {
Ensure(r >= 0); Ensure(r >= 0);
} }
BORROWED_PTR_GETTER_FUNC(PkmnLib::Battling::Battle, CreatureLib::Battling::ChoiceQueue, GetCurrentTurnQueue);
CreatureLib::Battling::BattleSide* GetBattleSideWrapper(PkmnLib::Battling::Battle* battle, u8 index) { CreatureLib::Battling::BattleSide* GetBattleSideWrapper(PkmnLib::Battling::Battle* battle, u8 index) {
return battle->GetSides()[index]; return battle->GetSides()[index];
} }
@ -98,24 +97,19 @@ void RegisterBattleClass::RegisterBattleSide(asIScriptEngine* engine) {
} }
void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) { void RegisterBattleClass::RegisterBattle(asIScriptEngine* engine) {
int r = engine->RegisterObjectMethod("Battle", "const BattleLibrary@ get_Library() const property", REGISTER_GETTER("Battle", "const BattleLibrary@ get_Library() const property", CreatureLib::Battling::Battle,
asMETHOD(PkmnLib::Battling::Battle, GetLibrary), asCALL_THISCALL); GetLibrary);
Ensure(r >= 0); REGISTER_GETTER("Battle", "bool get_CanFlee() const property", CreatureLib::Battling::Battle,
r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)", CanFlee);
asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL); REGISTER_GETTER("Battle", "uint get_CurrentTurn() const property", CreatureLib::Battling::Battle,
Ensure(r >= 0); GetCurrentTurn);
r = engine->RegisterObjectMethod("Battle", "bool get_CanFlee() const property", REGISTER_GETTER("Battle", "BattleRandom@ get_Random() const property", CreatureLib::Battling::Battle,
asMETHOD(PkmnLib::Battling::Battle, CanFlee), asCALL_THISCALL); GetRandom);
Ensure(r >= 0); REGISTER_GETTER("Battle", "ChoiceQueue@ get_TurnQueue() const property", CreatureLib::Battling::Battle,
r = engine->RegisterObjectMethod("Battle", "uint get_CurrentTurn() const property", GetCurrentTurnQueue);
asMETHOD(PkmnLib::Battling::Battle, GetCurrentTurn), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Battle", "BattleRandom@ get_Random() const property", auto r = engine->RegisterObjectMethod("Battle", "bool CanUse(BaseTurnChoice@ choice)",
asMETHOD(PkmnLib::Battling::Battle, GetRandom), asCALL_THISCALL); asMETHOD(PkmnLib::Battling::Battle, CanUse), asCALL_THISCALL);
Ensure(r >= 0);
r = engine->RegisterObjectMethod("Battle", "ChoiceQueue@ get_TurnQueue() const property",
asFUNCTION(GetCurrentTurnQueueWrapper), asCALL_CDECL_OBJFIRST);
Ensure(r >= 0); Ensure(r >= 0);
r = engine->RegisterObjectMethod("Battle", "ref@ AddVolatile(const constString &in name)", r = engine->RegisterObjectMethod("Battle", "ref@ AddVolatile(const constString &in name)",
asFUNCTION(AddVolatileWrapper), asCALL_CDECL_OBJFIRST); asFUNCTION(AddVolatileWrapper), asCALL_CDECL_OBJFIRST);

View File

@ -63,5 +63,36 @@ void RegisterGetter(asIScriptEngine* engine, const char* asType, const char* asD
} }
} }
template <typename T, typename R, R (T::*Method)()>
void RegisterGetter(asIScriptEngine* engine, const char* asType, const char* asDef) {
if constexpr (std::is_enum<R>()) {
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<R, ArbUt::BorrowedPtr>::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<R, std::unique_ptr>::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<R, ArbUt::OptionalBorrowedPtr>::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<R>::value) {
static_assert(!std::is_enum<R>());
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<R>());
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) \ #define REGISTER_GETTER(asType, asDef, cType, cFunc) \
RegisterGetter<cType, std::result_of<decltype (&cType::cFunc)(cType)>::type, &cType::cFunc>(engine, asType, asDef); RegisterGetter<cType, std::result_of<decltype (&cType::cFunc)(cType)>::type, &cType::cFunc>(engine, asType, asDef);