libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
StructDatabase< KeyType, StructType > Class Template Reference

A database that maps keys to struct entries, initialized from an initializer list. More...

#include <structdatabase.h>

Public Types

using Database = promeki::Map< KeyType, StructType >
 
using NameDatabase = promeki::Map< String, KeyType >
 

Public Member Functions

 StructDatabase ()=default
 Constructs an empty database.
 
 StructDatabase (const std::initializer_list< StructType > &&list)
 Constructs a database and populates it from an initializer list.
 
void add (const StructType &&val)
 Adds a single entry to the database.
 
const StructTypeget (const KeyType &id) const
 Retrieves an entry by its key, falling back to key 0 if not found.
 
KeyType lookupKeyByName (const String &name) const
 Looks up a key by the entry's name.
 
void load (const std::initializer_list< StructType > &&list)
 Loads all entries from an initializer list.
 
const Database & database () const
 Returns a const reference to the underlying database map.
 
Database & database ()
 Returns a mutable reference to the underlying database map.
 

Detailed Description

template<typename KeyType, typename StructType>
class StructDatabase< KeyType, StructType >

A database that maps keys to struct entries, initialized from an initializer list.

Provides a structure database that can be initialized from an initializer list, allowing you to initialize a structure database on startup. The struct must contain members named id and name. The id is used as the key in the map. The ID 0 must exist as it will be returned as the fallback when an id is not found. The name must be a unique name for each struct entry.

Template Parameters
KeyTypeThe type used as the lookup key (must be convertible from 0 for the fallback).
StructTypeThe struct type stored in the database; must have id and name members.

Constructor & Destructor Documentation

◆ StructDatabase()

StructDatabase< KeyType, StructType >::StructDatabase ( const std::initializer_list< StructType > &&  list)
inline

Constructs a database and populates it from an initializer list.

Parameters
listInitializer list of StructType entries to load.

Member Function Documentation

◆ add()

Adds a single entry to the database.

Parameters
valThe struct entry to add (keyed by val.id).

◆ get()

Retrieves an entry by its key, falling back to key 0 if not found.

Parameters
idThe key to look up.
Returns
A const reference to the matching entry, or the fallback entry.

◆ load()

void StructDatabase< KeyType, StructType >::load ( const std::initializer_list< StructType > &&  list)
inline

Loads all entries from an initializer list.

Parameters
listThe initializer list of entries to add.

◆ lookupKeyByName()

KeyType StructDatabase< KeyType, StructType >::lookupKeyByName ( const String name) const
inline

Looks up a key by the entry's name.

Parameters
nameThe unique name to search for.
Returns
The corresponding key, or KeyType(0) if not found.

The documentation for this class was generated from the following file: