Support for getting Creature by ID.

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-08-22 13:24:08 +02:00
parent 9547b92f14
commit fcdc58176f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 20 additions and 3 deletions

View File

@ -10,3 +10,7 @@ export const SpeciesLibrary* CreatureLib_SpeciesLibrary_Construct(size_t initial
export void CreatureLib_SpeciesLibrary_Destruct(const SpeciesLibrary* p) { delete p; } export void CreatureLib_SpeciesLibrary_Destruct(const SpeciesLibrary* p) { delete p; }
BASELIBRARY(CreatureLib_SpeciesLibrary, SpeciesLibrary, CreatureSpecies); BASELIBRARY(CreatureLib_SpeciesLibrary, SpeciesLibrary, CreatureSpecies);
export const CreatureSpecies* CreatureLib_SpeciesLibrary_GetById(const SpeciesLibrary* p, uint16_t id) {
return p->GetById(id).GetRaw();
}

View File

@ -23,12 +23,12 @@ namespace CreatureLib::Library {
virtual ~BaseLibrary() noexcept { _values.Clear(); } virtual ~BaseLibrary() noexcept { _values.Clear(); }
inline void Insert(const ArbUt::StringView& key, const T* value) { inline virtual void Insert(const ArbUt::StringView& key, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)}); _values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
_listValues.Append(key); _listValues.Append(key);
} }
inline void Insert(uint32_t hashedKey, const T* value) { inline virtual void Insert(uint32_t hashedKey, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)}); _values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)});
_listValues.Append(hashedKey); _listValues.Append(hashedKey);

View File

@ -9,8 +9,21 @@
namespace CreatureLib::Library { namespace CreatureLib::Library {
class SpeciesLibrary : public BaseLibrary<CreatureSpecies> { class SpeciesLibrary : public BaseLibrary<CreatureSpecies> {
private: private:
ArbUt::Dictionary<uint16_t, ArbUt::BorrowedPtr<const CreatureSpecies>> _valuesById;
public: public:
SpeciesLibrary(size_t initialCapacity = 32) : BaseLibrary(initialCapacity){}; SpeciesLibrary(size_t initialCapacity = 32) : BaseLibrary(initialCapacity){};
void Insert(const ArbUt::StringView& key, const CreatureSpecies* value) override {
BaseLibrary::Insert(key, value);
_valuesById.Insert(value->GetId(), value);
}
void Insert(uint32_t hashedKey, const CreatureSpecies* value) override {
BaseLibrary::Insert(hashedKey, value);
_valuesById.Insert(value->GetId(), value);
}
const ArbUt::BorrowedPtr<const CreatureSpecies>& GetById(uint16_t id) const { return _valuesById[id]; }
}; };
} }