vkmEngine 1.0.15
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
48 template<typename... Ts>
50 (registerBehavior(Reflect::Traits<Ts>::NAME, [] { return std::make_unique<Ts>(); }), ...);
51 }
52
59 std::unique_ptr<Behavior> create(const std::string& name) const;
60
67 bool contains(const std::string& name) const;
68
74 std::vector<std::string> names() const;
75
79 void clear();
80
81 private:
82 BehaviorRegistry() = default;
83
90 void registerBehavior(std::string name, Factory factory);
91
92 private:
93 std::unordered_map<std::string, Factory> m_factories;
94};
95
96} // 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, each under the name its reflect block records.
Definition behavior_registry.h:49
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.
The reflection trait, specialised per type by VKM_REFLECT_BEGIN / VKM_F / VKM_REFLECT_END.
Definition reflect.h:41