11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
19PROMEKI_NAMESPACE_BEGIN
42template <
typename KeyType,
typename StructType>
class StructDatabase {
44 using Database = ::promeki::Map<KeyType, StructType>;
45 using NameDatabase = ::promeki::Map<String, KeyType>;
48 StructDatabase() =
default;
52 StructDatabase(std::initializer_list<StructType> list) { load(list); }
56 void add(
const StructType &val) {
58 _nameDb[val.name] = val.id;
65 const StructType &get(
const KeyType &
id)
const {
66 auto it = _db.find(
id);
67 if (it != _db.end())
return it->second;
68 it = _db.find(
static_cast<KeyType
>(0));
69 if (it != _db.end())
return it->second;
70 static const StructType fallback{};
78 Result<KeyType> lookupKeyByName(
const String &name)
const {
79 auto it = _nameDb.find(name);
80 if (it == _nameDb.end())
return Result<KeyType>(KeyType{}, Error::NotFound);
81 return Result<KeyType>(it->second, Error::Ok);
86 void load(std::initializer_list<StructType> list) {
87 for (
const auto &item : list) add(item);
92 const Database &database()
const {
return _db; }
94 Database &database() {
return _db; }