aboutsummaryrefslogtreecommitdiff
path: root/src/DI/container.tpp
blob: 21bf81a1bd4f335d1fcaf321b73b89a6f00c552f (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
#pragma once

#include "container.hpp"

#include "function_wrapper.hpp"
#include "object_wrapper.hpp"

template <class Interface>
BindingBuilder<Interface>::BindingBuilder(Container *container) : _container(container)
{
}

template <class Interface>
template <class ObjectImpl, class>
void BindingBuilder<Interface>::to()
{
	_container->bindings.emplace(
		ObjectType<Interface>(),
		std::dynamic_pointer_cast<IGenericWrapper>(
			std::make_shared<ObjectWrapper<Interface, ObjectImpl>>(*_container)));
}

template <class Interface>
void BindingBuilder<Interface>::to_factory(Interface func)
{
	_container->bindings.emplace(ObjectType<Interface>(),
								 std::dynamic_pointer_cast<IGenericWrapper>(
									 std::make_shared<FunctionWrapper<Interface>>(func)));
}

template <class Interface>
BindingBuilder<Interface> Container::bind()
{
	return BindingBuilder<Interface>(this);
}

template <class Interface, class>
std::shared_ptr<Interface> Container::get() const
{
	auto wrapper = std::dynamic_pointer_cast<IWrapper<std::shared_ptr<Interface>>>(
		bindings.at(ObjectType<Interface>()));

	return wrapper->get();
}

template <typename Interface, typename>
Interface Container::get() const
{
	auto wrapper = std::dynamic_pointer_cast<IWrapper<Interface>>(
		bindings.at(ObjectType<Interface>()));

	return wrapper->get();
}