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
|
#pragma once
#include "copyable_functor.hpp"
#include <memory>
#include <utility>
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define COPYABLE_FUNCTOR_TEMPLATE \
template <class Function, class Allocator, class Return, class... Args>
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define COPYABLE_FUNCTOR CopyableFunctor<Function, Allocator, Return(Args...)>
COPYABLE_FUNCTOR_TEMPLATE
COPYABLE_FUNCTOR::CopyableFunctor(Function &&function) : _functor(std::move(function)) {}
COPYABLE_FUNCTOR_TEMPLATE
COPYABLE_FUNCTOR::CopyableFunctor(const Function &function, const Allocator &allocator)
: _functor(function, allocator)
{
}
COPYABLE_FUNCTOR_TEMPLATE
COPYABLE_FUNCTOR::CopyableFunctor(const Function &function, Allocator &&allocator)
: _functor(function, std::move(allocator))
{
}
COPYABLE_FUNCTOR_TEMPLATE
COPYABLE_FUNCTOR::CopyableFunctor(Function &&function, Allocator &&allocator)
: _functor(std::move(function), std::move(allocator))
{
}
COPYABLE_FUNCTOR_TEMPLATE
auto COPYABLE_FUNCTOR::operator()(Args &&...args) -> Return
{
return _functor(std::forward<Args>(args)...);
}
COPYABLE_FUNCTOR_TEMPLATE
auto COPYABLE_FUNCTOR::clone() const -> ICopyableFunctor<Return(Args...)> *
{
using AllocTraits = std::allocator_traits<Allocator>;
using AllocHelper = typename RebindAllocHelper<AllocTraits, CopyableFunctor>::type;
auto alloc_helper = AllocHelper(_functor.get_allocator());
using Destructor = AllocDestructor<AllocHelper>;
auto hold = std::unique_ptr<CopyableFunctor, Destructor>(
alloc_helper.allocate(1),
Destructor(alloc_helper, 1)
);
::new (static_cast<void *>(hold.get()))
CopyableFunctor(_functor.target(), Allocator(alloc_helper));
return hold.release();
}
COPYABLE_FUNCTOR_TEMPLATE
void COPYABLE_FUNCTOR::clone(ICopyableFunctor<Return(Args...)> *functor) const
{
::new (static_cast<void *>(functor))
CopyableFunctor(_functor.target(), _functor.get_allocator());
}
COPYABLE_FUNCTOR_TEMPLATE
void COPYABLE_FUNCTOR::destroy() noexcept
{
_functor.destroy();
}
COPYABLE_FUNCTOR_TEMPLATE
void COPYABLE_FUNCTOR::destroy_deallocate() noexcept
{
using AllocTraits = std::allocator_traits<Allocator>;
using AllocHelper = typename RebindAllocHelper<AllocTraits, CopyableFunctor>::type;
auto alloc_helper = AllocHelper(_functor.get_allocator());
_functor.destroy();
alloc_helper.deallocate(this, 1);
}
COPYABLE_FUNCTOR_TEMPLATE
auto COPYABLE_FUNCTOR::target(const std::type_info &type_info) const noexcept -> const
void *
{
if (type_info == typeid(Function))
{
return std::addressof(_functor.target());
}
return nullptr;
}
COPYABLE_FUNCTOR_TEMPLATE
auto COPYABLE_FUNCTOR::target_type() const noexcept -> const std::type_info &
{
return typeid(Function);
}
|