1#ifndef NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
2#define NEFORCE_CORE_FUNCTIONAL_FUNCTION_HPP__
15NEFORCE_BEGIN_NAMESPACE__
29template <
typename Sign>
41enum class FUNCTION_OPERATE {
55class __undefined_util;
65 const void* const_object_;
66 void (*function_pointer_)();
67 void (__undefined_util::*member_pointer_)();
77 storage_data() noexcept = default;
79 NEFORCE_NODISCARD
void* access() noexcept {
return &data_[0]; }
80 NEFORCE_NODISCARD
const void* access() const noexcept {
return &data_[0]; }
83 NEFORCE_NODISCARD T& access() noexcept {
84 return *
static_cast<T*
>(access());
88 NEFORCE_NODISCARD
const T& access() const noexcept {
89 return *
static_cast<const T*
>(access());
92 __nocopy_type unused_;
93 byte_t data_[
sizeof(__nocopy_type)];
102class __function_base {
104 static constexpr size_t max_size_ =
sizeof(__nocopy_type);
105 static constexpr size_t max_align_ =
alignof(__nocopy_type);
114 template <
typename F>
115 class __manager_base {
117 static constexpr bool stored_ = is_location_invariant_v<F> &&
sizeof(F) <= max_size_ &&
118 alignof(F) <= max_align_ && max_align_ %
alignof(F) == 0;
121 template <
typename U,
bool = stored_>
122 struct __get_pointer_impl;
124 template <
typename U>
125 struct __get_pointer_impl<U, true> {
126 static U*
get(
const storage_data& src)
noexcept {
127 const U& f = src.access<U>();
128 return const_cast<U*
>(_NEFORCE
addressof(f));
132 template <
typename U>
133 struct __get_pointer_impl<U, false> {
134 static U*
get(
const storage_data& src)
noexcept {
return src.access<U*>(); }
138 using storage_ = bool_constant<stored_>;
140 static F* get_pointer(
const storage_data& src)
noexcept {
return __get_pointer_impl<F>::get(src); }
143 template <
typename Fn>
144 static void create(storage_data& data, Fn&& f, true_type ) {
145 ::new (
data.access()) F(_NEFORCE forward<Fn>(f));
147 template <typename Fn>
148 static
void create(storage_data& data, Fn&& f, false_type ) {
149 data.access<F*>() =
new F(_NEFORCE forward<Fn>(f));
152 static void destroy(storage_data& data, true_type ) {
data.access<F>().~F(); }
153 static void destroy(storage_data& data, false_type ) {
delete data.access<F*>(); }
156 static bool manage(storage_data& dest,
const storage_data& src,
const FUNCTION_OPERATE oper) {
158 case FUNCTION_OPERATE::GET_TYPE_INFO:
159 dest.access<
const std::type_info*>() = &
typeid(F);
161 case FUNCTION_OPERATE::GET_PTR:
162 dest.access<F*>() = __manager_base::get_pointer(src);
164 case FUNCTION_OPERATE::COPY_PTR:
165 __manager_base::init_func(dest, *
const_cast<const F*
>(__manager_base::get_pointer(src)));
167 case FUNCTION_OPERATE::DESTROY_PTR:
168 __manager_base::destroy(dest, storage_());
174 template <
typename Fn>
175 static void init_func(storage_data& func,
176 Fn&& f)
noexcept(conjunction_v<storage_, is_nothrow_constructible<F, Fn>>) {
177 __manager_base::create(func, _NEFORCE forward<Fn>(f), storage_());
180 template <
typename Sign>
181 static bool not_empty_function(
const _NEFORCE function<Sign>& f)
noexcept {
182 return static_cast<bool>(f);
184 template <
typename T>
185 static bool not_empty_function(T* fptr)
noexcept {
186 return fptr !=
nullptr;
188 template <
typename Class,
typename T>
189 static bool not_empty_function(T Class::* mptr)
noexcept {
190 return mptr !=
nullptr;
192 template <
typename T>
193 static bool not_empty_function(
const T& )
noexcept {
198 using manage_type = bool (*)(storage_data&,
const storage_data&, FUNCTION_OPERATE);
200 storage_data func_{};
201 manage_type manager_ =
nullptr;
204 __function_base() noexcept = default;
206 if (manager_ !=
nullptr) {
207 manager_(func_, func_, FUNCTION_OPERATE::DESTROY_PTR);
211 NEFORCE_NODISCARD
bool empty()
const {
return manager_ ==
nullptr; }
222template <
typename Sign,
typename F>
223class __function_manage_handler;
225template <
typename Res,
typename F,
typename... Args>
226class __function_manage_handler<Res(Args...), F> :
public __function_base::__manager_base<F> {
228 using base_type = __function_base::__manager_base<F>;
231 static bool manage(storage_data& dest,
const storage_data& src, FUNCTION_OPERATE oper) {
233 case FUNCTION_OPERATE::GET_TYPE_INFO:
234 dest.access<
const std::type_info*>() = &
typeid(F);
236 case FUNCTION_OPERATE::GET_PTR:
237 dest.access<F*>() = base_type::get_pointer(src);
240 base_type::manage(dest, src, oper);
245 static Res
invoke(
const storage_data& f, Args... args) {
246 return _NEFORCE invoke_r<Res>(*base_type::get_pointer(f), _NEFORCE forward<Args>(args)...);
249 template <
typename Fn>
250 static constexpr bool nothrow_init() noexcept {
251 return conjunction_v<typename base_type::storage_, is_nothrow_constructible<F, Fn>>;
256class __function_manage_handler<void, void> {
258 static bool manage(storage_data& ,
const storage_data& , FUNCTION_OPERATE ) {
263template <
typename Sign,
typename F,
bool Val
id = is_
object_v<F>>
264struct __function_handler_dispatch : __function_manage_handler<Sign, remove_cv_t<F>> {};
266template <
typename Sign,
typename F>
267struct __function_handler_dispatch<Sign, F, false> : __function_manage_handler<void, void> {};
279template <
typename Res,
typename... Args>
280class function<Res(Args...)> : inner::__function_base {
282 using invoker_type = Res (*)(
const inner::storage_data&, Args...);
285 invoker_type invoker_ =
nullptr;
288 template <
typename F,
bool IsSelf = is_same_v<remove_cvref_t<F>, function>>
291 template <
typename F,
typename =
void>
294 template <
typename F>
299 template <
typename F>
300 using handler_t = inner::__function_manage_handler<Res(Args...),
decay_t<F>>;
302 template <
typename F, enable_if_t<is_
object_v<F>,
int> = 0>
303 NEFORCE_ALWAYS_INLINE
const F* __target_impl()
const noexcept {
304 if (manager_ == &inner::__function_handler_dispatch<Res(Args...), F>::manage) {
305 inner::storage_data ptr{};
306 manager_(ptr, func_, inner::FUNCTION_OPERATE::GET_PTR);
307 return ptr.access<
const F*>();
312 template <
typename F, enable_if_t<!is_
object_v<F>,
int> = 0>
313 NEFORCE_ALWAYS_INLINE
const F* __target_impl()
const noexcept {
333 if (
static_cast<bool>(other)) {
334 other.manager_(func_, other.func_, inner::FUNCTION_OPERATE::COPY_PTR);
335 invoker_ = other.invoker_;
336 manager_ = other.manager_;
347 manager_ = other.manager_;
348 invoker_ = other.invoker_;
349 other.manager_ =
nullptr;
350 other.invoker_ =
nullptr;
358 template <typename F, enable_if_t<callable_t<F>::value,
int> = 0>
359 function(F&& callable)
noexcept(handler_t<F>::template nothrow_init<F>()) :
362 "target of function must be constructible");
364 using handler = handler_t<F>;
365 if (handler::not_empty_function(callable)) {
366 handler::init_func(func_, _NEFORCE
forward<F>(callable));
367 invoker_ = &handler::invoke;
368 manager_ = &handler::manage;
399 manager_(func_, func_, inner::FUNCTION_OPERATE::DESTROY_PTR);
412 template <typename F, enable_if_t<callable_t<F>::value,
int> = 0>
424 template <
typename F>
435 _NEFORCE
swap(func_, other.func_);
436 _NEFORCE
swap(manager_, other.manager_);
437 _NEFORCE
swap(invoker_, other.invoker_);
444 explicit operator bool() const noexcept {
return !
empty(); }
463 NEFORCE_NODISCARD
const std::type_info&
target_type() const noexcept {
465 inner::storage_data result{};
466 manager_(result, func_, inner::FUNCTION_OPERATE::GET_TYPE_INFO);
467 if (
const auto*
const info = result.access<
const std::type_info*>()) {
479 template <
typename F>
481 return __target_impl<F>();
489 template <
typename F>
492 return const_cast<F*
>(f);
496#ifdef NEFORCE_STANDARD_17
501struct __function_guide_helper;
503template <
typename Result,
typename Class,
typename... Args>
504struct __function_guide_helper<Result (Class::*)(Args...)> {
505 using type = Result(Args...);
511template <
typename Res,
typename... Args>
514template <
typename Func,
typename Sign =
typename inner::__function_guide_helper<
529template <
typename Res,
typename... Args>
531 return !
static_cast<bool>(f);
542template <
typename Res,
typename... Args>
544 return !
static_cast<bool>(f);
555template <
typename Res,
typename... Args>
557 return static_cast<bool>(f);
568template <
typename Res,
typename... Args>
570 return static_cast<bool>(f);
575NEFORCE_END_NAMESPACE__
function(function &&other) noexcept
移动构造函数
function & operator=(const function &other)
复制赋值运算符
function & operator=(function &&other) noexcept
移动赋值运算符
function(nullptr_t np=nullptr) noexcept
默认构造函数
function & operator=(reference_wrapper< F > wrapper) noexcept
从引用包装器赋值
const std::type_info & target_type() const noexcept
获取目标类型信息
function(F &&callable) noexcept(handler_t< F >::template nothrow_init< F >())
从任意可调用对象构造
Res operator()(Args... args) const
函数调用运算符
F * target() noexcept
获取目标对象的指针
function & operator=(nullptr_t np) noexcept
空指针赋值运算符
void swap(function &other) noexcept
交换两个function对象
function(const function &other)
复制构造函数
const F * target() const noexcept
获取目标对象的常量指针
function & operator=(F &&callable) noexcept(handler_t< F >::template nothrow_init< F >())
从任意可调用对象赋值
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
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
等于空指针比较
constexpr void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
constexpr inner::__invoke_result_aux< Callable, Args... >::type invoke(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
统一调用接口
constexpr bool is_invocable_r_v
is_invocable_r的便捷变量模板
typename remove_cvref< T >::type remove_cvref_t
remove_cvref的便捷别名
typename remove_function_qualifiers< T >::type remove_function_qualifiers_t
remove_function_qualifiers的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
typename decay< T >::type decay_t
decay的便捷别名
constexpr bool empty(const Container &cont) noexcept(noexcept(cont.empty()))
检查容器是否为空
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
constexpr bool is_constructible_v
is_constructible的便捷变量模板
constexpr bool is_copy_constructible_v
is_copy_constructible的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr bool is_same_v
is_same的便捷变量模板
bool_constant< false > false_type
表示false的类型
bool_constant< true > true_type
表示true的类型
function< float(float)> function
缓动函数类型:输入归一化时间 [0,1],输出进度 [0,1]