8#include "core/memory/types.h"
10namespace Vkm::Engine {
20template <
typename Base>
23 TypeRegistry() =
default;
24 ~TypeRegistry() =
default;
26 TypeRegistry(
const TypeRegistry& other) =
delete;
27 TypeRegistry& operator=(
const TypeRegistry& other) =
delete;
30 TypeRegistry(TypeRegistry && other) =
delete;
31 TypeRegistry& operator=(TypeRegistry && other) =
delete;
42 template <
typename T,
typename Make>
44 std::unique_ptr<Base>& held =
slot<T>();
45 if (!held) held = make();
57 const TypeId
id = typeId<T>();
58 return id < m_slots.size() ? m_slots[id].get() :
nullptr;
64 const TypeId
id = typeId<T>();
65 return id < m_slots.size() ? m_slots[id].get() :
nullptr;
77 std::unique_ptr<Base>&
slot() {
78 const TypeId
id = typeId<T>();
79 if (
id >= m_slots.size()) m_slots.resize(
id + 1);
84 void clear() { m_slots.clear(); }
95 template <
typename Pred>
97 for (
auto& held : m_slots) {
98 if (held && pred(
static_cast<const Base&
>(*held))) held.reset();
107 void swap(TypeRegistry& other)
noexcept { m_slots.swap(other.m_slots); }
118 for (
const auto& held : m_slots) {
130 template <
typename Fn>
132 for (
auto& held : m_slots) {
138 template <
typename Fn>
140 for (
const auto& held : m_slots) {
146 std::vector<std::unique_ptr<Base>> m_slots;
Base * find()
The slot for T without creating one.
Definition type_registry.h:56
Base & ensure(Make &&make)
The slot for T, created by make the first time it is asked for.
Definition type_registry.h:43
void forEach(Fn &&fn) const
Invoke fn(Base&) for every filled slot, in type-id order.
Definition type_registry.h:139
const Base * find() const
The slot for T without creating one.
Definition type_registry.h:63
std::size_t count() const
How many types actually have a slot.
Definition type_registry.h:116
std::unique_ptr< Base > & slot()
The owning pointer for T's slot, grown into existence but left empty.
Definition type_registry.h:77
void clear()
Drop every slot. Types are re-registered lazily on the next ask.
Definition type_registry.h:84
void forEach(Fn &&fn)
Invoke fn(Base&) for every filled slot, in type-id order.
Definition type_registry.h:131
void removeIf(Pred &&pred)
Empty every slot pred accepts, keeping the rest where they are.
Definition type_registry.h:96
void swap(TypeRegistry &other) noexcept
Exchange every slot with another registry.
Definition type_registry.h:107