2015-04-18 01:35:36 +00:00
|
|
|
/* Function objects for OctaSTD.
|
|
|
|
*
|
|
|
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OCTA_FUNCTIONAL_H
|
|
|
|
#define OCTA_FUNCTIONAL_H
|
|
|
|
|
2015-04-22 17:42:58 +00:00
|
|
|
#include "octa/new.h"
|
2015-04-18 18:02:44 +00:00
|
|
|
#include "octa/memory.h"
|
2015-04-22 17:42:58 +00:00
|
|
|
#include "octa/utility.h"
|
2015-04-25 15:13:21 +00:00
|
|
|
#include "octa/type_traits.h"
|
2015-04-18 18:02:44 +00:00
|
|
|
|
2015-04-18 01:35:36 +00:00
|
|
|
namespace octa {
|
2015-04-24 17:16:35 +00:00
|
|
|
/* basic function objects */
|
|
|
|
|
|
|
|
#define __OCTA_DEFINE_BINARY_OP(name, op, rettype) \
|
2015-04-18 01:35:36 +00:00
|
|
|
template<typename T> struct name { \
|
2015-04-29 00:38:16 +00:00
|
|
|
bool operator()(const T &x, const T &y) const noexcept( \
|
|
|
|
noexcept(x op y) \
|
|
|
|
) { return x op y; } \
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef T first_argument_type; \
|
|
|
|
typedef T second_argument_type; \
|
|
|
|
typedef rettype result_type; \
|
2015-04-18 01:35:36 +00:00
|
|
|
};
|
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
__OCTA_DEFINE_BINARY_OP(Less, <, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(LessEqual, <=, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Greater, >, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(GreaterEqual, >=, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Equal, ==, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(NotEqual, !=, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(LogicalAnd, &&, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(LogicalOr, ||, bool)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Modulus, %, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Multiplies, *, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Divides, /, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Plus, +, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(Minus, -, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(BitAnd, &, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(BitOr, |, T)
|
|
|
|
__OCTA_DEFINE_BINARY_OP(BitXor, ^, T)
|
|
|
|
|
|
|
|
#undef __OCTA_DEFINE_BINARY_OP
|
2015-04-18 01:35:36 +00:00
|
|
|
|
|
|
|
template<typename T> struct LogicalNot {
|
2015-04-28 17:48:58 +00:00
|
|
|
bool operator()(const T &x) const noexcept(noexcept(!x)) { return !x; }
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef T argument_type;
|
|
|
|
typedef bool result_type;
|
2015-04-18 01:35:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct Negate {
|
2015-04-28 17:48:58 +00:00
|
|
|
bool operator()(const T &x) const noexcept(noexcept(-x)) { return -x; }
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef T argument_type;
|
|
|
|
typedef T result_type;
|
2015-04-18 01:35:36 +00:00
|
|
|
};
|
2015-04-18 01:50:20 +00:00
|
|
|
|
|
|
|
template<typename T> struct BinaryNegate {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef typename T::first_argument_type first_argument_type;
|
|
|
|
typedef typename T::second_argument_type second_argument_type;
|
|
|
|
typedef bool result_type;
|
|
|
|
|
2015-04-29 00:38:16 +00:00
|
|
|
explicit BinaryNegate(const T &f) noexcept(
|
|
|
|
IsNothrowCopyConstructible<T>::value
|
|
|
|
): p_fn(f) {}
|
2015-04-28 17:48:58 +00:00
|
|
|
|
2015-04-27 18:38:34 +00:00
|
|
|
bool operator()(const first_argument_type &x,
|
2015-04-29 00:38:16 +00:00
|
|
|
const second_argument_type &y) noexcept(
|
|
|
|
noexcept(p_fn(x, y))
|
|
|
|
) {
|
2015-04-18 01:50:20 +00:00
|
|
|
return !p_fn(x, y);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T p_fn;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct UnaryNegate {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef typename T::argument_type argument_type;
|
|
|
|
typedef bool result_type;
|
|
|
|
|
2015-04-29 00:38:16 +00:00
|
|
|
explicit UnaryNegate(const T &f) noexcept(
|
|
|
|
IsNothrowCopyConstructible<T>::value
|
|
|
|
): p_fn(f) {}
|
2015-04-28 01:30:17 +00:00
|
|
|
bool operator()(const argument_type &x) noexcept(noexcept(p_fn(x))) {
|
2015-04-18 01:50:20 +00:00
|
|
|
return !p_fn(x);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T p_fn;
|
|
|
|
};
|
2015-04-18 17:36:06 +00:00
|
|
|
|
2015-04-29 00:38:16 +00:00
|
|
|
template<typename T> UnaryNegate<T> not1(const T &fn) noexcept(
|
|
|
|
IsNothrowCopyConstructible<UnaryNegate<T>>::value
|
|
|
|
) {
|
2015-04-18 17:36:06 +00:00
|
|
|
return UnaryNegate<T>(fn);
|
|
|
|
}
|
|
|
|
|
2015-04-29 00:38:16 +00:00
|
|
|
template<typename T> BinaryNegate<T> not2(const T &fn) noexcept(
|
|
|
|
IsNothrowCopyConstructible<BinaryNegate<T>>::value
|
|
|
|
) {
|
2015-04-18 17:36:06 +00:00
|
|
|
return BinaryNegate<T>(fn);
|
|
|
|
}
|
2015-04-18 18:02:44 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
/* reference wrapper */
|
|
|
|
|
2015-04-18 18:02:44 +00:00
|
|
|
template<typename T>
|
|
|
|
struct ReferenceWrapper {
|
|
|
|
typedef T type;
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
ReferenceWrapper(T &v) noexcept: p_ptr(address_of(v)) {}
|
2015-04-18 18:02:44 +00:00
|
|
|
ReferenceWrapper(const ReferenceWrapper &) = default;
|
|
|
|
ReferenceWrapper(T &&) = delete;
|
|
|
|
|
|
|
|
ReferenceWrapper &operator=(const ReferenceWrapper &) = default;
|
|
|
|
|
|
|
|
operator T &() const { return *p_ptr; }
|
|
|
|
T &get() const { return *p_ptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *p_ptr;
|
|
|
|
};
|
|
|
|
|
2015-04-28 01:30:17 +00:00
|
|
|
template<typename T>
|
|
|
|
ReferenceWrapper<T> ref(T &v) noexcept {
|
2015-04-18 18:02:44 +00:00
|
|
|
return ReferenceWrapper<T>(v);
|
|
|
|
}
|
2015-04-28 01:30:17 +00:00
|
|
|
template<typename T>
|
|
|
|
ReferenceWrapper<T> ref(ReferenceWrapper<T> v) noexcept {
|
2015-04-18 18:02:44 +00:00
|
|
|
return ReferenceWrapper<T>(v);
|
|
|
|
}
|
|
|
|
template<typename T> void ref(const T &&) = delete;
|
|
|
|
|
2015-04-28 01:30:17 +00:00
|
|
|
template<typename T>
|
|
|
|
ReferenceWrapper<const T> cref(const T &v) noexcept {
|
2015-04-18 18:02:44 +00:00
|
|
|
return ReferenceWrapper<T>(v);
|
|
|
|
}
|
2015-04-28 01:30:17 +00:00
|
|
|
template<typename T>
|
|
|
|
ReferenceWrapper<const T> cref(ReferenceWrapper<T> v) noexcept {
|
2015-04-18 18:02:44 +00:00
|
|
|
return ReferenceWrapper<T>(v);
|
|
|
|
}
|
|
|
|
template<typename T> void cref(const T &&) = delete;
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
/* mem_fn */
|
2015-04-22 22:25:02 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename, typename> struct __OctaMemTypes;
|
|
|
|
template<typename T, typename R, typename ...A>
|
|
|
|
struct __OctaMemTypes<T, R(A...)> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef T argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
|
|
|
template<typename T, typename R, typename A>
|
|
|
|
struct __OctaMemTypes<T, R(A)> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef T first_argument_type;
|
|
|
|
typedef A second_argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
|
|
|
template<typename T, typename R, typename ...A>
|
|
|
|
struct __OctaMemTypes<T, R(A...) const> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef const T argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
|
|
|
template<typename T, typename R, typename A>
|
|
|
|
struct __OctaMemTypes<T, R(A) const> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef const T first_argument_type;
|
|
|
|
typedef A second_argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 22:25:02 +00:00
|
|
|
|
|
|
|
template<typename R, typename T>
|
2015-04-27 18:38:34 +00:00
|
|
|
class __OctaMemFn: __OctaMemTypes<T, R> {
|
2015-04-24 17:16:35 +00:00
|
|
|
R T::*p_ptr;
|
|
|
|
public:
|
|
|
|
__OctaMemFn(R T::*ptr): p_ptr(ptr) {}
|
|
|
|
template<typename... A>
|
|
|
|
auto operator()(T &obj, A &&...args) ->
|
|
|
|
decltype(((obj).*(p_ptr))(forward<A>(args)...)) {
|
|
|
|
return ((obj).*(p_ptr))(forward<A>(args)...);
|
|
|
|
}
|
|
|
|
template<typename... A>
|
|
|
|
auto operator()(const T &obj, A &&...args) ->
|
|
|
|
decltype(((obj).*(p_ptr))(forward<A>(args)...)) const {
|
|
|
|
return ((obj).*(p_ptr))(forward<A>(args)...);
|
|
|
|
}
|
|
|
|
template<typename... A>
|
|
|
|
auto operator()(T *obj, A &&...args) ->
|
|
|
|
decltype(((obj)->*(p_ptr))(forward<A>(args)...)) {
|
|
|
|
return ((obj)->*(p_ptr))(forward<A>(args)...);
|
|
|
|
}
|
|
|
|
template<typename... A>
|
|
|
|
auto operator()(const T *obj, A &&...args) ->
|
|
|
|
decltype(((obj)->*(p_ptr))(forward<A>(args)...)) const {
|
|
|
|
return ((obj)->*(p_ptr))(forward<A>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename R, typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
__OctaMemFn<R, T> mem_fn(R T:: *ptr) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
return __OctaMemFn<R, T>(ptr);
|
2015-04-22 22:25:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 17:42:58 +00:00
|
|
|
/* function impl
|
|
|
|
* reference: http://probablydance.com/2013/01/13/a-faster-implementation-of-stdfunction
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename> struct Function;
|
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
struct __OctaFunctorData {
|
|
|
|
void *p1, *p2;
|
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
|
|
|
struct __OctaFunctorInPlace {
|
|
|
|
static constexpr bool value = sizeof(T) <= sizeof(__OctaFunctorData)
|
|
|
|
&& (alignof(__OctaFunctorData) % alignof(T)) == 0
|
2015-04-24 18:43:20 +00:00
|
|
|
&& octa::IsNothrowMoveConstructible<T>();
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T, typename E = void>
|
|
|
|
struct __OctaFunctorDataManager {
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
static R call(const __OctaFunctorData &s, A ...args) {
|
|
|
|
return ((T &)s)(forward<A>(args)...);
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
static void store_f(__OctaFunctorData &s, T v) {
|
|
|
|
new (&get_ref(s)) T(forward<T>(v));
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static void move_f(__OctaFunctorData &lhs, __OctaFunctorData &&rhs) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
new (&get_ref(lhs)) T(move(get_ref(rhs)));
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static void destroy_f(__OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
get_ref(s).~T();
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static T &get_ref(const __OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
return (T &)s;
|
|
|
|
}
|
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
2015-04-24 18:43:20 +00:00
|
|
|
struct __OctaFunctorDataManager<T, EnableIf<!__OctaFunctorInPlace<T>::value>> {
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename R, typename ...A>
|
|
|
|
static R call(const __OctaFunctorData &s, A ...args) {
|
|
|
|
return (*(T *&)s)(forward<A>(args)...);
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
static void store_f(__OctaFunctorData &s, T v) {
|
|
|
|
new (&get_ptr_ref(s)) T *(new T(forward<T>(v)));
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static void move_f(__OctaFunctorData &lhs, __OctaFunctorData &&rhs) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
new (&get_ptr_ref(lhs)) T *(get_ptr_ref(rhs));
|
|
|
|
get_ptr_ref(rhs) = nullptr;
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static void destroy_f(__OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
T *&ptr = get_ptr_ref(s);
|
|
|
|
if (!ptr) return;
|
|
|
|
delete ptr;
|
|
|
|
ptr = nullptr;
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static T &get_ref(const __OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
return *get_ptr_ref(s);
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static T *&get_ptr_ref(__OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
return (T *&)s;
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
static T *&get_ptr_ref(const __OctaFunctorData &s) noexcept {
|
2015-04-24 17:16:35 +00:00
|
|
|
return (T *&)s;
|
|
|
|
}
|
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
struct __OctaFunctionManager;
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-27 19:23:38 +00:00
|
|
|
struct __OctaFmStorage {
|
2015-04-24 17:16:35 +00:00
|
|
|
__OctaFunctorData data;
|
|
|
|
const __OctaFunctionManager *manager;
|
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
|
|
|
static const __OctaFunctionManager &__octa_get_default_fm();
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
struct __OctaFunctionManager {
|
|
|
|
template<typename T>
|
|
|
|
inline static const __OctaFunctionManager create_default_manager() {
|
|
|
|
return __OctaFunctionManager {
|
|
|
|
&t_call_move_and_destroy<T>,
|
|
|
|
&t_call_copy<T>,
|
|
|
|
&t_call_destroy<T>
|
|
|
|
};
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-27 19:23:38 +00:00
|
|
|
void (* const call_move_and_destroy)(__OctaFmStorage &lhs,
|
|
|
|
__OctaFmStorage &&rhs);
|
|
|
|
void (* const call_copy)(__OctaFmStorage &lhs,
|
|
|
|
const __OctaFmStorage &rhs);
|
|
|
|
void (* const call_destroy)(__OctaFmStorage &s);
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
2015-04-27 19:23:38 +00:00
|
|
|
static void t_call_move_and_destroy(__OctaFmStorage &lhs,
|
|
|
|
__OctaFmStorage &&rhs) {
|
2015-04-24 17:16:35 +00:00
|
|
|
typedef __OctaFunctorDataManager<T> spec;
|
|
|
|
spec::move_f(lhs.data, move(rhs.data));
|
|
|
|
spec::destroy_f(rhs.data);
|
|
|
|
lhs.manager = &__octa_get_default_fm<T>();
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
2015-04-27 19:23:38 +00:00
|
|
|
static void t_call_copy(__OctaFmStorage &lhs,
|
|
|
|
const __OctaFmStorage &rhs) {
|
2015-04-24 17:16:35 +00:00
|
|
|
typedef __OctaFunctorDataManager<T> spec;
|
|
|
|
lhs.manager = &__octa_get_default_fm<T>();
|
|
|
|
spec::store_f(lhs.data, spec::get_ref(rhs.data));
|
|
|
|
}
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2015-04-27 19:23:38 +00:00
|
|
|
static void t_call_destroy(__OctaFmStorage &s) {
|
2015-04-24 17:16:35 +00:00
|
|
|
typedef __OctaFunctorDataManager<T> spec;
|
|
|
|
spec::destroy_f(s.data);
|
2015-04-22 17:42:58 +00:00
|
|
|
}
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename T>
|
|
|
|
inline static const __OctaFunctionManager &__octa_get_default_fm() {
|
|
|
|
static const __OctaFunctionManager def_manager
|
|
|
|
= __OctaFunctionManager::create_default_manager<T>();
|
|
|
|
return def_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename R, typename...>
|
|
|
|
struct __OctaFunction {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename R, typename T>
|
|
|
|
struct __OctaFunction<R, T> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef T argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-24 17:16:35 +00:00
|
|
|
template<typename R, typename T, typename U>
|
|
|
|
struct __OctaFunction<R, T, U> {
|
2015-04-27 18:38:34 +00:00
|
|
|
typedef R result_type;
|
|
|
|
typedef T first_argument_type;
|
|
|
|
typedef U second_argument_type;
|
2015-04-24 17:16:35 +00:00
|
|
|
};
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-05-03 15:09:47 +00:00
|
|
|
template<typename, typename>
|
|
|
|
struct IsValidFunctor {
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename R, typename ...A>
|
|
|
|
struct IsValidFunctor<Function<R(A...)>, R(A...)> {
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct __OctaEmpty {
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T func_to_functor(T &&f) {
|
|
|
|
return forward<T>(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RR, typename T, typename ...AA>
|
|
|
|
auto func_to_functor(RR (T::*f)(AA...)) -> decltype(mem_fn(f)) {
|
|
|
|
return mem_fn(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RR, typename T, typename ...AA>
|
|
|
|
auto func_to_functor(RR (T::*f)(AA...) const) -> decltype(mem_fn(f)) {
|
|
|
|
return mem_fn(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename R, typename ...A>
|
|
|
|
struct IsValidFunctor<T, R(A...)> {
|
|
|
|
template<typename U>
|
|
|
|
static decltype(func_to_functor(declval<U>())(declval<A>()...)) __octa_test(U *);
|
|
|
|
template<typename>
|
|
|
|
static __OctaEmpty __octa_test(...);
|
|
|
|
|
|
|
|
static constexpr bool value = IsConvertible<
|
|
|
|
decltype(__octa_test<T>(nullptr)), R
|
|
|
|
>::value;
|
|
|
|
};
|
|
|
|
|
2015-04-22 17:42:58 +00:00
|
|
|
template<typename R, typename ...A>
|
2015-04-24 17:16:35 +00:00
|
|
|
struct Function<R(A...)>: __OctaFunction<R, A...> {
|
2015-04-28 01:30:17 +00:00
|
|
|
Function( ) noexcept { initialize_empty(); }
|
|
|
|
Function(nullptr_t) noexcept { initialize_empty(); }
|
2015-04-22 17:42:58 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
Function(Function &&f) noexcept {
|
2015-04-22 17:42:58 +00:00
|
|
|
initialize_empty();
|
|
|
|
swap(f);
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
Function(const Function &f) noexcept: p_call(f.p_call) {
|
2015-04-22 17:42:58 +00:00
|
|
|
f.p_stor.manager->call_copy(p_stor, f.p_stor);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2015-05-03 15:09:47 +00:00
|
|
|
Function(T f, EnableIf<IsValidFunctor<T, R(A...)>::value, __OctaEmpty>
|
|
|
|
= __OctaEmpty())
|
|
|
|
noexcept(__OctaFunctorInPlace<T>::value) {
|
2015-04-22 17:42:58 +00:00
|
|
|
if (func_is_null(f)) {
|
|
|
|
initialize_empty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
initialize(func_to_functor(forward<T>(f)));
|
|
|
|
}
|
|
|
|
|
|
|
|
~Function() {
|
|
|
|
p_stor.manager->call_destroy(p_stor);
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
Function &operator=(Function &&f) noexcept {
|
2015-04-22 17:42:58 +00:00
|
|
|
p_stor.manager->call_destroy(p_stor);
|
|
|
|
swap(f);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
Function &operator=(const Function &f) noexcept {
|
2015-04-22 17:42:58 +00:00
|
|
|
p_stor.manager->call_destroy(p_stor);
|
|
|
|
f.p_stor.manager->call_copy(p_stor, f.p_stor);
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
|
|
|
|
R operator()(A ...args) const {
|
|
|
|
return p_call(p_stor.data, forward<A>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
2015-04-28 17:48:58 +00:00
|
|
|
void assign(F &&f) noexcept(__OctaFunctorInPlace<F>::value) {
|
2015-04-22 17:42:58 +00:00
|
|
|
Function(forward<F>(f)).swap(*this);
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:30:17 +00:00
|
|
|
void swap(Function &f) noexcept {
|
2015-04-27 19:23:38 +00:00
|
|
|
__OctaFmStorage tmp;
|
2015-04-22 17:42:58 +00:00
|
|
|
f.p_stor.manager->call_move_and_destroy(tmp, move(f.p_stor));
|
|
|
|
p_stor.manager->call_move_and_destroy(f.p_stor, move(p_stor));
|
|
|
|
tmp.manager->call_move_and_destroy(p_stor, move(tmp));
|
|
|
|
octa::swap(p_call, f.p_call);
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:30:17 +00:00
|
|
|
operator bool() const noexcept { return p_call != nullptr; }
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
private:
|
2015-04-27 19:23:38 +00:00
|
|
|
__OctaFmStorage p_stor;
|
2015-04-24 17:16:35 +00:00
|
|
|
R (*p_call)(const __OctaFunctorData &, A...);
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void initialize(T f) {
|
2015-04-24 17:16:35 +00:00
|
|
|
p_call = &__OctaFunctorDataManager<T>::template call<R, A...>;
|
|
|
|
p_stor.manager = &__octa_get_default_fm<T>();
|
|
|
|
__OctaFunctorDataManager<T>::store_f(p_stor.data, forward<T>(f));
|
2015-04-22 17:42:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
void initialize_empty() noexcept {
|
2015-04-22 17:42:58 +00:00
|
|
|
typedef R(*emptyf)(A...);
|
|
|
|
p_call = nullptr;
|
2015-04-24 17:16:35 +00:00
|
|
|
p_stor.manager = &__octa_get_default_fm<emptyf>();
|
|
|
|
__OctaFunctorDataManager<emptyf>::store_f(p_stor.data, nullptr);
|
2015-04-22 17:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static bool func_is_null(const T &) { return false; }
|
|
|
|
|
|
|
|
static bool func_is_null(R (* const &fptr)(A...)) {
|
|
|
|
return fptr == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RR, typename T, typename ...AA>
|
|
|
|
static bool func_is_null(RR (T::* const &fptr)(AA...)) {
|
|
|
|
return fptr == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename RR, typename T, typename ...AA>
|
|
|
|
static bool func_is_null(RR (T::* const &fptr)(AA...) const) {
|
|
|
|
return fptr == nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
void swap(Function<T> &a, Function<T> &b) noexcept {
|
2015-04-22 17:42:58 +00:00
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
bool operator==(nullptr_t, const Function<T> &rhs) noexcept { return !rhs; }
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
bool operator==(const Function<T> &lhs, nullptr_t) noexcept { return !lhs; }
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
bool operator!=(nullptr_t, const Function<T> &rhs) noexcept { return rhs; }
|
2015-04-22 17:42:58 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2015-04-28 01:30:17 +00:00
|
|
|
bool operator!=(const Function<T> &lhs, nullptr_t) noexcept { return lhs; }
|
2015-04-18 01:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|