Make the last couple classes use smart pointers.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-06-02 18:30:37 +02:00
parent 7262ae9e8b
commit 657d646fda
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
10 changed files with 33 additions and 25 deletions

View File

@ -6,18 +6,23 @@ export void CreatureLib_EventData_Destruct(const EventData* p) { delete p; }
#define SIMPLE_GET_FUNC(type, name, returnType) \ #define SIMPLE_GET_FUNC(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); } export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); }
#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); }
SIMPLE_GET_FUNC(EventData, GetKind, EventDataKind); SIMPLE_GET_FUNC(EventData, GetKind, EventDataKind);
SIMPLE_GET_FUNC(DamageEvent, GetCreature, Creature*); SIMPLE_GET_FUNC_SMART_PTR(DamageEvent, GetCreature, Creature*);
SIMPLE_GET_FUNC(DamageEvent, GetDamageSource, DamageSource); SIMPLE_GET_FUNC(DamageEvent, GetDamageSource, DamageSource);
SIMPLE_GET_FUNC(DamageEvent, GetOriginalHealth, uint32_t); SIMPLE_GET_FUNC(DamageEvent, GetOriginalHealth, uint32_t);
SIMPLE_GET_FUNC(DamageEvent, GetNewHealth, uint32_t); SIMPLE_GET_FUNC(DamageEvent, GetNewHealth, uint32_t);
SIMPLE_GET_FUNC(HealEvent, GetCreature, Creature*); SIMPLE_GET_FUNC_SMART_PTR(HealEvent, GetCreature, Creature*);
SIMPLE_GET_FUNC(HealEvent, GetOriginalHealth, uint32_t); SIMPLE_GET_FUNC(HealEvent, GetOriginalHealth, uint32_t);
SIMPLE_GET_FUNC(HealEvent, GetNewHealth, uint32_t); SIMPLE_GET_FUNC(HealEvent, GetNewHealth, uint32_t);
SIMPLE_GET_FUNC(FaintEvent, GetCreature, Creature*); SIMPLE_GET_FUNC_SMART_PTR(FaintEvent, GetCreature, Creature*);
export const char* CreatureLib_DisplayTextEvent_GetText(const DisplayTextEvent* p) { return p->GetText().c_str(); } export const char* CreatureLib_DisplayTextEvent_GetText(const DisplayTextEvent* p) { return p->GetText().c_str(); }
#undef SIMPLE_GET_FUNC
#undef SIMPLE_GET_FUNC_SMART_PTR

View File

@ -23,7 +23,7 @@ export void CreatureLib_BaseTurnChoice_Destruct(const BaseTurnChoice* p) { delet
export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); } export returnType CreatureLib_##type##_##name(const type* p) { return p->name().operator->(); }
SIMPLE_GET_FUNC(BaseTurnChoice, GetKind, TurnChoiceKind) SIMPLE_GET_FUNC(BaseTurnChoice, GetKind, TurnChoiceKind)
SIMPLE_GET_FUNC(BaseTurnChoice, GetUser, Creature*) SIMPLE_GET_FUNC_SMART_PTR(BaseTurnChoice, GetUser, Creature*)
SIMPLE_GET_FUNC_SMART_PTR(AttackTurnChoice, GetAttack, LearnedAttack*) SIMPLE_GET_FUNC_SMART_PTR(AttackTurnChoice, GetAttack, LearnedAttack*)
SIMPLE_GET_FUNC(AttackTurnChoice, GetKind, TurnChoiceKind) SIMPLE_GET_FUNC(AttackTurnChoice, GetKind, TurnChoiceKind)
@ -38,7 +38,7 @@ export uint8_t CreatureLib_BaseTurnChoice_GetTargetCreatureIndex(const AttackTur
return p->GetTarget().GetCreatureIndex(); return p->GetTarget().GetCreatureIndex();
} }
SIMPLE_GET_FUNC(SwitchTurnChoice, GetNewCreature, Creature*) SIMPLE_GET_FUNC_SMART_PTR(SwitchTurnChoice, GetNewCreature, Creature*)
#undef SIMPLE_GET_FUNC #undef SIMPLE_GET_FUNC
#undef SIMPLE_GET_FUNC_SMART_PTR #undef SIMPLE_GET_FUNC_SMART_PTR

View File

@ -1,8 +1,10 @@
#ifndef CREATURELIB_EVENTDATA_HPP #ifndef CREATURELIB_EVENTDATA_HPP
#define CREATURELIB_EVENTDATA_HPP #define CREATURELIB_EVENTDATA_HPP
#include <Arbutils/Memory/BorrowedPtr.hpp>
#include "../Models/DamageSource.hpp" #include "../Models/DamageSource.hpp"
#include "EventDataKind.hpp" #include "EventDataKind.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
// Predeclare some classes. // Predeclare some classes.
class Creature; class Creature;
@ -14,7 +16,7 @@ namespace CreatureLib::Battling {
}; };
class DamageEvent : public EventData { class DamageEvent : public EventData {
Creature* _creature; ArbUt::BorrowedPtr<Creature> _creature;
DamageSource _damageSource; DamageSource _damageSource;
uint32_t _originalHealth; uint32_t _originalHealth;
uint32_t _newHealth; uint32_t _newHealth;
@ -23,33 +25,33 @@ namespace CreatureLib::Battling {
DamageEvent(Creature* c, DamageSource s, uint32_t oHealth, uint32_t newHealth) noexcept DamageEvent(Creature* c, DamageSource s, uint32_t oHealth, uint32_t newHealth) noexcept
: _creature(c), _damageSource(s), _originalHealth(oHealth), _newHealth(newHealth) {} : _creature(c), _damageSource(s), _originalHealth(oHealth), _newHealth(newHealth) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Damage; } EventDataKind GetKind() const noexcept override { return EventDataKind ::Damage; }
Creature* GetCreature() const noexcept { return _creature; } const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
DamageSource GetDamageSource() const noexcept { return _damageSource; } DamageSource GetDamageSource() const noexcept { return _damageSource; }
uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } uint32_t GetOriginalHealth() const noexcept { return _originalHealth; }
uint32_t GetNewHealth() const noexcept { return _newHealth; } uint32_t GetNewHealth() const noexcept { return _newHealth; }
}; };
class HealEvent : public EventData { class HealEvent : public EventData {
Creature* _creature; ArbUt::BorrowedPtr<Creature> _creature;
uint32_t _originalHealth; uint32_t _originalHealth;
uint32_t _newHealth; uint32_t _newHealth;
public: public:
HealEvent(Creature* c, uint32_t oHealth, uint32_t newHealth) noexcept HealEvent(ArbUt::BorrowedPtr<Creature> c, uint32_t oHealth, uint32_t newHealth) noexcept
: _creature(c), _originalHealth(oHealth), _newHealth(newHealth) {} : _creature(c), _originalHealth(oHealth), _newHealth(newHealth) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Heal; } EventDataKind GetKind() const noexcept override { return EventDataKind ::Heal; }
Creature* GetCreature() const noexcept { return _creature; } const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
uint32_t GetOriginalHealth() const noexcept { return _originalHealth; } uint32_t GetOriginalHealth() const noexcept { return _originalHealth; }
uint32_t GetNewHealth() const noexcept { return _newHealth; } uint32_t GetNewHealth() const noexcept { return _newHealth; }
}; };
class FaintEvent : public EventData { class FaintEvent : public EventData {
Creature* _creature; ArbUt::BorrowedPtr<Creature> _creature;
public: public:
FaintEvent(Creature* c) noexcept : _creature(c) {} FaintEvent(ArbUt::BorrowedPtr<Creature> c) noexcept : _creature(c) {}
EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; } EventDataKind GetKind() const noexcept override { return EventDataKind ::Faint; }
Creature* GetCreature() const noexcept { return _creature; } const ArbUt::BorrowedPtr<Creature>& GetCreature() const noexcept { return _creature; }
}; };
class DisplayTextEvent : public EventData { class DisplayTextEvent : public EventData {

View File

@ -36,7 +36,7 @@ void BattleSide::SetChoice(BaseTurnChoice* choice) {
_choicesSet++; _choicesSet++;
} }
void BattleSide::SetCreature(Creature* creature, uint8_t index) { void BattleSide::SetCreature(ArbUt::BorrowedPtr<Creature> creature, uint8_t index) {
auto old = _creatures[index]; auto old = _creatures[index];
if (old != nullptr) { if (old != nullptr) {
old->SetOnBattleField(false); old->SetOnBattleField(false);

View File

@ -43,7 +43,7 @@ namespace CreatureLib::Battling {
void SetChoice(BaseTurnChoice* choice); void SetChoice(BaseTurnChoice* choice);
void ResetChoices() noexcept; void ResetChoices() noexcept;
void SetCreature(Creature* creature, uint8_t index); void SetCreature(ArbUt::BorrowedPtr<Creature> creature, uint8_t index);
const ArbUt::BorrowedPtr<Creature>& GetCreature(uint8_t index) const; const ArbUt::BorrowedPtr<Creature>& GetCreature(uint8_t index) const;
bool CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const; bool CreatureOnSide(const ArbUt::BorrowedPtr<Creature>& creature) const;

View File

@ -44,8 +44,9 @@ namespace CreatureLib::Battling {
std::unique_ptr<Script> _script = nullptr; std::unique_ptr<Script> _script = nullptr;
public: public:
ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits, Creature* user, ExecutingAttack(const ArbUt::List<ArbUt::BorrowedPtr<Creature>>& targets, uint8_t numberHits,
const ArbUt::BorrowedPtr<LearnedAttack>& attack, const std::unique_ptr<Script>& script) ArbUt::BorrowedPtr<Creature> user, const ArbUt::BorrowedPtr<LearnedAttack>& attack,
const std::unique_ptr<Script>& script)
: _targets(targets.Count()), _numberHits(numberHits), : _targets(targets.Count()), _numberHits(numberHits),
_hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) { _hits(std::make_unique<HitData[]>(targets.Count() * numberHits)), _user(user), _attack(attack) {
AssertNotNull(user) AssertNotNull(user)

View File

@ -9,16 +9,16 @@ namespace CreatureLib::Battling {
class Creature; class Creature;
class BaseTurnChoice : public ScriptSource { class BaseTurnChoice : public ScriptSource {
Creature* _user; ArbUt::BorrowedPtr<Creature> _user;
int32_t _randomValue; int32_t _randomValue;
protected: protected:
BaseTurnChoice(Creature* user) noexcept : _user(user){}; BaseTurnChoice(ArbUt::BorrowedPtr<Creature> user) noexcept : _user(user){};
public: public:
virtual ~BaseTurnChoice() = default; virtual ~BaseTurnChoice() = default;
[[nodiscard]] virtual TurnChoiceKind GetKind() const noexcept = 0; [[nodiscard]] virtual TurnChoiceKind GetKind() const noexcept = 0;
[[nodiscard]] inline Creature* GetUser() const noexcept { return _user; } [[nodiscard]] inline const ArbUt::BorrowedPtr<Creature>& GetUser() const noexcept { return _user; }
inline void __SetRandomValue(int32_t val) noexcept { _randomValue = val; } inline void __SetRandomValue(int32_t val) noexcept { _randomValue = val; }
inline int32_t __GetRandomValue() const noexcept { return _randomValue; } inline int32_t __GetRandomValue() const noexcept { return _randomValue; }

View File

@ -7,7 +7,7 @@
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class FleeTurnChoice : public BaseTurnChoice { class FleeTurnChoice : public BaseTurnChoice {
public: public:
FleeTurnChoice(Creature* user) : BaseTurnChoice(user) {} FleeTurnChoice(ArbUt::BorrowedPtr<Creature> user) : BaseTurnChoice(user) {}
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Flee; } TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Flee; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); } size_t ScriptCount() const override { return GetUser()->ScriptCount(); }

View File

@ -6,7 +6,7 @@
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class PassTurnChoice : public BaseTurnChoice { class PassTurnChoice : public BaseTurnChoice {
public: public:
PassTurnChoice(Creature* c) : BaseTurnChoice(c) {} PassTurnChoice(ArbUt::BorrowedPtr<Creature> c) : BaseTurnChoice(c) {}
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Pass; } TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Pass; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); } size_t ScriptCount() const override { return GetUser()->ScriptCount(); }

View File

@ -4,15 +4,15 @@
#include "BaseTurnChoice.hpp" #include "BaseTurnChoice.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class SwitchTurnChoice : public BaseTurnChoice { class SwitchTurnChoice : public BaseTurnChoice {
Creature* _newCreature; ArbUt::BorrowedPtr<Creature> _newCreature;
public: public:
SwitchTurnChoice(Creature* user, Creature* newCreature) noexcept SwitchTurnChoice(ArbUt::BorrowedPtr<Creature> user, ArbUt::BorrowedPtr<Creature> newCreature) noexcept
: BaseTurnChoice(user), _newCreature(newCreature) {} : BaseTurnChoice(user), _newCreature(newCreature) {}
TurnChoiceKind GetKind() const noexcept final { return TurnChoiceKind::Switch; } TurnChoiceKind GetKind() const noexcept final { return TurnChoiceKind::Switch; }
inline Creature* GetNewCreature() const noexcept { return _newCreature; } inline const ArbUt::BorrowedPtr<Creature>& GetNewCreature() const noexcept { return _newCreature; }
size_t ScriptCount() const override { return GetUser()->ScriptCount(); } size_t ScriptCount() const override { return GetUser()->ScriptCount(); }
protected: protected: