blob: 54b09c63bed2fe34f32dc43a230c85391e5a6be6 (
plain)
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
|
#pragma once
#include "container.hpp"
#include "function_wrapper.hpp"
#include "object_wrapper.hpp"
#include <iostream>
template <typename Interface>
BindingBuilder<Interface>::BindingBuilder(Container *container) noexcept
: _container(container)
{
}
template <typename Interface>
template <typename Impl>
requires std::derived_from<Impl, Interface>
void BindingBuilder<Interface>::to() noexcept
{
using Wrapper = ObjectWrapper<Interface, Impl>;
auto wrapper = Container::Ptr<Wrapper>(new Wrapper(*_container));
_container->add(ObjectType<Interface>(),
std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
}
template <typename Interface>
void BindingBuilder<Interface>::to_factory(Interface func) noexcept
{
using Wrapper = FunctionWrapper<Interface>;
auto wrapper = Container::Ptr<Wrapper>(new Wrapper(func));
_container->add(ObjectType<Interface>(),
std::dynamic_pointer_cast<IGenericWrapper>(wrapper));
}
template <typename Interface>
auto Container::bind() noexcept -> BindingBuilder<Interface>
{
return BindingBuilder<Interface>(this);
}
template <typename Interface>
requires Abstract<Interface>
auto Container::get() const noexcept -> Ptr<Interface>
{
ObjectType<Interface> interface_type;
if (_bindings.count(interface_type) == 0)
{
std::cerr
<< "Error: Tried to get a item from the container using unbound interface '"
<< interface_type.name() << "'" << std::endl;
exit(EXIT_FAILURE);
}
auto wrapper =
std::dynamic_pointer_cast<IWrapper<Ptr<Interface>>>(_bindings.at(interface_type));
return wrapper->get();
}
template <typename Interface>
requires Function<Interface>
auto Container::get() const noexcept -> Interface
{
auto wrapper = std::dynamic_pointer_cast<IWrapper<Interface>>(
_bindings.at(ObjectType<Interface>()));
return wrapper->get();
}
|