forked from vastik/xray-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrFactory.h
125 lines (103 loc) · 2.69 KB
/
xrFactory.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include "BindFactory/BindFactory.h"
class XRCORE_API xrFactory
{
protected:
xrFactory() = default;
~xrFactory() = default;
public:
template<typename T>
static BindFactoryTypeRegistrator<T>* bind()
{
return d_factory.bind<T>();
}
template<typename T, typename ... Args>
static T* get(Args ... args)
{
return d_factory.get<T>(args...);
}
template<typename T, typename V, typename ... Args>
static V* get(Args ... args)
{
return d_factory.get<T, V>(args ...);
}
template<typename T, typename ... Args>
static std::shared_ptr<T> getShared(Args ... args)
{
return d_factory.getShared<T>(args ...);
}
template<typename T, typename V, typename ... Args>
static std::shared_ptr<V> getShared(Args ... args)
{
return d_factory.getShared<T, V>(args ...);
}
template<typename T>
static void unbind()
{
return d_factory.unbind<T>();
}
static void bind(BindFactory::Injector* inject)
{
return d_factory.bind(inject);
}
static void unbind(BindFactory::Injector* inject)
{
return d_factory.unbind(inject);
}
private:
static BindFactory d_factory;
};
template<typename T, typename ... Args>
class xrInject : public BindFactoryTypeInjector<T>
{
public:
xrInject(Args ... args) : d_args(args...)
{
xrFactory::bind(this);
}
~xrInject()
{
xrFactory::unbind(this);
}
void inject() override
{
inject_type(d_args, std::make_index_sequence<std::tuple_size<typename std::decay<std::tuple<Args...>>::type>::value>{});
}
void uninject() override
{
d_object = nullptr;
}
private:
template<typename TupleType, std::size_t... index>
void inject_type(TupleType& tup, std::index_sequence<index...>) {
d_object = xrFactory::get<T>(std::get<index>(tup)...);
}
std::tuple<Args...> d_args;
};
template<typename T, typename ... Args>
class xrSharedInject : public BindFactoryTypeSharedInjector<T>
{
public:
xrSharedInject(Args ... args) : d_args(args...)
{
xrFactory::bind(this);
}
~xrSharedInject()
{
xrFactory::unbind(this);
}
void inject() override
{
inject_type(d_args, std::make_index_sequence<std::tuple_size<typename std::decay<std::tuple<Args...>>::type>::value>{});
}
void uninject() override
{
d_object = nullptr;
}
private:
template<typename TupleType, std::size_t... index>
void inject_type(TupleType& tup, std::index_sequence<index...>) {
d_object = xrFactory::getShared<T>(std::get<index>(tup)...);
}
std::tuple<Args...> d_args;
};