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