Made SpeciesVariant follow PIMPL idiom.

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-09-25 19:11:56 +02:00
parent 6189919496
commit fbdeaf9e9c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 141 additions and 68 deletions

View File

@ -1,16 +1,122 @@
#include "SpeciesVariant.hpp" #include "SpeciesVariant.hpp"
const CreatureLib::Library::TalentIndex namespace CreatureLib::Library {
CreatureLib::Library::SpeciesVariant::GetTalentIndex(const ArbUt::StringView& talent) const { struct SpeciesVariant::impl {
for (size_t i = 0; i < _talents.Count(); i++) { private:
if (_talents.At(i) == talent) { ArbUt::StringView _name;
return TalentIndex(false, i); float _height;
float _weight;
uint32_t _baseExperience;
ArbUt::List<uint8_t> _types;
Library::StatisticSet<uint16_t> _baseStatistics;
ArbUt::List<ArbUt::StringView> _talents;
ArbUt::List<ArbUt::StringView> _secretTalents;
std::unique_ptr<const LearnableAttacks> _attacks;
std::unordered_set<uint32_t> _flags;
public:
impl(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience,
const ArbUt::List<uint8_t>& types, Library::StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents, const ArbUt::List<ArbUt::StringView>& secretTalents,
const LearnableAttacks* attacks, std::unordered_set<uint32_t> 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<uint8_t>& 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(); }
for (size_t i = 0; i < _secretTalents.Count(); i++) { [[nodiscard]] inline size_t GetSecretTalentCount() const noexcept { return _secretTalents.Count(); }
if (_secretTalents.At(i) == talent) { [[nodiscard]] const ArbUt::StringView& GetTalent(const TalentIndex& index) const {
return TalentIndex(true, i); 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<const CreatureLib::Library::LearnableAttacks>
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<ArbUt::StringView>& GetTalents() const { return _talents; }
[[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& 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<uint8_t>& types, StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents,
const ArbUt::List<ArbUt::StringView>& secretTalents, const LearnableAttacks* attacks,
const std::unordered_set<uint32_t>& 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<uint8_t>&, 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."); TalentIndex SpeciesVariant::GetTalentIndex(const ArbUt::StringView& talent) const {
} return _impl->GetTalentIndex(talent);
}
ImplGetter(ArbUt::BorrowedPtr<const LearnableAttacks>, GetLearnableAttacks);
TalentIndex SpeciesVariant::GetRandomTalent(ArbUt::Random& rand) const noexcept {
return _impl->GetRandomTalent(rand);
}
ImplGetter(const ArbUt::List<ArbUt::StringView>&, GetTalents);
ImplGetter(const ArbUt::List<ArbUt::StringView>&, 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;
}

View File

@ -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. \brief A single species can have more than one variant. This class holds the data for those variants.
*/ */
class SpeciesVariant { class SpeciesVariant {
protected:
ArbUt::StringView _name;
float _height;
float _weight;
uint32_t _baseExperience;
private: private:
ArbUt::List<uint8_t> _types; struct impl;
Library::StatisticSet<uint16_t> _baseStatistics; std::unique_ptr<impl> _impl;
ArbUt::List<ArbUt::StringView> _talents;
ArbUt::List<ArbUt::StringView> _secretTalents;
std::unique_ptr<const LearnableAttacks> _attacks;
std::unordered_set<uint32_t> _flags;
protected:
private:
public: public:
SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience, SpeciesVariant(const ArbUt::StringView& name, float height, float weight, uint32_t baseExperience,
const ArbUt::List<uint8_t>& types, Library::StatisticSet<uint16_t> baseStats, const ArbUt::List<uint8_t>& types, Library::StatisticSet<uint16_t> baseStats,
const ArbUt::List<ArbUt::StringView>& talents, const ArbUt::List<ArbUt::StringView>& talents,
const ArbUt::List<ArbUt::StringView>& secretTalents, const LearnableAttacks* attacks, const ArbUt::List<ArbUt::StringView>& secretTalents, const LearnableAttacks* attacks,
std::unordered_set<uint32_t> flags = {}) const std::unordered_set<uint32_t>& flags = {});
: _name(name), _height(height), _weight(weight), _baseExperience(baseExperience), _types((types)), virtual ~SpeciesVariant();
_baseStatistics(baseStats), _talents(talents), _secretTalents(secretTalents), _attacks(attacks),
_flags(flags){};
virtual ~SpeciesVariant() = default;
inline const ArbUt::StringView& GetName() const { return _name; } const ArbUt::StringView& GetName() const noexcept;
inline float GetHeight() const { return _height; } float GetHeight() const noexcept;
inline float GetWeight() const { return _weight; } float GetWeight() const noexcept;
inline uint32_t GetBaseExperience() const { return _baseExperience; } uint32_t GetBaseExperience() const noexcept;
[[nodiscard]] inline size_t GetTypeCount() const { return _types.Count(); } [[nodiscard]] size_t GetTypeCount() const noexcept;
[[nodiscard]] inline uint8_t GetType(size_t index) const { return _types[index]; } [[nodiscard]] uint8_t GetType(size_t index) const;
[[nodiscard]] inline const ArbUt::List<uint8_t>& GetTypes() const { return _types; } [[nodiscard]] const ArbUt::List<uint8_t>& GetTypes() const noexcept;
[[nodiscard]] inline uint16_t GetStatistic(Library::Statistic stat) const { [[nodiscard]] uint16_t GetStatistic(Library::Statistic stat) const noexcept;
return _baseStatistics.GetStat(stat); [[nodiscard]] size_t GetTalentCount() const noexcept;
} [[nodiscard]] size_t GetSecretTalentCount() const noexcept;
[[nodiscard]] inline size_t GetTalentCount() const noexcept { return _talents.Count(); } [[nodiscard]] const ArbUt::StringView& GetTalent(const TalentIndex& index) const;
[[nodiscard]] inline size_t GetSecretTalentCount() const noexcept { return _secretTalents.Count(); } [[nodiscard]] TalentIndex GetTalentIndex(const ArbUt::StringView& talent) const;
[[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]] inline ArbUt::BorrowedPtr<const CreatureLib::Library::LearnableAttacks> [[nodiscard]] ArbUt::BorrowedPtr<const LearnableAttacks> GetLearnableAttacks() const noexcept;
GetLearnableAttacks() const { [[nodiscard]] TalentIndex GetRandomTalent(ArbUt::Random& rand) const noexcept;
return _attacks; [[nodiscard]] const ArbUt::List<ArbUt::StringView>& GetTalents() const noexcept;
} [[nodiscard]] const ArbUt::List<ArbUt::StringView>& GetSecretTalents() const noexcept;
[[nodiscard]] inline TalentIndex GetRandomTalent(ArbUt::Random& rand) const noexcept {
return TalentIndex(false, rand.Get(_talents.Count()));
}
[[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetTalents() const { return _talents; }
[[nodiscard]] inline const ArbUt::List<ArbUt::StringView>& GetSecretTalents() const { return _secretTalents; }
inline bool HasFlag(const ArbUt::StringView& key) const noexcept { bool HasFlag(const ArbUt::StringView& key) const noexcept;
return this->_flags.find(key) != this->_flags.end(); bool HasFlag(uint32_t keyHash) const noexcept;
}
inline bool HasFlag(uint32_t keyHash) const noexcept {
return this->_flags.find(keyHash) != this->_flags.end();
}
}; };
} }