Make CreatureSpecies follow PIMPL idiom.
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
parent
3a4eb9264e
commit
d4505f4293
|
@ -8,7 +8,6 @@ namespace CreatureLib::Library {
|
|||
protected:
|
||||
ArbUt::Dictionary<uint32_t, std::unique_ptr<const T>> _values;
|
||||
ArbUt::List<uint32_t> _listValues;
|
||||
size_t _index;
|
||||
|
||||
public:
|
||||
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
|
||||
|
|
|
@ -2,31 +2,54 @@
|
|||
|
||||
using namespace CreatureLib::Library;
|
||||
|
||||
CreatureSpecies::CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
|
||||
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate,
|
||||
std::unordered_set<uint32_t> flags)
|
||||
struct CreatureSpecies::impl {
|
||||
const ArbUt::StringView _name;
|
||||
uint16_t _id;
|
||||
float _genderRate;
|
||||
const ArbUt::StringView _growthRate;
|
||||
uint8_t _captureRate;
|
||||
|
||||
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup;
|
||||
ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList;
|
||||
std::unordered_set<uint32_t> _flags;
|
||||
|
||||
impl(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant, float genderRatio,
|
||||
const ArbUt::StringView& growthRate, uint8_t captureRate, std::unordered_set<uint32_t> flags)
|
||||
: _name(name), _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
|
||||
_variantsLookup(1), _variantsList(1), _flags(std::move(flags)) {
|
||||
AssertNotNull(defaultVariant)
|
||||
SetVariant("default"_cnc, defaultVariant);
|
||||
}
|
||||
|
||||
bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
~impl() { _variantsLookup.Clear(); }
|
||||
|
||||
inline uint16_t GetId() const noexcept { return _id; }
|
||||
inline float GetGenderRate() const noexcept { return _genderRate; }
|
||||
inline const ArbUt::StringView& GetGrowthRate() const noexcept { return _growthRate; }
|
||||
inline uint8_t GetCaptureRate() const noexcept { return _captureRate; }
|
||||
|
||||
[[nodiscard]] inline bool HasVariant(const ArbUt::BasicStringView& key) const noexcept {
|
||||
return _variantsLookup.Has(key);
|
||||
}
|
||||
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); }
|
||||
[[nodiscard]] inline bool TryGetVariant(const ArbUt::BasicStringView& name,
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
return TryGetVariant(name.GetHash(), out);
|
||||
}
|
||||
[[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
auto find = _variantsLookup.GetStdMap().find(hash);
|
||||
if (find == _variantsLookup.end())
|
||||
return false;
|
||||
out = std::get<1>(*find);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CreatureSpecies::SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) {
|
||||
Assert(!name.IsEmpty())
|
||||
AssertNotNull(variant)
|
||||
_variantsList.CreateBack(variant);
|
||||
_variantsLookup.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)});
|
||||
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const {
|
||||
return _variantsLookup.Get(key);
|
||||
}
|
||||
|
||||
Gender CreatureSpecies::GetRandomGender(ArbUt::Random& rand) const noexcept {
|
||||
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const {
|
||||
return _variantsLookup.Get(key);
|
||||
}
|
||||
[[nodiscard]] Gender GetRandomGender(ArbUt::Random& rand) const noexcept {
|
||||
if (this->_genderRate == -1) {
|
||||
return Gender::Genderless;
|
||||
}
|
||||
|
@ -35,3 +58,63 @@ Gender CreatureSpecies::GetRandomGender(ArbUt::Random& rand) const noexcept {
|
|||
return Gender ::Female;
|
||||
return Gender ::Male;
|
||||
}
|
||||
[[nodiscard]] inline const ArbUt::StringView& GetName() const noexcept { return _name; }
|
||||
|
||||
void SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) {
|
||||
Assert(!name.IsEmpty())
|
||||
AssertNotNull(variant)
|
||||
_variantsList.CreateBack(variant);
|
||||
_variantsLookup.GetStdMap().insert({name, std::unique_ptr<const SpeciesVariant>(variant)});
|
||||
}
|
||||
|
||||
inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const {
|
||||
return _variantsList;
|
||||
}
|
||||
|
||||
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(); }
|
||||
};
|
||||
|
||||
CreatureSpecies::CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
|
||||
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate,
|
||||
std::unordered_set<uint32_t> flags)
|
||||
: _impl(new impl(id, name, defaultVariant, genderRatio, growthRate, captureRate, flags)) {}
|
||||
|
||||
#define ImplGetter(type, func) \
|
||||
type CreatureSpecies::func() const noexcept { return _impl->func(); }
|
||||
|
||||
CreatureSpecies::~CreatureSpecies() = default;
|
||||
ImplGetter(uint16_t, GetId);
|
||||
ImplGetter(float, GetGenderRate);
|
||||
ImplGetter(const ArbUt::StringView&, GetGrowthRate);
|
||||
ImplGetter(uint8_t, GetCaptureRate);
|
||||
ImplGetter(const ArbUt::StringView&, GetName);
|
||||
|
||||
bool CreatureSpecies::HasVariant(const ArbUt::BasicStringView& key) const noexcept { return _impl->HasVariant(key); }
|
||||
bool CreatureSpecies::HasVariant(uint32_t hash) const noexcept { return _impl->HasVariant(hash); }
|
||||
|
||||
bool CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name,
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
return _impl->TryGetVariant(name, out);
|
||||
}
|
||||
bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
return _impl->TryGetVariant(hash, out);
|
||||
}
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant> CreatureSpecies::GetVariant(const ArbUt::BasicStringView& key) const {
|
||||
return _impl->GetVariant(key);
|
||||
}
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant> CreatureSpecies::GetVariant(uint32_t key) const {
|
||||
return _impl->GetVariant(key);
|
||||
}
|
||||
Gender CreatureSpecies::GetRandomGender(ArbUt::Random& rand) const noexcept { return _impl->GetRandomGender(rand); }
|
||||
void CreatureSpecies::SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant) {
|
||||
_impl->SetVariant(name, variant);
|
||||
}
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& CreatureSpecies::GetVariantsIterator() const {
|
||||
return _impl->GetVariantsIterator();
|
||||
}
|
||||
|
||||
bool CreatureSpecies::HasFlag(const ArbUt::StringView& key) const noexcept { return _impl->HasFlag(key); }
|
||||
bool CreatureSpecies::HasFlag(uint32_t keyHash) const noexcept { return _impl->HasFlag(keyHash); }
|
||||
|
|
|
@ -10,59 +10,37 @@ namespace CreatureLib::Library {
|
|||
creatures with.
|
||||
*/
|
||||
class CreatureSpecies {
|
||||
const ArbUt::StringView _name;
|
||||
uint16_t _id;
|
||||
float _genderRate;
|
||||
const ArbUt::StringView _growthRate;
|
||||
uint8_t _captureRate;
|
||||
|
||||
ArbUt::Dictionary<uint32_t, std::unique_ptr<const SpeciesVariant>> _variantsLookup;
|
||||
ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>> _variantsList;
|
||||
std::unordered_set<uint32_t> _flags;
|
||||
struct impl;
|
||||
std::unique_ptr<impl> _impl;
|
||||
|
||||
public:
|
||||
CreatureSpecies(uint16_t id, const ArbUt::StringView& name, const SpeciesVariant* defaultVariant,
|
||||
float genderRatio, const ArbUt::StringView& growthRate, uint8_t captureRate,
|
||||
std::unordered_set<uint32_t> flags = {});
|
||||
|
||||
virtual ~CreatureSpecies() noexcept { _variantsLookup.Clear(); }
|
||||
virtual ~CreatureSpecies();
|
||||
|
||||
inline uint16_t GetId() const noexcept { return _id; }
|
||||
inline float GetGenderRate() const noexcept { return _genderRate; }
|
||||
inline const ArbUt::StringView& GetGrowthRate() const noexcept { return _growthRate; }
|
||||
inline uint8_t GetCaptureRate() const noexcept { return _captureRate; }
|
||||
uint16_t GetId() const noexcept;
|
||||
float GetGenderRate() const noexcept;
|
||||
const ArbUt::StringView& GetGrowthRate() const noexcept;
|
||||
uint8_t GetCaptureRate() const noexcept;
|
||||
|
||||
[[nodiscard]] inline bool HasVariant(const ArbUt::BasicStringView& key) const noexcept {
|
||||
return _variantsLookup.Has(key);
|
||||
}
|
||||
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); }
|
||||
[[nodiscard]] inline bool TryGetVariant(const ArbUt::BasicStringView& name,
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
|
||||
return TryGetVariant(name.GetHash(), out);
|
||||
}
|
||||
[[nodiscard]] bool HasVariant(const ArbUt::BasicStringView& key) const noexcept;
|
||||
[[nodiscard]] bool HasVariant(uint32_t hash) const noexcept;
|
||||
[[nodiscard]] bool TryGetVariant(const ArbUt::BasicStringView& name,
|
||||
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept;
|
||||
[[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept;
|
||||
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant>
|
||||
GetVariant(const ArbUt::BasicStringView& key) const {
|
||||
return _variantsLookup.Get(key);
|
||||
}
|
||||
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const {
|
||||
return _variantsLookup.Get(key);
|
||||
}
|
||||
[[nodiscard]] ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const;
|
||||
[[nodiscard]] ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(uint32_t key) const;
|
||||
[[nodiscard]] Gender GetRandomGender(ArbUt::Random& rand) const noexcept;
|
||||
[[nodiscard]] inline const ArbUt::StringView& GetName() const { return _name; }
|
||||
[[nodiscard]] const ArbUt::StringView& GetName() const noexcept;
|
||||
|
||||
void SetVariant(const ArbUt::StringView& name, const SpeciesVariant* variant);
|
||||
|
||||
inline const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const {
|
||||
return _variantsList;
|
||||
}
|
||||
const ArbUt::List<ArbUt::BorrowedPtr<const SpeciesVariant>>& GetVariantsIterator() const;
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef CREATURELIB_PRECOMPILED_HXX
|
||||
#define CREATURELIB_PRECOMPILED_HXX
|
||||
|
||||
// std
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
|
Loading…
Reference in New Issue