1#ifndef NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
2#define NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
14NEFORCE_BEGIN_NAMESPACE__
28template <
typename Sign>
40enum class FUNCTION_OPERATE {
54class __undefined_util;
64 const void* const_object_;
65 void (*function_pointer_)();
66 void (__undefined_util::*member_pointer_)();
76 storage_data() noexcept = default;
78 NEFORCE_NODISCARD
void* access() noexcept {
return &data_[0]; }
79 NEFORCE_NODISCARD
const void* access() const noexcept {
return &data_[0]; }
82 NEFORCE_NODISCARD T& access() noexcept {
83 return *
static_cast<T*
>(access());
87 NEFORCE_NODISCARD
const T& access() const noexcept {
88 return *
static_cast<const T*
>(access());
91 __nocopy_type unused_;
92 byte_t data_[
sizeof(__nocopy_type)];
101class __function_base {
103 static constexpr size_t max_size_ =
sizeof(__nocopy_type);
104 static constexpr size_t max_align_ =
alignof(__nocopy_type);
113 template <
typename F>
114 class __manager_base {
117 alignof(F) <= max_align_ && max_align_ %
alignof(F) == 0;
120 template <
typename U,
bool = stored_>
121 struct __get_pointer_impl;
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));
131 template <
typename U>
132 struct __get_pointer_impl<U, false> {
133 static U*
get(
const storage_data& src)
noexcept {
return src.access<U*>(); }
139 static F* get_pointer(
const storage_data& src)
noexcept {
return __get_pointer_impl<F>::get(src); }
142 template <
typename Fn>
146 template <typename Fn>
155 static bool manage(storage_data& dest,
const storage_data& src,
const FUNCTION_OPERATE oper) {
157 case FUNCTION_OPERATE::GET_TYPE_INFO:
158 dest.access<
const std::type_info*>() = &
typeid(F);
160 case FUNCTION_OPERATE::GET_PTR:
161 dest.access<F*>() = __manager_base::get_pointer(src);
163 case FUNCTION_OPERATE::COPY_PTR:
164 __manager_base::init_func(dest, *
const_cast<const F*
>(__manager_base::get_pointer(src)));
166 case FUNCTION_OPERATE::DESTROY_PTR:
167 __manager_base::destroy(dest, storage_());
173 template <
typename Fn>
174 static void init_func(storage_data& func,
176 __manager_base::create(func, _NEFORCE
forward<Fn>(f), storage_());
179 template <
typename Sign>
180 static bool not_empty_function(
const _NEFORCE function<Sign>& f)
noexcept {
181 return static_cast<bool>(f);
183 template <
typename T>
184 static bool not_empty_function(T* fptr)
noexcept {
185 return fptr !=
nullptr;
187 template <
typename Class,
typename T>
188 static bool not_empty_function(T Class::* mptr)
noexcept {
189 return mptr !=
nullptr;
191 template <
typename T>
192 static bool not_empty_function(
const T&)
noexcept {
197 using manage_type = bool (*)(storage_data&,
const storage_data&, FUNCTION_OPERATE);
199 storage_data func_{};
200 manage_type manager_ =
nullptr;
203 __function_base() noexcept = default;
206 manager_(func_, func_, FUNCTION_OPERATE::DESTROY_PTR);
210 NEFORCE_NODISCARD
bool empty()
const {
return !manager_; }
221template <
typename Sign,
typename F>
222class __function_manage_handler;
224template <
typename Res,
typename F,
typename... Args>
225class __function_manage_handler<Res(Args...), F> :
public __function_base::__manager_base<F> {
227 using base_type = __function_base::__manager_base<F>;
230 static bool manage(storage_data& dest,
const storage_data& src, FUNCTION_OPERATE oper) {
232 case FUNCTION_OPERATE::GET_TYPE_INFO:
233 dest.access<
const std::type_info*>() = &
typeid(F);
235 case FUNCTION_OPERATE::GET_PTR:
236 dest.access<F*>() = base_type::get_pointer(src);
239 base_type::manage(dest, src, oper);
244 static Res
invoke(
const storage_data& f, Args&&... args) {
248 template <
typename Fn>
249 static constexpr bool nothrow_init() noexcept {
255class __function_manage_handler<void, void> {
257 static bool manage(storage_data&,
const storage_data&, FUNCTION_OPERATE) {
return false; }
260template <
typename Sign,
typename F,
bool Val
id = is_
object_v<F>>
261struct __function_handler_dispatch : __function_manage_handler<Sign, remove_cv_t<F>> {};
263template <
typename Sign,
typename F>
264struct __function_handler_dispatch<Sign, F, false> : __function_manage_handler<void, void> {};
276template <
typename Res,
typename... Args>
277class function<Res(Args...)> : inner::__function_base {
279 using invoker_type = Res (*)(
const inner::storage_data&, Args&&...);
282 invoker_type invoker_ =
nullptr;
285 template <
typename F,
bool IsSelf = is_same_v<remove_cvref_t<F>, function>>
288 template <
typename F,
typename =
void>
291 template <
typename F>
296 template <
typename F>
297 using handler_t = inner::__function_manage_handler<Res(Args...),
decay_t<F>>;
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*>();
309 template <
typename F, enable_if_t<!is_
object_v<F>,
int> = 0>
310 NEFORCE_ALWAYS_INLINE
const F* __target_impl()
const noexcept {
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_;
344 manager_ = other.manager_;
345 invoker_ = other.invoker_;
346 other.manager_ =
nullptr;
347 other.invoker_ =
nullptr;
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>()) :
359 "target of function must be constructible");
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;
396 manager_(func_, func_, inner::FUNCTION_OPERATE::DESTROY_PTR);
409 template <typename F, enable_if_t<callable_t<F>::value,
int> = 0>
421 template <
typename F>
432 _NEFORCE
swap(func_, other.func_);
433 _NEFORCE
swap(manager_, other.manager_);
434 _NEFORCE
swap(invoker_, other.invoker_);
441 explicit operator bool() const noexcept {
return !
empty(); }
460 NEFORCE_NODISCARD
const std::type_info&
target_type() const noexcept {
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*>()) {
476 template <
typename F>
478 return __target_impl<F>();
486 template <
typename F>
489 return const_cast<F*
>(f);
493#ifdef NEFORCE_STANDARD_17
498struct __function_guide_helper;
500template <
typename Result,
typename Class,
typename... Args>
501struct __function_guide_helper<Result (Class::*)(Args...)> {
502 using type = Result(Args...);
508template <
typename Res,
typename... Args>
509function(Res (*)(Args...)) -> function<Res(Args...)>;
511template <
typename Func,
typename Sign =
typename inner::__function_guide_helper<
513function(Func) -> function<Sign>;
526template <
typename Res,
typename... Args>
528 return !
static_cast<bool>(f);
539template <
typename Res,
typename... Args>
541 return !
static_cast<bool>(f);
552template <
typename Res,
typename... Args>
554 return static_cast<bool>(f);
565template <
typename Res,
typename... Args>
567 return static_cast<bool>(f);
572NEFORCE_END_NAMESPACE__
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的便捷别名