aboutsummaryrefslogtreecommitdiff
path: root/src/DI/container.hpp
blob: 24028941cead9c4da55f1b7ae4eaa8d1c24b2f41 (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
#pragma once

#include "DI/object_type.hpp"
#include "interfaces/wrapper.hpp"

#include <functional>
#include <memory>
#include <type_traits>
#include <unordered_map>

class Container;

template <typename Interface>
class BindingBuilder
{
public:
	explicit BindingBuilder(Container *container);

	template <class ObjectImpl,
			  class = std::enable_if_t<std::is_base_of_v<Interface, ObjectImpl>>>
	void to();

	void to_factory(Interface func);

private:
	Container *_container;
};

template <typename Satan>
struct is_func : public std::false_type // NOLINT(readability-identifier-naming)
{
};

template <typename Satan, typename... Args>
struct is_func<std::function<Satan(Args...)>> : public std::true_type
{
};

class Container
{
public:
	Container() = default;

	template <class Interface>
	BindingBuilder<Interface> bind();

	template <class Interface, class = std::enable_if_t<std::is_abstract_v<Interface>>>
	std::shared_ptr<Interface> get() const;

	/*
	template <typename Interface,
			  typename = std::enable_if_t<
				  !std::is_abstract_v<Interface> &&
				  std::is_invocable_v<Interface, typename Interface::argument_type>>>
	Interface get() const;
	*/

	template <typename Interface, typename = std::enable_if_t<is_func<Interface>::value>>
	Interface get() const;

	std::unordered_map<BaseObjectType, std::shared_ptr<IGenericWrapper>, ObjectTypeHasher>
		bindings;
};

#include "container.tpp"