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
|
#pragma once
#include "alloc_functor.hpp"
template <class Return>
template <class... Args>
auto InvokeReturnWrapper<Return>::call(Args &&...args) -> Return
{
return std::invoke(std::forward<Args>(args)...);
}
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define ALLOC_FUNCTOR_TEMPLATE \
template <class Function, class Allocator, class Return, class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define ALLOC_FUNCTOR AllocFunctor<Function, Allocator, Return(Args...)>
ALLOC_FUNCTOR_TEMPLATE
ALLOC_FUNCTOR::AllocFunctor(Target &&function)
: _function(
std::piecewise_construct,
std::forward_as_tuple(std::move(function)),
std::forward_as_tuple()
)
{
}
ALLOC_FUNCTOR_TEMPLATE
ALLOC_FUNCTOR::AllocFunctor(const Target &function, const Alloc &allocator)
: _function(
std::piecewise_construct,
std::forward_as_tuple(function),
std::forward_as_tuple(allocator)
)
{
}
ALLOC_FUNCTOR_TEMPLATE
ALLOC_FUNCTOR::AllocFunctor(const Target &function, Alloc &&allocator)
: _function(
std::piecewise_construct,
std::forward_as_tuple(function),
std::forward_as_tuple(std::move(allocator))
)
{
}
ALLOC_FUNCTOR_TEMPLATE
ALLOC_FUNCTOR::AllocFunctor(Target &&function, Alloc &&allocator)
: _function(
std::piecewise_construct,
std::forward_as_tuple(std::move(function)),
std::forward_as_tuple(std::move(allocator))
)
{
}
ALLOC_FUNCTOR_TEMPLATE
auto ALLOC_FUNCTOR::operator()(Args &&...args) -> Return
{
using Invoker = InvokeReturnWrapper<Return>;
return Invoker::call(_function.first(), std::forward<Args>(args)...);
}
ALLOC_FUNCTOR_TEMPLATE
auto ALLOC_FUNCTOR::target() const -> const Target &
{
return _function.first();
}
ALLOC_FUNCTOR_TEMPLATE
auto ALLOC_FUNCTOR::get_allocator() const -> const Alloc &
{
return _function.second();
}
ALLOC_FUNCTOR_TEMPLATE
auto ALLOC_FUNCTOR::clone() const -> AllocFunctor *
{
using AllocTraits = std::allocator_traits<Alloc>;
using AllocHelper = typename RebindAllocHelper<AllocTraits, AllocFunctor>::type;
auto alloc_helper = AllocHelper(_function.second());
using Destructor = AllocDestructor<AllocHelper>;
auto hold = std::unique_ptr<AllocFunctor, Destructor>(
alloc_helper.allocate(1),
_Dp(alloc_helper, 1)
);
::new (static_cast<void *>(hold.get()))
AllocFunctor(_function.first(), _Alloc(alloc_helper));
return hold.release();
}
ALLOC_FUNCTOR_TEMPLATE
void ALLOC_FUNCTOR::destroy() noexcept
{
_function.~CompressedPair<Target, Alloc>();
}
ALLOC_FUNCTOR_TEMPLATE
void ALLOC_FUNCTOR::destroy_and_delete(AllocFunctor *functor)
{
using AllocTraits = std::allocator_traits<Alloc>;
using FunctorAlloc = typename RebindAllocHelper<AllocTraits, AllocFunctor>::type;
auto functor_alloc = FunctorAlloc(functor->get_allocator());
functor->destroy();
functor_alloc.deallocate(functor, 1);
}
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define DEFAULT_ALLOC_FUNCTOR_TEMPLATE \
template <class Function, class Return, class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define DEFAULT_ALLOC_FUNCTOR DefaultAllocFunctor<Function, Return(Args...)>
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
DEFAULT_ALLOC_FUNCTOR::DefaultAllocFunctor(Target &&function)
: _function(std::move(function))
{
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
DEFAULT_ALLOC_FUNCTOR::DefaultAllocFunctor(const Target &function) : _function(function)
{
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
auto DEFAULT_ALLOC_FUNCTOR::operator()(Args &&...args) -> Return
{
using Invoker = InvokeReturnWrapper<Return>;
return Invoker::call(_function, std::forward<Args>(args)...);
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
auto DEFAULT_ALLOC_FUNCTOR::target() const -> const Target &
{
return _function;
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
auto DEFAULT_ALLOC_FUNCTOR::clone() const -> DefaultAllocFunctor *
{
std::allocator<DefaultAllocFunctor> allocator;
DefaultAllocFunctor *functor_ptr = allocator.allocate(1);
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
auto *res = ::new (static_cast<void *>(functor_ptr)) DefaultAllocFunctor(_function);
return res;
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
void DEFAULT_ALLOC_FUNCTOR::destroy() noexcept
{
_function.~_Target();
}
DEFAULT_ALLOC_FUNCTOR_TEMPLATE
void DEFAULT_ALLOC_FUNCTOR::destroy_and_delete(DefaultAllocFunctor *function)
{
function->destroy();
std::allocator<DefaultAllocFunctor> allocator;
allocator.deallocate(function, 1);
}
|