-
Notifications
You must be signed in to change notification settings - Fork 0
/
Factory.h
83 lines (65 loc) · 2 KB
/
Factory.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include "Singleton.h"
#include "Node.h"
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <type_traits>
namespace tngl {
struct NodeBuilderBase;
using NodeBuilders = std::multimap<std::string, NodeBuilderBase const*>;
using NodeBuilderRegistry = Singleton<NodeBuilders>;
struct NodeBuilderBase {
private:
std::string _name;
std::type_info const& _info;
std::function<std::unique_ptr<Node>()> _createFunc;
public:
template<typename Func>
NodeBuilderBase(std::string name, std::type_info const& info, Func f)
: _name{std::move(name)}
, _info{info}
, _createFunc{[=] { return std::unique_ptr<Node>{f()}; }} {
NodeBuilderRegistry::getInstance().emplace(_name, this);
}
NodeBuilderBase(NodeBuilderBase const&) = delete;
NodeBuilderBase& operator=(NodeBuilderBase const&) = delete;
~NodeBuilderBase() {
NodeBuilderRegistry::getInstance().erase(_name);
}
std::unique_ptr<Node> create() const {
return _createFunc();
}
std::type_info const& getType() const {
return _info;
}
};
template<typename T>
struct NodeBuilder : NodeBuilderBase {
using NodeBuilderBase::NodeBuilderBase;
NodeBuilder(std::string const& name)
: NodeBuilderBase(name, typeid(T), []{
if constexpr (std::is_default_constructible_v<T>) {
return std::make_unique<T>();
} else {
return nullptr;
}
})
{}
template <typename Func>
NodeBuilder(std::string const& name, Func f)
: NodeBuilderBase(name, typeid(T), f)
{}
};
namespace detail {
bool is_type_ancestor(const std::type_info& base, const std::type_info& deriv);
}
using Builders = NodeBuilderRegistry::value_type;
Builders getBuildersForType(const std::type_info& base);
// get all builders that can produce a specialization of T or a T itself
template<typename T>
Builders getBuildersForType() {
return getBuildersForType(typeid(T));
}
}