Remove shared_ptr, instead use borrowed_ptr to more accurately depict ownership of the objects in the BaseLibraries.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-26 14:13:30 +02:00
parent d82792e27a
commit 36208da2fb
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
11 changed files with 63 additions and 63 deletions

View File

@ -11,10 +11,10 @@ export uint8_t CreatureLib_Creature_Construct(Creature*& out, const BattleLibrar
const char* nickname, bool secretTalent, uint8_t talent, const char* nickname, bool secretTalent, uint8_t talent,
LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) { LearnedAttack* attacks[], size_t attacksNum, bool allowedExperienceGain) {
Try(auto attacksVec = List<LearnedAttack*>(attacks, attacks + attacksNum); Try(auto attacksVec = List<LearnedAttack*>(attacks, attacks + attacksNum);
out = new Creature(library, std::shared_ptr<const CreatureLib::Library::CreatureSpecies>(species), variant, out = new Creature(library, borrowed_ptr<const CreatureLib::Library::CreatureSpecies>(species), variant, level,
level, experience, uid, gender, coloring, experience, uid, gender, coloring, borrowed_ptr<const CreatureLib::Library::Item>(heldItem),
std::shared_ptr<const CreatureLib::Library::Item>(heldItem), nickname, nickname, CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec,
CreatureLib::Library::TalentIndex(secretTalent, talent), attacksVec, allowedExperienceGain);) allowedExperienceGain);)
}; };
export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; } export void CreatureLib_Creature_Destruct(const Creature* p) { delete p; }
@ -41,7 +41,7 @@ export uint8_t CreatureLib_Creature_SetHeldItem(Creature* p, const char* name) {
} }
export uint8_t CreatureLib_Creature_SetHeldItemWithHash(Creature* p, uint32_t hash) { Try(p->SetHeldItem(hash);) } export uint8_t CreatureLib_Creature_SetHeldItemWithHash(Creature* p, uint32_t hash) { Try(p->SetHeldItem(hash);) }
export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) { export void CreatureLib_Creature_SetHeldItemFromItem(Creature* p, const CreatureLib::Library::Item* item) {
return p->SetHeldItem(std::shared_ptr<const CreatureLib::Library::Item>(item)); return p->SetHeldItem(borrowed_ptr<const CreatureLib::Library::Item>(item));
} }
SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t); SIMPLE_GET_FUNC(Creature, GetCurrentHealth, uint32_t);
SIMPLE_GET_FUNC(Creature, GetBattle, Battle*); SIMPLE_GET_FUNC(Creature, GetBattle, Battle*);
@ -86,7 +86,7 @@ export LearnedAttack* const* CreatureLib_Creature_GetAttacks(Creature* p) { retu
SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*); SIMPLE_GET_FUNC_SMART_PTR(Creature, GetDisplaySpecies, const CreatureLib::Library::CreatureSpecies*);
SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*); SIMPLE_GET_FUNC(Creature, GetDisplayVariant, const CreatureLib::Library::SpeciesVariant*);
export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) { export void CreatureLib_Creature_SetDisplaySpecies(Creature* p, const CreatureLib::Library::CreatureSpecies* species) {
return p->SetDisplaySpecies(std::shared_ptr<const CreatureLib::Library::CreatureSpecies>(species)); return p->SetDisplaySpecies(borrowed_ptr<const CreatureLib::Library::CreatureSpecies>(species));
} }
export void CreatureLib_Creature_SetDisplayVariant(Creature* p, const CreatureLib::Library::SpeciesVariant* variant) { export void CreatureLib_Creature_SetDisplayVariant(Creature* p, const CreatureLib::Library::SpeciesVariant* variant) {
return p->SetDisplayVariant(variant); return p->SetDisplayVariant(variant);

View File

@ -4,7 +4,7 @@ using namespace CreatureLib::Battling;
export uint8_t CreatureLib_LearnedAttack_Construct(LearnedAttack*& out, const CreatureLib::Library::AttackData* attack, export uint8_t CreatureLib_LearnedAttack_Construct(LearnedAttack*& out, const CreatureLib::Library::AttackData* attack,
uint8_t maxUses, AttackLearnMethod learnMethod) { uint8_t maxUses, AttackLearnMethod learnMethod) {
Try(out = new LearnedAttack(std::shared_ptr<const CreatureLib::Library::AttackData>(attack), maxUses, learnMethod);) Try(out = new LearnedAttack(borrowed_ptr<const CreatureLib::Library::AttackData>(attack), maxUses, learnMethod);)
} }
export void CreatureLib_LearnedAttack_Destruct(LearnedAttack* p) { delete p; } export void CreatureLib_LearnedAttack_Destruct(LearnedAttack* p) { delete p; }

View File

@ -16,14 +16,14 @@
export uint8_t simpleName##_DeleteWithHash(fullname* p, uint32_t hashedKey) { Try(p->Delete(hashedKey);) } \ export uint8_t simpleName##_DeleteWithHash(fullname* p, uint32_t hashedKey) { Try(p->Delete(hashedKey);) } \
\ \
export bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \ export bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \
std::shared_ptr<const returnType> o; \ borrowed_ptr<const returnType> o; \
auto v = p->TryGet(Arbutils::CaseInsensitiveConstString::GetHash(name), o); \ auto v = p->TryGet(Arbutils::CaseInsensitiveConstString::GetHash(name), o); \
out = o.operator->(); \ out = o.operator->(); \
return v; \ return v; \
} \ } \
\ \
export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \ export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \
std::shared_ptr<const returnType> o; \ borrowed_ptr<const returnType> o; \
auto v = p->TryGet(hashedKey, o); \ auto v = p->TryGet(hashedKey, o); \
out = o.operator->(); \ out = o.operator->(); \
return v; \ return v; \

View File

@ -11,12 +11,12 @@ bool CreatureLib::Battling::MiscLibrary::IsCritical(CreatureLib::Battling::Execu
} }
static CreatureLib::Battling::LearnedAttack* _replacementAttack = nullptr; static CreatureLib::Battling::LearnedAttack* _replacementAttack = nullptr;
static std::shared_ptr<const CreatureLib::Library::AttackData> _replacementAttackData = nullptr; static std::unique_ptr<const CreatureLib::Library::AttackData> _replacementAttackData = nullptr;
static const std::shared_ptr<const CreatureLib::Library::AttackData>& GetReplacementAttackData() { static borrowed_ptr<const CreatureLib::Library::AttackData> GetReplacementAttackData() {
if (_replacementAttackData == nullptr) { if (_replacementAttackData == nullptr) {
_replacementAttackData = _replacementAttackData =
std::shared_ptr<const CreatureLib::Library::AttackData>(new CreatureLib::Library::AttackData( std::unique_ptr<const CreatureLib::Library::AttackData>(new CreatureLib::Library::AttackData(
"replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, 30, 255, 255, "replacement"_cnc, 0, CreatureLib::Library::AttackCategory::Physical, 30, 255, 255,
CreatureLib::Library::AttackTarget::Any, 0, new CreatureLib::Library::SecondaryEffect(), {})); CreatureLib::Library::AttackTarget::Any, 0, new CreatureLib::Library::SecondaryEffect(), {}));
} }

View File

@ -25,14 +25,14 @@ CreateCreature CreateCreature::WithAttack(const Arbutils::CaseInsensitiveConstSt
if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves()) if (_attacks.Count() >= _library->GetSettings()->GetMaximalMoves())
throw CreatureException("You have already set the maximum amount of allowed moves."); throw CreatureException("You have already set the maximum amount of allowed moves.");
auto attackData = _library->GetAttackLibrary()->Get(attackName); auto attackData = _library->GetAttackLibrary()->Get(attackName.GetHash());
_attacks.Append(std::tuple(attackData, learnMethod)); _attacks.Append(std::tuple(attackData, learnMethod));
return *this; return *this;
} }
Creature* CreateCreature::Create() { Creature* CreateCreature::Create() {
auto rand = Arbutils::Random(); auto rand = Arbutils::Random();
auto& species = this->_library->GetSpeciesLibrary()->Get(this->_species); auto species = this->_library->GetSpeciesLibrary()->Get(this->_species.GetHash());
auto variant = species->GetVariant(this->_variant); auto variant = species->GetVariant(this->_variant);
Library::TalentIndex talent; Library::TalentIndex talent;
if (this->_talent.Empty()) { if (this->_talent.Empty()) {
@ -48,7 +48,7 @@ Creature* CreateCreature::Create() {
if (gender == static_cast<Library::Gender>(-1)) { if (gender == static_cast<Library::Gender>(-1)) {
gender = species->GetRandomGender(rand); gender = species->GetRandomGender(rand);
} }
std::shared_ptr<const Library::Item> heldItem = nullptr; borrowed_ptr<const Library::Item> heldItem;
if (!this->_heldItem.Empty()) { if (!this->_heldItem.Empty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) { if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) {
throw CreatureException("Invalid held item."); throw CreatureException("Invalid held item.");

View File

@ -21,7 +21,7 @@ namespace CreatureLib::Battling {
uint8_t _coloring = 0; uint8_t _coloring = 0;
Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc; Arbutils::CaseInsensitiveConstString _heldItem = ""_cnc;
uint32_t _identifier = 0; uint32_t _identifier = 0;
List<std::tuple<std::shared_ptr<const Library::AttackData>, AttackLearnMethod>> _attacks; List<std::tuple<borrowed_ptr<const Library::AttackData>, AttackLearnMethod>> _attacks;
public: public:
CreateCreature(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level) CreateCreature(const BattleLibrary* library, const Arbutils::CaseInsensitiveConstString& species, uint8_t level)

View File

@ -6,13 +6,11 @@
using namespace CreatureLib; using namespace CreatureLib;
Battling::Creature::Creature(const BattleLibrary* library, Battling::Creature::Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species,
const std::shared_ptr<const Library::CreatureSpecies>& species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid,
Library::Gender gender, uint8_t coloring, Library::Gender gender, uint8_t coloring, const borrowed_ptr<const Library::Item> heldItem,
const std::shared_ptr<const Library::Item> heldItem, std::string nickname, std::string nickname, const Library::TalentIndex& talent,
const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks, const List<LearnedAttack*>& attacks, bool allowedExperienceGain)
bool allowedExperienceGain)
: _library(library), _species(species), _variant(variant), _level(level), _experience(experience), : _library(library), _species(species), _variant(variant), _level(level), _experience(experience),
_uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)), _uniqueIdentifier(uid), _gender(gender), _coloring(coloring), _heldItem(heldItem), _nickname(std::move(nickname)),
_talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks), _talentIndex(talent), _hasOverridenTalent(false), _attacks(attacks),
@ -187,7 +185,7 @@ void Battling::Creature::AddExperience(uint32_t amount) {
_experience = exp; _experience = exp;
_level = level; _level = level;
} }
std::shared_ptr<const Library::CreatureSpecies> Battling::Creature::GetDisplaySpecies() const noexcept { borrowed_ptr<const Library::CreatureSpecies> Battling::Creature::GetDisplaySpecies() const noexcept {
auto species = _displaySpecies; auto species = _displaySpecies;
if (species == nullptr) if (species == nullptr)
species = _species; species = _species;
@ -200,14 +198,14 @@ const Library::SpeciesVariant* Battling::Creature::GetDisplayVariant() const noe
return variant; return variant;
} }
void Battling::Creature::SetHeldItem(const ConstString& itemName) { void Battling::Creature::SetHeldItem(const ConstString& itemName) {
std::shared_ptr<const Library::Item> item; borrowed_ptr<const Library::Item> item;
if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) { if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) {
throw CreatureException("Item not found."); throw CreatureException("Item not found.");
} }
_heldItem = item; _heldItem = item;
} }
void Battling::Creature::SetHeldItem(uint32_t itemNameHash) { void Battling::Creature::SetHeldItem(uint32_t itemNameHash) {
std::shared_ptr<const Library::Item> item; borrowed_ptr<const Library::Item> item;
if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) { if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) {
throw CreatureException("Item not found."); throw CreatureException("Item not found.");
} }

View File

@ -2,6 +2,7 @@
#define CREATURELIB_BATTLECREATURE_HPP #define CREATURELIB_BATTLECREATURE_HPP
#include <Arbutils/Collections/List.hpp> #include <Arbutils/Collections/List.hpp>
#include <Arbutils/Memory/borrowed_ptr.hpp>
#include "../../Library/ClampedStatisticSet.hpp" #include "../../Library/ClampedStatisticSet.hpp"
#include "../../Library/CreatureData/CreatureSpecies.hpp" #include "../../Library/CreatureData/CreatureSpecies.hpp"
#include "../../Library/Items/Item.hpp" #include "../../Library/Items/Item.hpp"
@ -23,10 +24,10 @@ namespace CreatureLib::Battling {
protected: protected:
const BattleLibrary* _library; const BattleLibrary* _library;
std::shared_ptr<const Library::CreatureSpecies> _species; borrowed_ptr<const Library::CreatureSpecies> _species;
const Library::SpeciesVariant* _variant; const Library::SpeciesVariant* _variant;
std::shared_ptr<const Library::CreatureSpecies> _displaySpecies = nullptr; borrowed_ptr<const Library::CreatureSpecies> _displaySpecies = nullptr;
const Library::SpeciesVariant* _displayVariant = nullptr; const Library::SpeciesVariant* _displayVariant = nullptr;
uint8_t _level; uint8_t _level;
@ -34,7 +35,7 @@ namespace CreatureLib::Battling {
uint32_t _uniqueIdentifier; uint32_t _uniqueIdentifier;
Library::Gender _gender; Library::Gender _gender;
uint8_t _coloring; uint8_t _coloring;
std::shared_ptr<const Library::Item> _heldItem; borrowed_ptr<const Library::Item> _heldItem;
uint32_t _currentHealth; uint32_t _currentHealth;
Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost; Library::ClampedStatisticSet<int8_t, -6, 6> _statBoost;
@ -63,9 +64,9 @@ namespace CreatureLib::Battling {
void OnFaint(); void OnFaint();
public: public:
Creature(const BattleLibrary* library, const std::shared_ptr<const Library::CreatureSpecies>& species, Creature(const BattleLibrary* library, const borrowed_ptr<const Library::CreatureSpecies>& species,
const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid, const Library::SpeciesVariant* variant, uint8_t level, uint32_t experience, uint32_t uid,
Library::Gender gender, uint8_t coloring, const std::shared_ptr<const Library::Item> heldItem, Library::Gender gender, uint8_t coloring, const borrowed_ptr<const Library::Item> heldItem,
std::string nickname, const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks, std::string nickname, const Library::TalentIndex& talent, const List<LearnedAttack*>& attacks,
bool allowedExperienceGain = true); bool allowedExperienceGain = true);
@ -82,7 +83,7 @@ namespace CreatureLib::Battling {
_currentHealth = GetBoostedStat(Library::Statistic::Health); _currentHealth = GetBoostedStat(Library::Statistic::Health);
} }
inline const std::shared_ptr<const Library::CreatureSpecies>& GetSpecies() const noexcept { return _species; } inline const borrowed_ptr<const Library::CreatureSpecies>& GetSpecies() const noexcept { return _species; }
inline const Library::SpeciesVariant* GetVariant() const noexcept { return _variant; } inline const Library::SpeciesVariant* GetVariant() const noexcept { return _variant; }
inline uint8_t GetLevel() const noexcept { return _level; } inline uint8_t GetLevel() const noexcept { return _level; }
inline uint32_t GetExperience() const noexcept { return _experience; } inline uint32_t GetExperience() const noexcept { return _experience; }
@ -94,10 +95,10 @@ namespace CreatureLib::Battling {
inline bool HasHeldItem(uint32_t nameHash) const noexcept { inline bool HasHeldItem(uint32_t nameHash) const noexcept {
return _heldItem != nullptr && _heldItem->GetName() == nameHash; return _heldItem != nullptr && _heldItem->GetName() == nameHash;
} }
inline const std::shared_ptr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; } inline const borrowed_ptr<const Library::Item>& GetHeldItem() const noexcept { return _heldItem; }
void SetHeldItem(const ConstString& itemName); void SetHeldItem(const ConstString& itemName);
void SetHeldItem(uint32_t itemNameHash); void SetHeldItem(uint32_t itemNameHash);
inline void SetHeldItem(const std::shared_ptr<const Library::Item>& item) noexcept { _heldItem = item; }; inline void SetHeldItem(const borrowed_ptr<const Library::Item>& item) noexcept { _heldItem = item; };
inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; } inline uint32_t GetCurrentHealth() const noexcept { return _currentHealth; }
@ -135,10 +136,10 @@ namespace CreatureLib::Battling {
const List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; } const List<LearnedAttack*>& GetAttacks() noexcept { return _attacks; }
std::shared_ptr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept; borrowed_ptr<const Library::CreatureSpecies> GetDisplaySpecies() const noexcept;
const Library::SpeciesVariant* GetDisplayVariant() const noexcept; const Library::SpeciesVariant* GetDisplayVariant() const noexcept;
void SetDisplaySpecies(const std::shared_ptr<const Library::CreatureSpecies>& species) noexcept { void SetDisplaySpecies(const borrowed_ptr<const Library::CreatureSpecies>& species) noexcept {
_displaySpecies = species; _displaySpecies = species;
} }
void SetDisplayVariant(const Library::SpeciesVariant* variant) noexcept { _displayVariant = variant; }; void SetDisplayVariant(const Library::SpeciesVariant* variant) noexcept { _displayVariant = variant; };

View File

@ -1,19 +1,18 @@
#include "LearnedAttack.hpp" #include "LearnedAttack.hpp"
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
CreatureLib::Battling::LearnedAttack::LearnedAttack( CreatureLib::Battling::LearnedAttack::LearnedAttack(const borrowed_ptr<const CreatureLib::Library::AttackData>& attack,
const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses, uint8_t maxUses, AttackLearnMethod learnMethod)
AttackLearnMethod learnMethod)
: _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) { : _attack(attack), _maxUses(maxUses), _remainingUses(maxUses), _learnMethod(learnMethod) {
AssertNotNull(_attack) AssertNotNull(_attack)
} }
CreatureLib::Battling::LearnedAttack::LearnedAttack( CreatureLib::Battling::LearnedAttack::LearnedAttack(const borrowed_ptr<const CreatureLib::Library::AttackData>& attack,
const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, AttackLearnMethod learnMethod) AttackLearnMethod learnMethod)
: _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) { : _attack(attack), _maxUses(attack->GetBaseUsages()), _remainingUses(_maxUses), _learnMethod(learnMethod) {
AssertNotNull(_attack) AssertNotNull(_attack)
} }
const std::shared_ptr<const CreatureLib::Library::AttackData>& const borrowed_ptr<const CreatureLib::Library::AttackData>&
CreatureLib::Battling::LearnedAttack::GetAttack() const noexcept { CreatureLib::Battling::LearnedAttack::GetAttack() const noexcept {
return _attack; return _attack;
} }

View File

@ -1,26 +1,27 @@
#ifndef CREATURELIB_LEARNEDATTACK_HPP #ifndef CREATURELIB_LEARNEDATTACK_HPP
#define CREATURELIB_LEARNEDATTACK_HPP #define CREATURELIB_LEARNEDATTACK_HPP
#include <Arbutils/Memory/borrowed_ptr.hpp>
#include <memory> #include <memory>
#include "../../Library/Attacks/AttackData.hpp" #include "../../Library/Attacks/AttackData.hpp"
#include "AttackLearnMethod.hpp" #include "AttackLearnMethod.hpp"
namespace CreatureLib::Battling { namespace CreatureLib::Battling {
class LearnedAttack { class LearnedAttack {
std::shared_ptr<const Library::AttackData> _attack; borrowed_ptr<const Library::AttackData> _attack;
uint8_t _maxUses; uint8_t _maxUses;
uint8_t _remainingUses; uint8_t _remainingUses;
AttackLearnMethod _learnMethod; AttackLearnMethod _learnMethod;
public: public:
LearnedAttack(const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses, LearnedAttack(const borrowed_ptr<const CreatureLib::Library::AttackData>& attack, uint8_t maxUses,
AttackLearnMethod learnMethod); AttackLearnMethod learnMethod);
LearnedAttack(const std::shared_ptr<const CreatureLib::Library::AttackData>& attack, LearnedAttack(const borrowed_ptr<const CreatureLib::Library::AttackData>& attack,
AttackLearnMethod learnMethod); AttackLearnMethod learnMethod);
virtual ~LearnedAttack() = default; virtual ~LearnedAttack() = default;
const std::shared_ptr<const Library::AttackData>& GetAttack() const noexcept; const borrowed_ptr<const Library::AttackData>& GetAttack() const noexcept;
uint8_t GetMaxUses() const noexcept; uint8_t GetMaxUses() const noexcept;
uint8_t GetRemainingUses() const noexcept; uint8_t GetRemainingUses() const noexcept;
AttackLearnMethod GetLearnMethod() const noexcept; AttackLearnMethod GetLearnMethod() const noexcept;

View File

@ -5,6 +5,7 @@
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp> #include <Arbutils/Collections/List.hpp>
#include <Arbutils/ConstString.hpp> #include <Arbutils/ConstString.hpp>
#include <Arbutils/Memory/borrowed_ptr.hpp>
#include <Arbutils/Random.hpp> #include <Arbutils/Random.hpp>
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
@ -12,7 +13,7 @@
namespace CreatureLib::Library { namespace CreatureLib::Library {
template <class T> class BaseLibrary { template <class T> class BaseLibrary {
Arbutils::Collections::Dictionary<uint32_t, std::shared_ptr<const T>> _values; Arbutils::Collections::Dictionary<uint32_t, std::unique_ptr<const T>> _values;
Arbutils::Collections::List<uint32_t> _listValues; Arbutils::Collections::List<uint32_t> _listValues;
size_t _index; size_t _index;
@ -23,12 +24,12 @@ namespace CreatureLib::Library {
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) { inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.Insert(key.GetHash(), value); _values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
_listValues.Append(key); _listValues.Append(key);
} }
inline void Insert(uint32_t hashedKey, const T* value) { inline void Insert(uint32_t hashedKey, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.Insert(hashedKey, std::shared_ptr<const T>(value)); _values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)});
_listValues.Append(hashedKey); _listValues.Append(hashedKey);
} }
@ -43,38 +44,38 @@ namespace CreatureLib::Library {
_listValues.Remove(k); _listValues.Remove(k);
} }
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const std::shared_ptr<const T>& out) const { bool TryGet(const Arbutils::CaseInsensitiveConstString& name, borrowed_ptr<const T>& out) const {
return TryGet(name.GetHash(), out); return TryGet(name.GetHash(), out);
} }
bool TryGet(uint32_t hashedKey, std::shared_ptr<const T>& out) const { return _values.TryGet(hashedKey, out); } bool TryGet(uint32_t hashedKey, borrowed_ptr<const T>& out) const {
auto find = _values.GetStdMap().find(hashedKey);
if (find == _values.GetStdMap().end())
return false;
out = std::get<1>(*find);
return true;
}
[[nodiscard]] inline const std::shared_ptr<const T>& [[nodiscard]] inline borrowed_ptr<const T> Get(const Arbutils::CaseInsensitiveConstString& name) const {
Get(const Arbutils::CaseInsensitiveConstString& name) const {
return _values.Get(name.GetHash()); return _values.Get(name.GetHash());
} }
[[nodiscard]] inline const std::shared_ptr<const T>& Get(uint32_t hashedKey) const { [[nodiscard]] inline borrowed_ptr<const T> Get(uint32_t hashedKey) { return _values.Get(hashedKey); }
return _values.Get(hashedKey);
}
[[nodiscard]] inline const std::shared_ptr<const T>& [[nodiscard]] inline borrowed_ptr<const T> operator[](const Arbutils::CaseInsensitiveConstString& name) const {
operator[](const Arbutils::CaseInsensitiveConstString& name) const {
return Get(name); return Get(name);
} }
[[nodiscard]] inline const std::shared_ptr<const T>& operator[](uint32_t hashedKey) const { [[nodiscard]] inline borrowed_ptr<const T> operator[](uint32_t hashedKey) const { return Get(hashedKey); }
return Get(hashedKey); [[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const std::unique_ptr<const T>>&
}
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const std::shared_ptr<const T>>&
GetIterator() const { GetIterator() const {
return _values; return _values;
} }
[[nodiscard]] size_t GetCount() const { return _values.Count(); } [[nodiscard]] size_t GetCount() const { return _values.Count(); }
inline const std::shared_ptr<const T>& GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const { inline borrowed_ptr<const T> GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
auto i = rand.Get(_listValues.Count()); auto i = rand.Get(_listValues.Count());
return _values[_listValues[i]]; return _values[_listValues[i]];
} }
inline const std::shared_ptr<const T>& GetRandomValue(Arbutils::Random* rand) const { inline borrowed_ptr<const T> GetRandomValue(Arbutils::Random* rand) const {
auto i = rand->Get(_listValues.Count()); auto i = rand->Get(_listValues.Count());
return _values[_listValues[i]]; return _values[_listValues[i]];
} }