vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
behavior_registry.h
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "core/reflect.h"
10#include "system/script/behavior.h"
11
12namespace Vkm::Engine {
13
20class BehaviorRegistry {
21 public:
22 using Factory = std::function<std::unique_ptr<Behavior>()>;
23
24 public:
25 ~BehaviorRegistry() = default;
26
27 BehaviorRegistry(const BehaviorRegistry& other) = delete;
28 BehaviorRegistry& operator=(const BehaviorRegistry& other) = delete;
29
30 BehaviorRegistry(BehaviorRegistry && other) = delete;
31 BehaviorRegistry& operator=(BehaviorRegistry && other) = delete;
32
33 public:
34 static BehaviorRegistry& get();
35
44 void registerBehavior(std::string name, Factory factory);
45
53 template<typename T>
55 registerBehavior(Reflect::Traits<T>::NAME, [] { return std::make_unique<T>(); });
56 }
57
67 template<typename... Ts>
69 (registerBehavior<Ts>(), ...);
70 }
71
78 std::unique_ptr<Behavior> create(const std::string& name) const;
79
86 bool contains(const std::string& name) const;
87
93 std::vector<std::string> names() const;
94
98 void clear();
99
100 private:
101 BehaviorRegistry() = default;
102
103 private:
104 std::unordered_map<std::string, Factory> m_factories;
105};
106
107} // namespace Vkm::Engine
std::unique_ptr< Behavior > create(const std::string &name) const
Create a fresh instance by name.
void registerBehaviors()
Register every type in Ts, in order, as registerBehavior<T>() does.
Definition behavior_registry.h:68
void registerBehavior(std::string name, Factory factory)
Register name -> factory.
std::vector< std::string > names() const
List every registered behavior name, sorted.
bool contains(const std::string &name) const
Report whether name has a registered factory.
void clear()
Drop every registered factory; call before unloading the module whose code they hold.
void registerBehavior()
Register T under the name its reflect block records.
Definition behavior_registry.h:54
The reflection trait, specialised per type by VKM_REFLECT_BEGIN / VKM_F / VKM_REFLECT_END.
Definition reflect.h:41