Remove shared_ptr, instead use borrowed_ptr to more accurately depict ownership of the objects in the BaseLibraries.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <Arbutils/Collections/Dictionary.hpp>
|
||||
#include <Arbutils/Collections/List.hpp>
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <Arbutils/Memory/borrowed_ptr.hpp>
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
@@ -12,7 +13,7 @@
|
||||
|
||||
namespace CreatureLib::Library {
|
||||
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;
|
||||
size_t _index;
|
||||
|
||||
@@ -23,12 +24,12 @@ namespace CreatureLib::Library {
|
||||
|
||||
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
|
||||
AssertNotNull(value)
|
||||
_values.Insert(key.GetHash(), value);
|
||||
_values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
|
||||
_listValues.Append(key);
|
||||
}
|
||||
inline void Insert(uint32_t hashedKey, const T* 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);
|
||||
}
|
||||
|
||||
@@ -43,38 +44,38 @@ namespace CreatureLib::Library {
|
||||
_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);
|
||||
}
|
||||
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>&
|
||||
Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline borrowed_ptr<const T> Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return _values.Get(name.GetHash());
|
||||
}
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>& Get(uint32_t hashedKey) const {
|
||||
return _values.Get(hashedKey);
|
||||
}
|
||||
[[nodiscard]] inline borrowed_ptr<const T> Get(uint32_t hashedKey) { return _values.Get(hashedKey); }
|
||||
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>&
|
||||
operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline borrowed_ptr<const T> operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return Get(name);
|
||||
}
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>& operator[](uint32_t hashedKey) const {
|
||||
return Get(hashedKey);
|
||||
}
|
||||
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const std::shared_ptr<const T>>&
|
||||
[[nodiscard]] inline borrowed_ptr<const T> operator[](uint32_t hashedKey) const { return Get(hashedKey); }
|
||||
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const std::unique_ptr<const T>>&
|
||||
GetIterator() const {
|
||||
return _values;
|
||||
}
|
||||
|
||||
[[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());
|
||||
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());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user