Add the baselibrary classes to the C Interface.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-29 14:52:15 +01:00
parent 0a109e37bc
commit b7b54c04e8
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
5 changed files with 72 additions and 4 deletions

View File

@ -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);

View File

@ -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(); }

View File

@ -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);

View File

@ -1,4 +1,5 @@
#include "../../src/Library/SpeciesLibrary.hpp" #include "../../src/Library/SpeciesLibrary.hpp"
#include "BaseLibrary.cpp"
#define export extern "C" #define export extern "C"
export const CreatureLib::Library::SpeciesLibrary* CreatureLib_SpeciesLibrary_Construct(size_t initialCapacity = 32) { 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; } export void CreatureLib_SpeciesLibrary_Destruct(const CreatureLib::Library::SpeciesLibrary* p) { delete p; }
void CreatureLib_SpeciesLibrary_Insert(CreatureLib::Library::SpeciesLibrary* p, const char* name, BASELIBRARY(CreatureLib_SpeciesLibrary, CreatureLib::Library::SpeciesLibrary, CreatureLib::Library::CreatureSpecies);
CreatureLib::Library::CreatureSpecies* species) {
p->Insert(Arbutils::CaseInsensitiveConstString(name), species);
}

View File

@ -23,7 +23,10 @@ namespace CreatureLib::Library {
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) { inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
_values.insert({key.GetHash(), 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(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 { bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
auto find = this->_values.find(name.GetHash()); auto find = this->_values.find(name.GetHash());
@ -34,10 +37,23 @@ namespace CreatureLib::Library {
out = find->second; out = find->second;
return true; 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 { inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
return _values.at(name.GetHash()); 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[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); }
inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); }
inline const std::unordered_map<uint32_t, const T*>& GetIterator() const { return _values; } inline const std::unordered_map<uint32_t, const T*>& GetIterator() const { return _values; }
size_t GetCount() const { return _values.size(); } size_t GetCount() const { return _values.size(); }