From fbdeaf9e9cd7b1795783d03d7e858cf4b98ffde3 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 25 Sep 2020 19:11:56 +0200 Subject: [PATCH] Made SpeciesVariant follow PIMPL idiom. Signed-off-by: Deukhoofd --- src/Library/CreatureData/SpeciesVariant.cpp | 128 ++++++++++++++++++-- src/Library/CreatureData/SpeciesVariant.hpp | 81 ++++--------- 2 files changed, 141 insertions(+), 68 deletions(-) diff --git a/src/Library/CreatureData/SpeciesVariant.cpp b/src/Library/CreatureData/SpeciesVariant.cpp index 17e5507..3ebfa9f 100644 --- a/src/Library/CreatureData/SpeciesVariant.cpp +++ b/src/Library/CreatureData/SpeciesVariant.cpp @@ -1,16 +1,122 @@ #include "SpeciesVariant.hpp" -const CreatureLib::Library::TalentIndex -CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ArbUt::StringView& talent) const { - for (size_t i = 0; i < _talents.Count(); i++) { - if (_talents.At(i) == talent) { - return TalentIndex(false, i); +namespace CreatureLib::Library { + struct SpeciesVariant::impl { + private: + ArbUt::StringView _name; + float _height; + float _weight; + uint32_t _baseExperience; + ArbUt::List _types; + Library::StatisticSet _baseStatistics; + ArbUt::List _talents; + ArbUt::List _secretTalents; + std::unique_ptr _attacks; + std::unordered_set _flags; + + public: + impl(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience, + const ArbUt::List& types, Library::StatisticSet baseStats, + const ArbUt::List& talents, const ArbUt::List& secretTalents, + const LearnableAttacks* attacks, std::unordered_set flags) + : _name(name), _height(height), _weight(weight), _baseExperience(baseExperience), _types((types)), + _baseStatistics(baseStats), _talents(talents), _secretTalents(secretTalents), _attacks(attacks), + _flags(flags){}; + + inline const ArbUt::StringView& GetName() const { return _name; } + inline float GetHeight() const { return _height; } + inline float GetWeight() const { return _weight; } + inline uint32_t GetBaseExperience() const { return _baseExperience; } + + [[nodiscard]] inline size_t GetTypeCount() const { return _types.Count(); } + [[nodiscard]] inline uint8_t GetType(size_t index) const { return _types[index]; } + [[nodiscard]] inline const ArbUt::List& GetTypes() const { return _types; } + [[nodiscard]] inline uint16_t GetStatistic(Library::Statistic stat) const { + return _baseStatistics.GetStat(stat); } - } - for (size_t i = 0; i < _secretTalents.Count(); i++) { - if (_secretTalents.At(i) == talent) { - return TalentIndex(true, i); + [[nodiscard]] inline size_t GetTalentCount() const noexcept { return _talents.Count(); } + [[nodiscard]] inline size_t GetSecretTalentCount() const noexcept { return _secretTalents.Count(); } + [[nodiscard]] const ArbUt::StringView& GetTalent(const TalentIndex& index) const { + if (index.IsSecret() && _secretTalents.Count() > 0) { + auto i = index.GetIndex(); + if (i > _secretTalents.Count()) + i = _secretTalents.Count(); + return _secretTalents.At(i); + } + auto i = index.GetIndex(); + if (i > _talents.Count()) + i = _talents.Count(); + return _talents.At(i); } + [[nodiscard]] TalentIndex GetTalentIndex(const ArbUt::StringView& talent) const { + for (size_t i = 0; i < _talents.Count(); i++) { + if (_talents.At(i) == talent) { + return TalentIndex(false, i); + } + } + for (size_t i = 0; i < _secretTalents.Count(); i++) { + if (_secretTalents.At(i) == talent) { + return TalentIndex(true, i); + } + } + THROW("The given talent is not a valid talent for this creature."); + } + + [[nodiscard]] inline ArbUt::BorrowedPtr + GetLearnableAttacks() const { + return _attacks; + } + [[nodiscard]] inline TalentIndex GetRandomTalent(ArbUt::Random& rand) const noexcept { + return TalentIndex(false, rand.Get(_talents.Count())); + } + [[nodiscard]] inline const ArbUt::List& GetTalents() const { return _talents; } + [[nodiscard]] inline const ArbUt::List& GetSecretTalents() const { return _secretTalents; } + + inline bool HasFlag(const ArbUt::StringView& key) const noexcept { + return this->_flags.find(key) != this->_flags.end(); + } + inline bool HasFlag(uint32_t keyHash) const noexcept { + return this->_flags.find(keyHash) != this->_flags.end(); + } + }; + + SpeciesVariant::SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience, + const ArbUt::List& types, StatisticSet baseStats, + const ArbUt::List& talents, + const ArbUt::List& secretTalents, const LearnableAttacks* attacks, + const std::unordered_set& flags) + : _impl(new impl(name, height, weight, baseExperience, types, baseStats, talents, secretTalents, attacks, + flags)) {} + +#define ImplGetter(type, func) \ + type SpeciesVariant::func() const noexcept { return _impl->func(); } + + ImplGetter(const ArbUt::StringView&, GetName); + ImplGetter(float, GetHeight); + ImplGetter(float, GetWeight); + ImplGetter(uint32_t, GetBaseExperience); + ImplGetter(size_t, GetTypeCount); + uint8_t SpeciesVariant::GetType(size_t index) const { return _impl->GetType(index); } + ImplGetter(const ArbUt::List&, GetTypes); + uint16_t SpeciesVariant::GetStatistic(Library::Statistic stat) const noexcept { return _impl->GetStatistic(stat); } + + ImplGetter(size_t, GetTalentCount); + ImplGetter(size_t, GetSecretTalentCount); + const ArbUt::StringView& SpeciesVariant::GetTalent(const TalentIndex& index) const { + return _impl->GetTalent(index); } - THROW("The given talent is not a valid talent for this creature."); -} \ No newline at end of file + TalentIndex SpeciesVariant::GetTalentIndex(const ArbUt::StringView& talent) const { + return _impl->GetTalentIndex(talent); + } + ImplGetter(ArbUt::BorrowedPtr, GetLearnableAttacks); + TalentIndex SpeciesVariant::GetRandomTalent(ArbUt::Random& rand) const noexcept { + return _impl->GetRandomTalent(rand); + } + ImplGetter(const ArbUt::List&, GetTalents); + ImplGetter(const ArbUt::List&, GetSecretTalents); + + bool SpeciesVariant::HasFlag(const ArbUt::StringView& key) const noexcept { return _impl->HasFlag(key); } + bool SpeciesVariant::HasFlag(uint32_t keyHash) const noexcept { return _impl->HasFlag(keyHash); } + + SpeciesVariant::~SpeciesVariant() = default; +} diff --git a/src/Library/CreatureData/SpeciesVariant.hpp b/src/Library/CreatureData/SpeciesVariant.hpp index d86d2d4..6ddc6fb 100644 --- a/src/Library/CreatureData/SpeciesVariant.hpp +++ b/src/Library/CreatureData/SpeciesVariant.hpp @@ -10,74 +10,41 @@ namespace CreatureLib::Library { \brief A single species can have more than one variant. This class holds the data for those variants. */ class SpeciesVariant { - protected: - ArbUt::StringView _name; - float _height; - float _weight; - uint32_t _baseExperience; - private: - ArbUt::List _types; - Library::StatisticSet _baseStatistics; - ArbUt::List _talents; - ArbUt::List _secretTalents; - std::unique_ptr _attacks; - std::unordered_set _flags; + struct impl; + std::unique_ptr _impl; + protected: + private: public: SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience, const ArbUt::List& types, Library::StatisticSet baseStats, const ArbUt::List& talents, const ArbUt::List& secretTalents, const LearnableAttacks* attacks, - std::unordered_set flags = {}) - : _name(name), _height(height), _weight(weight), _baseExperience(baseExperience), _types((types)), - _baseStatistics(baseStats), _talents(talents), _secretTalents(secretTalents), _attacks(attacks), - _flags(flags){}; - virtual ~SpeciesVariant() = default; + const std::unordered_set& flags = {}); + virtual ~SpeciesVariant(); - inline const ArbUt::StringView& GetName() const { return _name; } - inline float GetHeight() const { return _height; } - inline float GetWeight() const { return _weight; } - inline uint32_t GetBaseExperience() const { return _baseExperience; } + const ArbUt::StringView& GetName() const noexcept; + float GetHeight() const noexcept; + float GetWeight() const noexcept; + uint32_t GetBaseExperience() const noexcept; - [[nodiscard]] inline size_t GetTypeCount() const { return _types.Count(); } - [[nodiscard]] inline uint8_t GetType(size_t index) const { return _types[index]; } - [[nodiscard]] inline const ArbUt::List& GetTypes() const { return _types; } - [[nodiscard]] inline uint16_t GetStatistic(Library::Statistic stat) const { - return _baseStatistics.GetStat(stat); - } - [[nodiscard]] inline size_t GetTalentCount() const noexcept { return _talents.Count(); } - [[nodiscard]] inline size_t GetSecretTalentCount() const noexcept { return _secretTalents.Count(); } - [[nodiscard]] const ArbUt::StringView& GetTalent(const TalentIndex& index) const { - if (index.IsSecret() && _secretTalents.Count() > 0) { - auto i = index.GetIndex(); - if (i > _secretTalents.Count()) - i = _secretTalents.Count(); - return _secretTalents.At(i); - } - auto i = index.GetIndex(); - if (i > _talents.Count()) - i = _talents.Count(); - return _talents.At(i); - } - [[nodiscard]] const TalentIndex GetTalentIndex(const ArbUt::StringView& talent) const; + [[nodiscard]] size_t GetTypeCount() const noexcept; + [[nodiscard]] uint8_t GetType(size_t index) const; + [[nodiscard]] const ArbUt::List& GetTypes() const noexcept; + [[nodiscard]] uint16_t GetStatistic(Library::Statistic stat) const noexcept; + [[nodiscard]] size_t GetTalentCount() const noexcept; + [[nodiscard]] size_t GetSecretTalentCount() const noexcept; + [[nodiscard]] const ArbUt::StringView& GetTalent(const TalentIndex& index) const; + [[nodiscard]] TalentIndex GetTalentIndex(const ArbUt::StringView& talent) const; - [[nodiscard]] inline ArbUt::BorrowedPtr - GetLearnableAttacks() const { - return _attacks; - } - [[nodiscard]] inline TalentIndex GetRandomTalent(ArbUt::Random& rand) const noexcept { - return TalentIndex(false, rand.Get(_talents.Count())); - } - [[nodiscard]] inline const ArbUt::List& GetTalents() const { return _talents; } - [[nodiscard]] inline const ArbUt::List& GetSecretTalents() const { return _secretTalents; } + [[nodiscard]] ArbUt::BorrowedPtr GetLearnableAttacks() const noexcept; + [[nodiscard]] TalentIndex GetRandomTalent(ArbUt::Random& rand) const noexcept; + [[nodiscard]] const ArbUt::List& GetTalents() const noexcept; + [[nodiscard]] const ArbUt::List& GetSecretTalents() const noexcept; - inline bool HasFlag(const ArbUt::StringView& key) const noexcept { - return this->_flags.find(key) != this->_flags.end(); - } - inline bool HasFlag(uint32_t keyHash) const noexcept { - return this->_flags.find(keyHash) != this->_flags.end(); - } + bool HasFlag(const ArbUt::StringView& key) const noexcept; + bool HasFlag(uint32_t keyHash) const noexcept; }; }