NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
functional/function.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
2#define NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
3
10
11#include <typeinfo>
14NEFORCE_BEGIN_NAMESPACE__
15
21
28template <typename Sign>
30
32NEFORCE_BEGIN_INNER__
33
40enum class FUNCTION_OPERATE {
41 GET_TYPE_INFO,
42 GET_PTR,
43 COPY_PTR,
44 DESTROY_PTR
45};
46
47
54class __undefined_util;
55
62union __nocopy_type {
63 void* object_;
64 const void* const_object_;
65 void (*function_pointer_)();
66 void (__undefined_util::*member_pointer_)();
67};
68
75union storage_data {
76 storage_data() noexcept = default;
77
78 NEFORCE_NODISCARD void* access() noexcept { return &data_[0]; }
79 NEFORCE_NODISCARD const void* access() const noexcept { return &data_[0]; }
80
81 template <typename T>
82 NEFORCE_NODISCARD T& access() noexcept {
83 return *static_cast<T*>(access());
84 }
85
86 template <typename T>
87 NEFORCE_NODISCARD const T& access() const noexcept {
88 return *static_cast<const T*>(access());
89 }
90
91 __nocopy_type unused_;
92 byte_t data_[sizeof(__nocopy_type)];
93};
94
101class __function_base {
102public:
103 static constexpr size_t max_size_ = sizeof(__nocopy_type);
104 static constexpr size_t max_align_ = alignof(__nocopy_type);
105
113 template <typename F>
114 class __manager_base {
115 protected:
116 static constexpr bool stored_ = is_location_invariant_v<F> && sizeof(F) <= max_size_ &&
117 alignof(F) <= max_align_ && max_align_ % alignof(F) == 0;
118
119 private:
120 template <typename U, bool = stored_>
121 struct __get_pointer_impl;
122
123 template <typename U>
124 struct __get_pointer_impl<U, true> {
125 static U* get(const storage_data& src) noexcept {
126 const U& f = src.access<U>();
127 return const_cast<U*>(_NEFORCE addressof(f));
128 }
129 };
130
131 template <typename U>
132 struct __get_pointer_impl<U, false> {
133 static U* get(const storage_data& src) noexcept { return src.access<U*>(); }
134 };
135
136 protected:
137 using storage_ = bool_constant<stored_>;
138
139 static F* get_pointer(const storage_data& src) noexcept { return __get_pointer_impl<F>::get(src); }
140
141 private:
142 template <typename Fn>
143 static void create(storage_data& data, Fn&& f, true_type) {
144 ::new (data.access()) F(_NEFORCE forward<Fn>(f));
145 }
146 template <typename Fn>
147 static void create(storage_data& data, Fn&& f, false_type) {
148 data.access<F*>() = new F(_NEFORCE forward<Fn>(f));
149 }
150
151 static void destroy(storage_data& data, true_type) { data.access<F>().~F(); }
152 static void destroy(storage_data& data, false_type) { delete data.access<F*>(); }
153
154 public:
155 static bool manage(storage_data& dest, const storage_data& src, const FUNCTION_OPERATE oper) {
156 switch (oper) {
157 case FUNCTION_OPERATE::GET_TYPE_INFO:
158 dest.access<const std::type_info*>() = &typeid(F);
159 break;
160 case FUNCTION_OPERATE::GET_PTR:
161 dest.access<F*>() = __manager_base::get_pointer(src);
162 break;
163 case FUNCTION_OPERATE::COPY_PTR:
164 __manager_base::init_func(dest, *const_cast<const F*>(__manager_base::get_pointer(src)));
165 break;
166 case FUNCTION_OPERATE::DESTROY_PTR:
167 __manager_base::destroy(dest, storage_());
168 break;
169 }
170 return false;
171 }
172
173 template <typename Fn>
174 static void init_func(storage_data& func,
176 __manager_base::create(func, _NEFORCE forward<Fn>(f), storage_());
177 }
178
179 template <typename Sign>
180 static bool not_empty_function(const _NEFORCE function<Sign>& f) noexcept {
181 return static_cast<bool>(f);
182 }
183 template <typename T>
184 static bool not_empty_function(T* fptr) noexcept {
185 return fptr != nullptr;
186 }
187 template <typename Class, typename T>
188 static bool not_empty_function(T Class::* mptr) noexcept {
189 return mptr != nullptr;
190 }
191 template <typename T>
192 static bool not_empty_function(const T&) noexcept {
193 return true;
194 }
195 };
196
197 using manage_type = bool (*)(storage_data&, const storage_data&, FUNCTION_OPERATE);
198
199 storage_data func_{};
200 manage_type manager_ = nullptr;
201
202
203 __function_base() noexcept = default;
204 ~__function_base() {
205 if (manager_) {
206 manager_(func_, func_, FUNCTION_OPERATE::DESTROY_PTR);
207 }
208 }
209
210 NEFORCE_NODISCARD bool empty() const { return !manager_; }
211};
212
221template <typename Sign, typename F>
222class __function_manage_handler;
223
224template <typename Res, typename F, typename... Args>
225class __function_manage_handler<Res(Args...), F> : public __function_base::__manager_base<F> {
226private:
227 using base_type = __function_base::__manager_base<F>;
228
229public:
230 static bool manage(storage_data& dest, const storage_data& src, FUNCTION_OPERATE oper) {
231 switch (oper) {
232 case FUNCTION_OPERATE::GET_TYPE_INFO:
233 dest.access<const std::type_info*>() = &typeid(F);
234 break;
235 case FUNCTION_OPERATE::GET_PTR:
236 dest.access<F*>() = base_type::get_pointer(src);
237 break;
238 default:
239 base_type::manage(dest, src, oper);
240 }
241 return false;
242 }
243
244 static Res invoke(const storage_data& f, Args&&... args) {
245 return _NEFORCE invoke_r<Res>(*base_type::get_pointer(f), _NEFORCE forward<Args>(args)...);
246 }
247
248 template <typename Fn>
249 static constexpr bool nothrow_init() noexcept {
251 }
252};
253
254template <>
255class __function_manage_handler<void, void> {
256public:
257 static bool manage(storage_data&, const storage_data&, FUNCTION_OPERATE) { return false; }
258};
259
260template <typename Sign, typename F, bool Valid = is_object_v<F>>
261struct __function_handler_dispatch : __function_manage_handler<Sign, remove_cv_t<F>> {};
262
263template <typename Sign, typename F>
264struct __function_handler_dispatch<Sign, F, false> : __function_manage_handler<void, void> {};
265
266NEFORCE_END_INNER__
268
276template <typename Res, typename... Args>
277class function<Res(Args...)> : inner::__function_base {
278private:
279 using invoker_type = Res (*)(const inner::storage_data&, Args&&...);
280
281private:
282 invoker_type invoker_ = nullptr;
283
284private:
285 template <typename F, bool IsSelf = is_same_v<remove_cvref_t<F>, function>>
286 using enable_decay_t = typename enable_if_t<!IsSelf, decay<F>>::type;
287
288 template <typename F, typename = void>
289 struct callable_t : false_type {};
290
291 template <typename F>
292 struct callable_t<
294 : true_type {};
295
296 template <typename F>
297 using handler_t = inner::__function_manage_handler<Res(Args...), decay_t<F>>;
298
299 template <typename F, enable_if_t<is_object_v<F>, int> = 0>
300 NEFORCE_ALWAYS_INLINE const F* __target_impl() const noexcept {
301 if (manager_ == &inner::__function_handler_dispatch<Res(Args...), F>::manage) {
302 inner::storage_data ptr{};
303 manager_(ptr, func_, inner::FUNCTION_OPERATE::GET_PTR);
304 return ptr.access<const F*>();
305 }
306 return nullptr;
307 }
308
309 template <typename F, enable_if_t<!is_object_v<F>, int> = 0>
310 NEFORCE_ALWAYS_INLINE const F* __target_impl() const noexcept {
311 return nullptr;
312 }
313
314public:
315 using result_type = Res;
316
321 function(nullptr_t np = nullptr) noexcept :
322 __function_base() {}
323
328 function(const function& other) :
329 __function_base() {
330 if (static_cast<bool>(other)) {
331 other.manager_(func_, other.func_, inner::FUNCTION_OPERATE::COPY_PTR);
332 invoker_ = other.invoker_;
333 manager_ = other.manager_;
334 }
335 }
336
341 function(function&& other) noexcept :
342 __function_base() {
343 func_ = other.func_;
344 manager_ = other.manager_;
345 invoker_ = other.invoker_;
346 other.manager_ = nullptr;
347 other.invoker_ = nullptr;
348 }
349
355 template <typename F, enable_if_t<callable_t<F>::value, int> = 0>
356 function(F&& callable) noexcept(handler_t<F>::template nothrow_init<F>()) :
357 __function_base() {
359 "target of function must be constructible");
360
361 using handler = handler_t<F>;
362 if (handler::not_empty_function(callable)) {
363 handler::init_func(func_, _NEFORCE forward<F>(callable));
364 invoker_ = &handler::invoke;
365 manager_ = &handler::manage;
366 }
367 }
368
374 function& operator=(const function& other) {
375 function(other).swap(*this);
376 return *this;
377 }
378
384 function& operator=(function&& other) noexcept {
385 function(_NEFORCE move(other)).swap(*this);
386 return *this;
387 }
388
395 if (manager_) {
396 manager_(func_, func_, inner::FUNCTION_OPERATE::DESTROY_PTR);
397 manager_ = nullptr;
398 invoker_ = nullptr;
399 }
400 return *this;
401 }
402
409 template <typename F, enable_if_t<callable_t<F>::value, int> = 0>
410 function& operator=(F&& callable) noexcept(handler_t<F>::template nothrow_init<F>()) {
411 function(_NEFORCE forward<F>(callable)).swap(*this);
412 return *this;
413 }
414
421 template <typename F>
423 function(wrapper).swap(*this);
424 return *this;
425 }
426
431 void swap(function& other) noexcept {
432 _NEFORCE swap(func_, other.func_);
433 _NEFORCE swap(manager_, other.manager_);
434 _NEFORCE swap(invoker_, other.invoker_);
435 }
436
441 explicit operator bool() const noexcept { return !empty(); }
442
449 Res operator()(Args&&... args) const {
450 if (empty()) {
451 NEFORCE_THROW_EXCEPTION(memory_exception("functional pointing to null."));
452 }
453 return invoker_(func_, _NEFORCE forward<Args>(args)...);
454 }
455
460 NEFORCE_NODISCARD const std::type_info& target_type() const noexcept {
461 if (manager_) {
462 inner::storage_data result{};
463 manager_(result, func_, inner::FUNCTION_OPERATE::GET_TYPE_INFO);
464 if (const auto info = result.access<const std::type_info*>()) {
465 return *info;
466 }
467 }
468 return typeid(void);
469 }
470
476 template <typename F>
477 const F* target() const noexcept {
478 return __target_impl<F>();
479 }
480
486 template <typename F>
487 F* target() noexcept {
488 const F* f = const_cast<const function*>(this)->target<F>();
489 return const_cast<F*>(f);
490 }
491};
492
493#ifdef NEFORCE_STANDARD_17
495NEFORCE_BEGIN_INNER__
496
497template <typename>
498struct __function_guide_helper;
499
500template <typename Result, typename Class, typename... Args>
501struct __function_guide_helper<Result (Class::*)(Args...)> {
502 using type = Result(Args...);
503};
504
505NEFORCE_END_INNER__
507
508template <typename Res, typename... Args>
509function(Res (*)(Args...)) -> function<Res(Args...)>;
510
511template <typename Func, typename Sign = typename inner::__function_guide_helper<
512 remove_function_qualifiers_t<decltype(&Func::operator())>>::type>
513function(Func) -> function<Sign>;
514
515#endif
516
517
526template <typename Res, typename... Args>
527bool operator==(const function<Res(Args...)>& f, nullptr_t np) noexcept {
528 return !static_cast<bool>(f);
529}
530
539template <typename Res, typename... Args>
540bool operator==(nullptr_t np, const function<Res(Args...)>& f) noexcept {
541 return !static_cast<bool>(f);
542}
543
552template <typename Res, typename... Args>
553bool operator!=(const function<Res(Args...)>& f, nullptr_t np) noexcept {
554 return static_cast<bool>(f);
555}
556
565template <typename Res, typename... Args>
566bool operator!=(nullptr_t np, const function<Res(Args...)>& f) noexcept {
567 return static_cast<bool>(f);
568}
569 // FunctionWrapper
571
572NEFORCE_END_NAMESPACE__
573#endif // NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
function & operator=(F &&callable) noexcept(handler_t< F >::template nothrow_init< F >())
从任意可调用对象赋值
void swap(function &other) noexcept
交换两个function对象
function & operator=(reference_wrapper< F > wrapper) noexcept
从引用包装器赋值
function(F &&callable) noexcept(handler_t< F >::template nothrow_init< F >())
从任意可调用对象构造
Res operator()(Args &&... args) const
函数调用运算符
function & operator=(nullptr_t np) noexcept
空指针赋值运算符
function & operator=(const function &other)
复制赋值运算符
function(nullptr_t np=nullptr) noexcept
默认构造函数
function & operator=(function &&other) noexcept
移动赋值运算符
const F * target() const noexcept
获取目标对象的常量指针
F * target() noexcept
获取目标对象的指针
function(const function &other)
复制构造函数
NEFORCE_NODISCARD const std::type_info & target_type() const noexcept
获取目标类型信息
function(function &&other) noexcept
移动构造函数
函数包装器主模板声明
引用包装器类模板
异常处理框架
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_ALWAYS_INLINE enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
unsigned char byte_t
字节类型,定义为无符号字符
decltype(nullptr) nullptr_t
空指针类型
bool operator!=(const function< Res(Args...)> &f, nullptr_t np) noexcept
不等于空指针比较
bool operator==(const function< Res(Args...)> &f, nullptr_t np) noexcept
等于空指针比较
NEFORCE_CONSTEXPR20 void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
NEFORCE_CONSTEXPR14 enable_if_t< is_invocable_r< Res, Callable, Args... >::value, Res > invoke_r(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
带返回类型检查的统一调用接口
NEFORCE_CONSTEXPR14 inner::__invoke_result_aux< Callable, Args... >::type invoke(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
统一调用接口
NEFORCE_INLINE17 constexpr bool is_invocable_r_v
is_invocable_r的便捷变量模板
typename remove_function_qualifiers< T >::type remove_function_qualifiers_t
remove_function_qualifiers的便捷别名
typename remove_cvref< T >::type remove_cvref_t
remove_cvref的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
typename decay< T >::type decay_t
decay的便捷别名
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr bool empty(const Container &cont) noexcept(noexcept(cont.empty()))
检查容器是否为空
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
NEFORCE_INLINE17 constexpr bool is_copy_constructible_v
is_copy_constructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_location_invariant_v
is_location_invariant的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_constructible_v
is_constructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_same_v
is_same的便捷变量模板
bool_constant< true > true_type
表示true的类型
bool_constant< false > false_type
表示false的类型
integral_constant< bool, Value > bool_constant
布尔常量包装器
NEFORCE_INLINE17 constexpr bool conjunction_v
conjunction的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
统一调用接口
内存操作异常