Make CreatureParty use smart pointers.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-31 19:04:40 +02:00
parent 01f6e59cb6
commit 69f6a89c0d
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 8 additions and 12 deletions

View File

@ -10,7 +10,7 @@ export CreatureParty* CreatureLib_CreatureParty_ConstructFromArray(Creature* cre
export void CreatureLib_CreatureParty_Destruct(const CreatureParty* p) { delete p; } export void CreatureLib_CreatureParty_Destruct(const CreatureParty* p) { delete p; }
export uint8_t CreatureLib_CreatureParty_GetAtIndex(Creature*& out, const CreatureParty* p, size_t index) { export uint8_t CreatureLib_CreatureParty_GetAtIndex(Creature*& out, const CreatureParty* p, size_t index) {
Try(out = p->GetAtIndex(index);) Try(out = p->GetAtIndex(index).operator->();)
} }
export uint8_t CreatureLib_CreatureParty_Switch(CreatureParty* p, size_t a, size_t b) { Try(p->Switch(a, b);) } export uint8_t CreatureLib_CreatureParty_Switch(CreatureParty* p, size_t a, size_t b) { Try(p->Switch(a, b);) }

View File

@ -33,7 +33,7 @@ namespace CreatureLib::Battling {
} }
inline bool HasCreaturesNotInField() const { inline bool HasCreaturesNotInField() const {
auto p = _party->GetParty(); auto& p = _party->GetParty();
for (const auto& creature : p) { for (const auto& creature : p) {
if (creature == nullptr) if (creature == nullptr)
continue; continue;

View File

@ -6,20 +6,16 @@
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class CreatureParty { class CreatureParty {
ArbUt::List<Creature*> _party; ArbUt::UniquePtrList<Creature> _party;
public: public:
CreatureParty(size_t size) noexcept : _party(size) {} CreatureParty(size_t size) noexcept : _party(size) {}
CreatureParty(ArbUt::List<Creature*> party) noexcept : _party(party) {} CreatureParty(ArbUt::List<Creature*> party) noexcept : _party(party.GetStdList()) {}
CreatureParty(std::initializer_list<Creature*> party) noexcept : _party(party) {} CreatureParty(std::initializer_list<Creature*> party) noexcept : _party(party) {}
virtual ~CreatureParty() noexcept { virtual ~CreatureParty() = default;
for (auto c : _party) {
delete c;
}
}
Creature* GetAtIndex(size_t index) const noexcept { return _party[index]; } ArbUt::BorrowedPtr<Creature> GetAtIndex(size_t index) const noexcept { return _party[index]; }
void Switch(size_t a, size_t b) noexcept { void Switch(size_t a, size_t b) noexcept {
auto ca = _party[a]; auto ca = _party[a];
@ -38,8 +34,8 @@ namespace CreatureLib::Battling {
return false; return false;
} }
ArbUt::List<Creature*>& GetParty() noexcept { return _party; } ArbUt::UniquePtrList<Creature>& GetParty() noexcept { return _party; }
const ArbUt::List<Creature*>& GetParty() const noexcept { return _party; } const ArbUt::UniquePtrList<Creature>& GetParty() const noexcept { return _party; }
size_t GetLength() const noexcept { return _party.Count(); } size_t GetLength() const noexcept { return _party.Count(); }
}; };