Make BaseLibraries use shared_ptr.
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:
@@ -7,23 +7,19 @@
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace CreatureLib::Library {
|
||||
template <class T> class BaseLibrary {
|
||||
Arbutils::Collections::Dictionary<uint32_t, const T*> _values;
|
||||
Arbutils::Collections::Dictionary<uint32_t, std::shared_ptr<const T>> _values;
|
||||
Arbutils::Collections::List<uint32_t> _listValues;
|
||||
size_t _index;
|
||||
|
||||
public:
|
||||
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
|
||||
|
||||
virtual ~BaseLibrary() {
|
||||
for (const auto& v : _values) {
|
||||
delete v.second;
|
||||
}
|
||||
_values.Clear();
|
||||
}
|
||||
virtual ~BaseLibrary() { _values.Clear(); }
|
||||
|
||||
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
|
||||
AssertNotNull(value)
|
||||
@@ -32,7 +28,7 @@ namespace CreatureLib::Library {
|
||||
}
|
||||
inline void Insert(uint32_t hashedKey, const T* value) {
|
||||
AssertNotNull(value)
|
||||
_values.Insert(hashedKey, value);
|
||||
_values.Insert(hashedKey, std::shared_ptr<const T>(value));
|
||||
_listValues.Append(hashedKey);
|
||||
}
|
||||
|
||||
@@ -47,31 +43,38 @@ namespace CreatureLib::Library {
|
||||
_listValues.Remove(k);
|
||||
}
|
||||
|
||||
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
|
||||
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const std::shared_ptr<const T>& out) const {
|
||||
return TryGet(name.GetHash(), out);
|
||||
}
|
||||
bool TryGet(uint32_t hashedKey, const T*& out) const { return _values.TryGet(hashedKey, out); }
|
||||
bool TryGet(uint32_t hashedKey, std::shared_ptr<const T>& out) const { return _values.TryGet(hashedKey, out); }
|
||||
|
||||
[[nodiscard]] inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>&
|
||||
Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return _values.Get(name.GetHash());
|
||||
}
|
||||
[[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.Get(hashedKey); }
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>& Get(uint32_t hashedKey) const {
|
||||
return _values.Get(hashedKey);
|
||||
}
|
||||
|
||||
[[nodiscard]] inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
[[nodiscard]] inline const std::shared_ptr<const T>&
|
||||
operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
||||
return Get(name);
|
||||
}
|
||||
[[nodiscard]] inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); }
|
||||
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const T*>& GetIterator() const {
|
||||
[[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>>&
|
||||
GetIterator() const {
|
||||
return _values;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t GetCount() const { return _values.Count(); }
|
||||
|
||||
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
inline const std::shared_ptr<const T>& GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
auto i = rand.Get(_listValues.Count());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
inline const T* GetRandomValue(Arbutils::Random* rand) const {
|
||||
inline const std::shared_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