-
Notifications
You must be signed in to change notification settings - Fork 7
/
fixed_function.h
218 lines (176 loc) · 5.74 KB
/
fixed_function.h
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_FIXED_FUNCTION_H
#define EASTL_FIXED_FUNCTION_H
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once
#endif
#include <eastl/internal/function_detail.h>
namespace eastl
{
template <int, typename>
class fixed_function;
namespace internal
{
template <typename>
struct is_fixed_function
: public eastl::false_type {};
template <int SIZE_IN_BYTES, typename R, typename... Args>
struct is_fixed_function<eastl::fixed_function<SIZE_IN_BYTES, R(Args...)>>
: public eastl::true_type {};
template<typename T>
EA_CONSTEXPR bool is_fixed_function_v = is_fixed_function<T>::value;
}
#define EASTL_INTERNAL_FIXED_FUNCTION_STATIC_ASSERT(TYPE) \
static_assert(sizeof(TYPE) <= sizeof(typename Base::FunctorStorageType), \
"fixed_function local buffer is not large enough to hold the callable object.")
#define EASTL_INTERNAL_FIXED_FUNCTION_NEW_SIZE_STATIC_ASSERT(NEW_SIZE_IN_BYTES) \
static_assert(SIZE_IN_BYTES >= NEW_SIZE_IN_BYTES, \
"fixed_function local buffer is not large enough to hold the new fixed_function type.")
template <typename Functor>
using EASTL_DISABLE_OVERLOAD_IF_FIXED_FUNCTION =
eastl::disable_if_t<internal::is_fixed_function_v<eastl::decay_t<Functor>>>;
// fixed_function
//
template <int SIZE_IN_BYTES, typename R, typename... Args>
class fixed_function<SIZE_IN_BYTES, R(Args...)> : public internal::function_detail<SIZE_IN_BYTES, R(Args...)>
{
using Base = internal::function_detail<SIZE_IN_BYTES, R(Args...)>;
public:
using typename Base::result_type;
fixed_function() EASTL_NOEXCEPT = default;
fixed_function(std::nullptr_t p) EASTL_NOEXCEPT
: Base(p)
{
}
fixed_function(const fixed_function& other)
: Base(other)
{
}
fixed_function(fixed_function&& other)
: Base(eastl::move(other))
{
}
template <typename Functor,
typename = EASTL_INTERNAL_FUNCTION_VALID_FUNCTION_ARGS(Functor, R, Args..., Base, fixed_function),
typename = EASTL_DISABLE_OVERLOAD_IF_FIXED_FUNCTION<Functor>>
fixed_function(Functor functor)
: Base(eastl::move(functor))
{
EASTL_INTERNAL_FIXED_FUNCTION_STATIC_ASSERT(Functor);
}
template<int NEW_SIZE_IN_BYTES>
fixed_function(const fixed_function<NEW_SIZE_IN_BYTES, R(Args...)>& other)
: Base(other)
{
EASTL_INTERNAL_FIXED_FUNCTION_NEW_SIZE_STATIC_ASSERT(NEW_SIZE_IN_BYTES);
}
template<int NEW_SIZE_IN_BYTES>
fixed_function(fixed_function<NEW_SIZE_IN_BYTES, R(Args...)>&& other)
: Base(eastl::move(other))
{
EASTL_INTERNAL_FIXED_FUNCTION_NEW_SIZE_STATIC_ASSERT(NEW_SIZE_IN_BYTES);
}
~fixed_function() EASTL_NOEXCEPT = default;
fixed_function& operator=(const fixed_function& other)
{
Base::operator=(other);
return *this;
}
fixed_function& operator=(fixed_function&& other)
{
Base::operator=(eastl::move(other));
return *this;
}
fixed_function& operator=(std::nullptr_t p) EASTL_NOEXCEPT
{
Base::operator=(p);
return *this;
}
template<int NEW_SIZE_IN_BYTES>
fixed_function& operator=(const fixed_function<NEW_SIZE_IN_BYTES, R(Args...)>& other)
{
EASTL_INTERNAL_FIXED_FUNCTION_NEW_SIZE_STATIC_ASSERT(NEW_SIZE_IN_BYTES);
Base::operator=(other);
return *this;
}
template<int NEW_SIZE_IN_BYTES>
fixed_function& operator=(fixed_function<NEW_SIZE_IN_BYTES, R(Args...)>&& other)
{
EASTL_INTERNAL_FIXED_FUNCTION_NEW_SIZE_STATIC_ASSERT(NEW_SIZE_IN_BYTES);
Base::operator=(eastl::move(other));
return *this;
}
template <typename Functor,
typename = EASTL_INTERNAL_FUNCTION_VALID_FUNCTION_ARGS(Functor, R, Args..., Base, fixed_function),
typename = EASTL_DISABLE_OVERLOAD_IF_FIXED_FUNCTION<Functor>>
fixed_function& operator=(Functor&& functor)
{
EASTL_INTERNAL_FIXED_FUNCTION_STATIC_ASSERT(eastl::decay_t<Functor>);
Base::operator=(eastl::forward<Functor>(functor));
return *this;
}
template <typename Functor>
fixed_function& operator=(eastl::reference_wrapper<Functor> f) EASTL_NOEXCEPT
{
EASTL_INTERNAL_FIXED_FUNCTION_STATIC_ASSERT(eastl::reference_wrapper<Functor>);
Base::operator=(f);
return *this;
}
void swap(fixed_function& other) EASTL_NOEXCEPT
{
Base::swap(other);
}
explicit operator bool() const EASTL_NOEXCEPT
{
return Base::operator bool();
}
R operator ()(Args... args) const
{
return Base::operator ()(eastl::forward<Args>(args)...);
}
#if EASTL_RTTI_ENABLED
const std::type_info& target_type() const EASTL_NOEXCEPT
{
return Base::target_type();
}
template <typename Functor>
Functor* target() EASTL_NOEXCEPT
{
return Base::target();
}
template <typename Functor>
const Functor* target() const EASTL_NOEXCEPT
{
return Base::target();
}
#endif
};
template <int S, typename R, typename... Args>
bool operator==(const fixed_function<S, R(Args...)>& f, std::nullptr_t) EASTL_NOEXCEPT
{
return !f;
}
template <int S, typename R, typename... Args>
bool operator==(std::nullptr_t, const fixed_function<S, R(Args...)>& f) EASTL_NOEXCEPT
{
return !f;
}
template <int S, typename R, typename... Args>
bool operator!=(const fixed_function<S, R(Args...)>& f, std::nullptr_t) EASTL_NOEXCEPT
{
return !!f;
}
template <int S, typename R, typename... Args>
bool operator!=(std::nullptr_t, const fixed_function<S, R(Args...)>& f) EASTL_NOEXCEPT
{
return !!f;
}
template <int S, typename R, typename... Args>
void swap(fixed_function<S, R(Args...)>& lhs, fixed_function<S, R(Args...)>& rhs)
{
lhs.swap(rhs);
}
} // namespace eastl
#endif // EASTL_FIXED_FUNCTION_H