aboutsummaryrefslogtreecommitdiff
path: root/src/DI/value_functor.tpp
blob: 3665f185f2b54bd012851e50d072d8dc285ad1c4 (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
#pragma once

#include "value_functor.hpp"

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define VALUE_FUNCTOR_TEMPLATE template <class Return, class... Args>

// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define VALUE_FUNCTOR ValueFunctor<Return(Args...)>

VALUE_FUNCTOR_TEMPLATE
VALUE_FUNCTOR::ValueFunctor() noexcept : _functor(nullptr) {}

VALUE_FUNCTOR_TEMPLATE
template <class Function, class Allocator>
VALUE_FUNCTOR::ValueFunctor(Function &&function, const Allocator &allocator)
	: _functor(nullptr)
{
	using AllocTraits = std::allocator_traits<Allocator>;

	using Functor = CopyableFunctor<Function, Allocator, Return(Args...)>;

	using FunctorAlloc = typename RebindAllocHelper<AllocTraits, Functor>::type;

	if (not_null(function))
	{
		auto functor_alloc = FunctorAlloc(allocator);

		if (sizeof(Functor) <= sizeof(_buf) &&
			std::is_nothrow_copy_constructible<Function>::value &&
			std::is_nothrow_copy_constructible<FunctorAlloc>::value)
		{
			// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
			_functor = ::new (static_cast<void *>(&_buf))
				Functor(std::forward<Function>(function), Allocator(functor_alloc));
		}
		else
		{
			using Destructor = AllocDestructor<FunctorAlloc>;

			auto hold = std::unique_ptr<TargetFunctor, Destructor>(
				functor_alloc.allocate(1), Destructor(functor_alloc, 1));

			::new (static_cast<void *>(hold.get()))
				Functor(std::forward<Function>(function), Allocator(allocator));

			_functor = hold.release();
		}
	}
}

VALUE_FUNCTOR_TEMPLATE
VALUE_FUNCTOR::ValueFunctor(const ValueFunctor &val_functor)
{
	if (val_functor._functor == nullptr)
	{
		_functor = nullptr;
	}
	else if (static_cast<void *>(val_functor._functor) == &val_functor._buf)
	{
		_functor = _as_copyable_functor(&_buf);
		val_functor._functor->clone(_functor);
	}
	else
	{
		_functor = val_functor._functor->clone();
	}
}

VALUE_FUNCTOR_TEMPLATE
VALUE_FUNCTOR::ValueFunctor(ValueFunctor &&val_functor) noexcept
{
	if (val_functor._functor == nullptr)
	{
		_functor = nullptr;
	}
	else if (static_cast<void *>(val_functor._functor) == &val_functor._buf)
	{
		_functor = _as_copyable_functor(&_buf);
		val_functor._functor->clone(_functor);
	}
	else
	{
		_functor = val_functor._functor;
		val_functor._functor = nullptr;
	}
}

VALUE_FUNCTOR_TEMPLATE
VALUE_FUNCTOR::~ValueFunctor()
{
	if (static_cast<void *>(_functor) == &_buf)
	{
		_functor->destroy();
	}
	else if (_functor)
	{
		_functor->destroy_deallocate();
	}
}

VALUE_FUNCTOR_TEMPLATE
auto VALUE_FUNCTOR::operator=(ValueFunctor &&val_functor) noexcept -> ValueFunctor &
{
	*this = nullptr;

	if (val_functor._functor == nullptr)
	{
		_functor = nullptr;
	}
	else if (static_cast<void *>(val_functor._functor) == &val_functor._buf)
	{
		_functor = _as_copyable_functor(&_buf);
		val_functor._functor->clone(_functor);
	}
	else
	{
		_functor = val_functor._functor;
		val_functor._functor = nullptr;
	}

	return *this;
}

VALUE_FUNCTOR_TEMPLATE
auto VALUE_FUNCTOR::operator=(std::nullptr_t) -> ValueFunctor &
{
	TargetFunctor *old_functor = _functor;

	_functor = nullptr;

	if (static_cast<void *>(old_functor) == &_buf)
	{
		old_functor->destroy();
	}
	else if (old_functor)
	{
		old_functor->destroy_deallocate();
	}

	return *this;
}

VALUE_FUNCTOR_TEMPLATE
auto VALUE_FUNCTOR::operator()(Args &&...args) const -> Return
{
	if (_functor == nullptr)
	{
		std::abort();
	}

	return (*_functor)(std::forward<Args>(args)...);
}

VALUE_FUNCTOR_TEMPLATE
VALUE_FUNCTOR::operator bool() const noexcept
{
	return _functor != nullptr;
}

VALUE_FUNCTOR_TEMPLATE
auto VALUE_FUNCTOR::target_type() const noexcept -> const std::type_info &
{
	if (_functor == nullptr)
	{
		return typeid(void);
	}

	return _functor->target_type();
}

VALUE_FUNCTOR_TEMPLATE
template <typename Target>
auto VALUE_FUNCTOR::target() const noexcept -> const Target *
{
	if (_functor == nullptr)
	{
		return nullptr;
	}

	return static_cast<const Target *>(_functor->target(typeid(Target)));
}

VALUE_FUNCTOR_TEMPLATE
auto VALUE_FUNCTOR::_as_copyable_functor(void *function_ptr) -> TargetFunctor *
{
	// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
	return reinterpret_cast<TargetFunctor *>(function_ptr);
}