66 lines
2.4 KiB
C++
66 lines
2.4 KiB
C++
#ifndef CREATURELIB_BASELIBRARY_HPP
|
|
#define CREATURELIB_BASELIBRARY_HPP
|
|
|
|
#include <Arbutils/ConstString.hpp>
|
|
#include <algorithm>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
namespace CreatureLib::Library {
|
|
template <class T> class BaseLibrary {
|
|
std::unordered_map<uint32_t, const T*> _values;
|
|
|
|
public:
|
|
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity) {}
|
|
|
|
virtual ~BaseLibrary() {
|
|
for (const auto& v : _values) {
|
|
delete v.second;
|
|
}
|
|
_values.clear();
|
|
}
|
|
|
|
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
|
|
_values.insert({key.GetHash(), value});
|
|
}
|
|
inline void Insert(uint32_t hashedKey, const T* value) { _values.insert({hashedKey, value}); }
|
|
|
|
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); }
|
|
inline void Delete(uint32_t hashedKey) { _values.erase({hashedKey}); }
|
|
|
|
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
|
|
auto find = this->_values.find(name.GetHash());
|
|
if (find == this->_values.end()) {
|
|
out = nullptr;
|
|
return false;
|
|
}
|
|
out = find->second;
|
|
return true;
|
|
}
|
|
bool TryGet(uint32_t hashedKey, const T*& out) const {
|
|
auto find = this->_values.find(hashedKey);
|
|
if (find == this->_values.end()) {
|
|
out = nullptr;
|
|
return false;
|
|
}
|
|
out = find->second;
|
|
return true;
|
|
}
|
|
|
|
[[nodiscard]] inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
|
return _values.at(name.GetHash());
|
|
}
|
|
[[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.at(hashedKey); }
|
|
|
|
[[nodiscard]] inline 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 std::unordered_map<uint32_t, const T*>& GetIterator() const { return _values; }
|
|
|
|
[[nodiscard]] size_t GetCount() const { return _values.size(); }
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_BASELIBRARY_HPP
|