diff --git a/CInterface/Library/AttackLibrary.cpp b/CInterface/Library/AttackLibrary.cpp new file mode 100644 index 0000000..6c5915a --- /dev/null +++ b/CInterface/Library/AttackLibrary.cpp @@ -0,0 +1,11 @@ +#include "../../src/Library/AttackLibrary.hpp" +#include "BaseLibrary.cpp" +#define export extern "C" + +export const CreatureLib::Library::AttackLibrary* CreatureLib_AttackLibrary_Construct(size_t initialCapacity = 32) { + return new CreatureLib::Library::AttackLibrary(initialCapacity); +}; + +export void CreatureLib_AttackLibrary_Destruct(const CreatureLib::Library::AttackLibrary* p) { delete p; } + +BASELIBRARY(CreatureLib_AttackLibrary, CreatureLib::Library::AttackLibrary, CreatureLib::Library::AttackData); \ No newline at end of file diff --git a/CInterface/Library/BaseLibrary.cpp b/CInterface/Library/BaseLibrary.cpp new file mode 100644 index 0000000..0d9130b --- /dev/null +++ b/CInterface/Library/BaseLibrary.cpp @@ -0,0 +1,32 @@ +#define export extern "C" + +#define BASELIBRARY(simpleName, fullname, returnType) \ + export void simpleName##_Insert(fullname* p, const char* name, returnType* t) { \ + p->Insert(Arbutils::CaseInsensitiveConstString::GetHash(name), t); \ + } \ + \ + export void simpleName##_InsertWithHash(fullname* p, uint32_t hashedKey, returnType* t) { \ + p->Insert(hashedKey, t); \ + } \ + \ + export void simpleName##_Delete(fullname* p, const char* name) { \ + p->Delete(Arbutils::CaseInsensitiveConstString::GetHash(name)); \ + } \ + \ + export void simpleName##_DeleteWithHash(fullname* p, uint32_t hashedKey) { p->Delete(hashedKey); } \ + \ + export bool simpleName##_TryGet(fullname* p, const char* name, const returnType* out) { \ + return p->TryGet(Arbutils::CaseInsensitiveConstString::GetHash(name), out); \ + } \ + \ + export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType* out) { \ + return p->TryGet(hashedKey, out); \ + } \ + \ + export bool simpleName##_Get(fullname* p, const char* name) { \ + return p->Get(Arbutils::CaseInsensitiveConstString::GetHash(name)); \ + } \ + \ + export bool simpleName##_GetWithHash(fullname* p, uint32_t hashedKey) { return p->Get(hashedKey); } \ + \ + export size_t simpleName##_GetCount(fullname* p) { return p->GetCount(); } diff --git a/CInterface/Library/ItemLibrary.cpp b/CInterface/Library/ItemLibrary.cpp new file mode 100644 index 0000000..6bbdaae --- /dev/null +++ b/CInterface/Library/ItemLibrary.cpp @@ -0,0 +1,11 @@ +#include "../../src/Library/ItemLibrary.hpp" +#include "BaseLibrary.cpp" +#define export extern "C" + +export const CreatureLib::Library::ItemLibrary* CreatureLib_ItemLibrary_Construct(size_t initialCapacity = 32) { + return new CreatureLib::Library::ItemLibrary(initialCapacity); +}; + +export void CreatureLib_ItemLibrary_Destruct(const CreatureLib::Library::ItemLibrary* p) { delete p; } + +BASELIBRARY(CreatureLib_ItemLibrary, CreatureLib::Library::ItemLibrary, CreatureLib::Library::Item); \ No newline at end of file diff --git a/CInterface/Library/SpeciesLibrary.cpp b/CInterface/Library/SpeciesLibrary.cpp index 06961b2..2d133cd 100644 --- a/CInterface/Library/SpeciesLibrary.cpp +++ b/CInterface/Library/SpeciesLibrary.cpp @@ -1,4 +1,5 @@ #include "../../src/Library/SpeciesLibrary.hpp" +#include "BaseLibrary.cpp" #define export extern "C" export const CreatureLib::Library::SpeciesLibrary* CreatureLib_SpeciesLibrary_Construct(size_t initialCapacity = 32) { @@ -7,7 +8,4 @@ export const CreatureLib::Library::SpeciesLibrary* CreatureLib_SpeciesLibrary_Co export void CreatureLib_SpeciesLibrary_Destruct(const CreatureLib::Library::SpeciesLibrary* p) { delete p; } -void CreatureLib_SpeciesLibrary_Insert(CreatureLib::Library::SpeciesLibrary* p, const char* name, - CreatureLib::Library::CreatureSpecies* species) { - p->Insert(Arbutils::CaseInsensitiveConstString(name), species); -} +BASELIBRARY(CreatureLib_SpeciesLibrary, CreatureLib::Library::SpeciesLibrary, CreatureLib::Library::CreatureSpecies); \ No newline at end of file diff --git a/src/Library/BaseLibrary.hpp b/src/Library/BaseLibrary.hpp index 19ab643..c8eff69 100644 --- a/src/Library/BaseLibrary.hpp +++ b/src/Library/BaseLibrary.hpp @@ -23,7 +23,10 @@ namespace CreatureLib::Library { 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()); @@ -34,10 +37,23 @@ namespace CreatureLib::Library { 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; + } + inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const { return _values.at(name.GetHash()); } + inline const T* Get(uint32_t hashedKey) const { return _values.at(hashedKey); } + inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); } + inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); } inline const std::unordered_map& GetIterator() const { return _values; } size_t GetCount() const { return _values.size(); }