forked from rasmus-storjohann/Inject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceContainer.hpp
More file actions
66 lines (53 loc) · 1.74 KB
/
InstanceContainer.hpp
File metadata and controls
66 lines (53 loc) · 1.74 KB
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
#include <memory>
namespace Injection
{
namespace Detail
{
class InstanceContainer
{
public:
InstanceContainer() = default;
InstanceContainer(InstanceContainer&& other)
: _concept(std::move(other._concept))
{}
InstanceContainer operator = (InstanceContainer&& other)
{
_concept = std::move(other._concept);
}
template <class Type, class Deleter>
InstanceContainer(std::unique_ptr<Type, Deleter> value)
: _concept(new TypeErasureModel<Type, Deleter>(std::move(value)))
{}
void* Get() const
{
return _concept->Get();
}
private:
struct TypeErasureConcept
{
virtual ~TypeErasureConcept() = default;
virtual void* Get() const = 0;
};
template <class Type, class Deleter>
struct TypeErasureModel : public TypeErasureConcept
{
TypeErasureModel(TypeErasureModel&& other)
: _value(std::move(other._value))
{}
TypeErasureModel operator = (TypeErasureModel&& other)
{
_value = std::move(other._value);
}
TypeErasureModel(std::unique_ptr<Type, Deleter> value)
: _value(std::move(value))
{}
void* Get() const override
{
return _value.get();
}
std::unique_ptr<Type, Deleter> _value;
};
std::unique_ptr<TypeErasureConcept> _concept;
};
}
}