aboutsummaryrefslogtreecommitdiff
path: root/src/DI/factory.hpp
blob: 035e557cd9446a1ba38bd025033582ad7450c5dc (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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once

#include "DI/interfaces/copyable_functor.hpp"

#include "DI/alloc_functor.hpp"
#include "DI/allocation.hpp"
#include "DI/copyable_functor.hpp"
#include "DI/extra_concepts.hpp"
#include "DI/strip_signature.hpp"
#include "DI/value_functor.hpp"

#include <type_traits>
#include <utility>

template <class>
class Factory;

template <class Return>
// NOLINTNEXTLINE(readability-identifier-naming)
struct maybe_derive_from_unary_function
{
};

template <class Return, class Arg>
struct maybe_derive_from_unary_function<Return(Arg)>
	: public std::unary_function<Arg, Return>
{
};

template <class Return>
// NOLINTNEXTLINE(readability-identifier-naming)
struct maybe_derive_from_binary_function
{
};

template <class Return, class ArgOne, class ArgTwo>
struct maybe_derive_from_binary_function<Return(ArgOne, ArgTwo)>
	: public std::binary_function<ArgOne, ArgTwo, Return>
{
};

template <class Function>
auto not_null(Function const & /*unused*/) -> bool
{
	return true;
}

template <class Function>
auto not_null(Function *function_ptr) -> bool
{
	return function_ptr;
}

template <class Return, class Class>
auto not_null(Return Class::*method_ptr) -> bool
{
	return method_ptr;
}

template <class Function>
auto not_null(Factory<Function> const &factory) -> bool
{
	return !!factory;
}

template <class Type>
// NOLINTNEXTLINE(readability-identifier-naming)
struct uncvref
{
	using type =
		typename std::remove_cv<typename std::remove_reference<Type>::type>::type;
};

template <class Type>
// NOLINTNEXTLINE(readability-identifier-naming)
using uncvref_t = typename uncvref<Type>::type;

template <class Tp, class Up, class = void>
// NOLINTNEXTLINE(readability-identifier-naming)
struct is_core_convertible : public std::false_type
{
};

template <class Tp, class Up>
struct is_core_convertible<
	Tp,
	Up,
	decltype(static_cast<void (*)(Up)>(0)(static_cast<Tp (*)()>(0)()))>
	: public std::true_type
{
};

template <class Tp, class Up>
constexpr bool is_core_convertible_v = is_core_convertible<Tp, Up>::value;

template <typename Function, typename Return, typename... Args>
concept Callable = !std::is_same_v<uncvref_t<Function>, Factory<Return(Args...)>> &&
				   std::is_invocable_v<Function, Args...> &&
				   is_core_convertible_v<std::invoke_result_t<Function, Args...>, Return>;

template <class Return, class... Args>
class Factory<Return(Args...)> : public maybe_derive_from_unary_function<Return(Args...)>,
								 public maybe_derive_from_binary_function<Return(Args...)>
{
public:
	using Result = Return;

	Factory() noexcept = default;

	explicit Factory(std::nullptr_t) noexcept {}

	Factory(const Factory &factory);

	Factory(Factory &&factory) noexcept;

	template <class Function>
	requires Callable<Function, Return, Args...>
	// NOLINTNEXTLINE(google-explicit-constructor)
	Factory(Function function) : _functor(std::move(function))
	{
	}

	auto operator=(const Factory &factory) = delete;

	auto operator=(Factory &&factory) noexcept -> Factory &;

	auto operator=(std::nullptr_t) noexcept -> Factory &;

	~Factory();

	explicit operator bool() const noexcept;

	// deleted overloads close possible hole in the type system
	template <class RhsReturn, class... RhsArgs>
	auto operator==(const Factory<RhsReturn(RhsArgs...)> &) const -> bool = delete;

	template <class RhsReturn, class... RhsArgs>
	auto operator!=(const Factory<RhsReturn(RhsArgs...)> &) const -> bool = delete;

	auto operator()(Args... args) const -> Return;

	[[nodiscard]] auto target_type() const noexcept -> const std::type_info &;

	template <typename Tp>
	auto target() noexcept -> Tp *;

	template <typename Tp>
	auto target() const noexcept -> const Tp *;

private:
	using ValFunctor = ValueFunctor<Return(Args...)>;

	ValFunctor _functor;
};

template <class Return, class... Args>
Factory(Return (*)(Args...)) -> Factory<Return(Args...)>;

template <
	class Function,
	class Stripped = strip_signature_t<decltype(&Function::operator())>>
Factory(Function) -> Factory<Stripped>;

template <class Return, class... Args>
inline auto operator==(const Factory<Return(Args...)> &factory, std::nullptr_t) noexcept
	-> bool
{
	return !factory;
}

template <class Return, class... Args>
inline auto operator==(std::nullptr_t, const Factory<Return(Args...)> &factory) noexcept
	-> bool
{
	return !factory;
}

template <class Return, class... Args>
inline auto operator!=(const Factory<Return(Args...)> &factory, std::nullptr_t) noexcept
	-> bool
{
	return static_cast<bool>(factory);
}

template <class Return, class... Args>
inline auto operator!=(std::nullptr_t, const Factory<Return(Args...)> &factory) noexcept
	-> bool
{
	return static_cast<bool>(factory);
}

#include "factory.tpp"