forked from phisko/kengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityFactory.hpp
54 lines (46 loc) · 1.51 KB
/
EntityFactory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <string_view>
#include <memory>
#include <GameObject.hpp>
namespace kengine
{
class EntityFactory
{
public:
virtual ~EntityFactory() = default;
public:
virtual std::unique_ptr<GameObject> make(std::string_view type, std::string_view name) = 0;
};
class ExtensibleFactory : public EntityFactory
{
public:
using Creator = std::function<std::unique_ptr<GameObject>(std::string_view name)>;
void addType(std::string_view type, const Creator &creator)
{
_creators[type.data()] = creator;
}
template<typename T>
void registerType()
{
static_assert(putils::is_reflectible<T>::value,
"Please make your type reflectible before registering it");
_creators[T::get_class_name()] = [](auto name) { return std::make_unique<T>(name); };
}
template<typename ...Types>
void registerTypes()
{
pmeta::tuple_for_each(std::make_tuple(pmeta::type<Types>()...),
[this](auto &&t)
{
registerType<pmeta_wrapped(t)>();
}
);
}
std::unique_ptr<GameObject> make(std::string_view type, std::string_view name) final
{
return (_creators.at(type.data()))(name);
}
private:
std::unordered_map<std::string, Creator> _creators;
};
}