20 #ifndef _MINLIB_MN_FUNCTION_LIGHT_H_
21 #define _MINLIB_MN_FUNCTION_LIGHT_H_
28 #include <type_traits>
33 template <
class Sig,
size_t sz,
size_t algn>
36 template <
class R,
class... Args,
size_t sz,
size_t algn>
39 void (*mover)(
void *src,
void *dest);
41 void (*destroyer)(
void *);
43 R (*invoke)(
void const *t, Args &&...args);
45 template <
class T>
static vtable_t
const *
get() {
46 static const vtable_t table = {
47 [](
void *src,
void *dest) {
new (dest)
T(
move(*
static_cast<T *
>(src))); },
48 [](
void *t) {
static_cast<T *
>(t)->~
T(); },
49 [](
void const *t, Args &&...args) -> R {
50 return (*
static_cast<T const *
>(t))(forward<Args>(args)...);
61 : table(o.table), data(o.data) { }
65 if (table) table->mover(&o.data, &data);
68 template <
class F,
class dF = decay_t<F>, enable_if_t<!is_same<dF, small_task>{}> * =
nullptr,
69 enable_if_t<is_convertible<res_of_t<dF &(Args...)>, R>{}> * =
nullptr>
71 static_assert(
sizeof(dF) <= sz,
"object too large");
72 static_assert(
alignof(dF) <= algn,
"object too aligned");
73 new (&data) dF(forward<F>(f));
76 if (table) table->destroyer(&data);
92 explicit operator bool()
const {
97 return table->invoke(&data, mn::forward<Args>(args)...);
100 vtable_t
const *table =
nullptr;
101 aligned_storage_t<sz, algn>
data;
104 template <
class R,
class... Args,
size_t sz,
size_t algn>
106 return !
static_cast<bool>(__f);
110 template <
class R,
class... Args,
size_t sz,
size_t algn>
112 return !
static_cast<bool>(__f);
115 template <
class R,
class... Args,
size_t sz,
size_t algn>
117 return static_cast<bool>(__f);
120 template <
class R,
class... Args,
size_t sz,
size_t algn>
122 return static_cast<bool>(__f);
126 using function =
small_task<Sig,
sizeof(
void *) * 4,
alignof(
void *)>;
130 #include <functional>
small_task(const small_task &o)
Definition: mn_function.hpp:60
small_task()
Definition: mn_function.hpp:59
small_task(small_task &&o)
Definition: mn_function.hpp:63
return_type operator()(Args... args) const
Definition: mn_function.hpp:96
R return_type
Definition: mn_function.hpp:57
small_task(F &&f)
Definition: mn_function.hpp:70
small_task & operator=(small_task &&o)
Definition: mn_function.hpp:86
aligned_storage_t< sz, algn > data
Definition: mn_function.hpp:101
~small_task()
Definition: mn_function.hpp:75
small_task & operator=(const small_task &o)
Definition: mn_function.hpp:80
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
T * get(const basic_linked_ptr< T > &_pPtr)
Get the pointer object.
Definition: mn_linked_ptr.hpp:223
Definition: mn_allocator_typetraits.hpp:25
constexpr bool operator==(const byte l, const byte r) noexcept
Definition: mn_def.hpp:112
constexpr bool operator!=(const byte l, const byte r) noexcept
Definition: mn_def.hpp:116
decltype(nullptr) nullptr_t
Definition: mn_def.hpp:45
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
Definition: mn_function.hpp:34
static vtable_t const * get()
Definition: mn_function.hpp:45