26#ifndef JAU_FUNCTIONAL_HPP_
27#define JAU_FUNCTIONAL_HPP_
301 template<
typename R,
typename... A>
336 template<
typename R,
typename... A>
362 template<
typename R,
typename C0,
typename C1,
typename... A>
363 class member_target_t final :
public target_t<R, A...> {
366 R(C0::*member)(A...);
379 std::enable_if_t<std::is_base_of_v<C0, C1>,
bool> =
true) noexcept
381 base(_base), member(_member) { }
384 return (base->*member)(args...);
389 return this == prhs ||
391 base == prhs->base &&
392 member == prhs->member
405 template<
typename R,
typename... A>
406 class free_target_t final :
public target_t<R, A...> {
421 return this == prhs ||
437 template<
typename R,
typename L,
typename...A>
438 class lambda_target_t final :
public target_t<R, A...>
444 constexpr size_t detail_size() const noexcept {
return sizeof(
function); }
449 sig( jau::make_ctti<R, L, A...>() ),
function(function_)
458 return this == prhs ||
460 detail_size() == prhs->detail_size() &&
476 template<
typename R,
typename I,
typename... A>
477 class capval_target_t final :
public target_t<R, A...> {
486 function(_function), data(_data) { }
499 return this == prhs ||
516 template<
typename R,
typename I,
typename... A>
517 class capref_target_t final :
public target_t<R, A...> {
525 function(_function), data_ptr(_data_ptr) { }
528 return (*
function)(data_ptr, args...);
533 return this == prhs ||
536 data_ptr == prhs->data_ptr
552 template<
typename R,
typename... A>
553 class std_target_t :
public target_t<R, A...> {
558 constexpr size_t detail_size() const noexcept {
return sizeof(id)+
sizeof(
function); }
575 return this == prhs ||
578 detail_size() && prhs->detail_size()
588 return static_cast<uint32_t
>(rhs);
601 template<
typename Signature>
615 template<
typename R,
typename... A>
616 class function<R(A...)> final {
622 std::shared_ptr<target_type> target_func;
623 size_t target_func_size;
638 : target_func(
std::make_shared<func::null_target_t<R, A...>>() ),
639 target_func_size( sizeof(func::null_target_t<R, A...>))
663 explicit function(std::shared_ptr<target_type> _funcPtr,
size_t asize_) noexcept
664 : target_func( _funcPtr ),
665 target_func_size( asize_ )
677 : target_func(
std::make_shared<func::free_target_t<R, A...>>(func) ),
678 target_func_size( sizeof(func::free_target_t<R, A...>))
692 std::enable_if_t<!std::is_same_v<L, std::shared_ptr<target_type>> &&
693 !std::is_pointer_v<L> &&
694 !std::is_same_v<L, R(A...)> &&
695 !std::is_same_v<L,
function<R(A...)>>
714 template<
typename C0,
typename C1>
716 : target_func(
std::make_shared<func::member_target_t<R, C0,
C1, A...>>(base, mfunc) ),
717 target_func_size( sizeof(func::member_target_t<R, C0,
C1, A...>))
738 function(
const I& data, R(*func)(I&, A...)) noexcept
739 : target_func(
std::make_shared<func::capval_target_t<R, I, A...>>(data, func) ),
740 target_func_size( sizeof(func::capval_target_t<R, I, A...>))
762 : target_func(
std::make_shared<func::capval_target_t<R, I, A...>>(
std::move(data), func) ),
763 target_func_size( sizeof(func::capval_target_t<R, I, A...>))
783 : target_func(
std::make_shared<func::capref_target_t<R, I, A...>>(data_ptr, func) ),
784 target_func_size( sizeof(func::capref_target_t<R, I, A...>))
801 function(uint64_t
id, std::function<R(A...)> func) noexcept
818 explicit constexpr operator bool() const noexcept {
return !is_null(); }
826 constexpr size_t target_size() const noexcept {
return target_func_size; }
829 return "function<" +
to_string( type() ) +
", " + signature().demangled_name() +
">( sz target_data " +
837 return (*target_func)(args...);
840 return (*target_func)(args...);
844 return target_func->operator==(*rhs.target_func);
866 template<
typename Rl,
typename... Al,
template <
typename...>
class Fl = function,
867 typename Rr,
typename... Ar,
template <
typename...>
class Fr = function,
868 std::enable_if_t< !std::is_same_v< Fl<Rl(Al...)>, Fr<Rr(Ar...)> >
870 bool operator==(
const function<Rl(Al...)>& lhs,
const function<Rr(Ar...)>& rhs)
noexcept
892 template<
typename Rl,
typename... Al,
template <
typename...>
class Fl = function,
893 typename Rr,
typename... Ar,
template <
typename...>
class Fr = function,
894 std::enable_if_t< std::is_same_v< Fl<Rl(Al...)>, Fr<Rr(Ar...)> >
896 bool operator==(
const function<Rl(Al...)>& lhs,
const function<Rr(Ar...)>& rhs)
noexcept
897 {
return lhs.operator==( rhs ); }
913 template<
typename Rl,
typename... Al,
template <
typename...>
typename Fl = function,
914 typename Rr,
typename... Ar,
template <
typename...>
typename Fr = function>
915 bool operator!=(
const function<Rl(Al...)>& lhs,
const function<Rr(Ar...)>& rhs)
noexcept
916 {
return !( lhs == rhs ); }
933 template<
typename R,
typename C0,
typename C1,
typename... A>
936 return function<R(A...)>( std::make_shared<func::member_target_t<R, C0,
C1, A...>>(base, mfunc),
937 sizeof(func::member_target_t<R, C0, C1, A...>) );
954 template<
typename R,
typename C,
typename... A>
956 bind_member(C *base, R(C::*mfunc)(A...)) noexcept {
957 return function<R(A...)>( std::make_shared<func::member_target_t<R, C, C, A...>>(base, mfunc),
958 sizeof(func::member_target_t<R, C, C, A...>) );
975 template<
typename C0,
typename C1,
typename... A>
978 return function<void(A...)>( std::make_shared<func::member_target_t<void, C0,
C1, A...>>(base, mfunc),
979 sizeof(func::member_target_t<void, C0, C1, A...>) );
995 template<
typename C,
typename... A>
997 bind_member(C *base,
void(C::*mfunc)(A...)) noexcept {
998 return function<void(A...)>( std::make_shared<func::member_target_t<void, C, C, A...>>(base, mfunc),
999 sizeof(func::member_target_t<void, C, C, A...>) );
1014 template<
typename R,
typename... A>
1017 return function<R(A...)>( std::make_shared<func::free_target_t<R, A...>>(func),
1018 sizeof(func::free_target_t<R, A...>) );
1032 template<
typename... A>
1035 return function<void(A...)>( std::make_shared<func::free_target_t<void, A...>>(func),
1036 sizeof(func::free_target_t<void, A...>) );
1055 template<
typename R,
typename L,
typename... A>
1057 bind_lambda(L func)
noexcept {
1058 return function<R(A...)>( std::make_shared<func::lambda_target_t<R, L, A...>>(func),
1059 sizeof(func::lambda_target_t<R, L, A...>) );
1074 template<
typename L,
typename... A>
1076 bind_lambda(L func)
noexcept {
1077 return function<void(A...)>( std::make_shared<func::lambda_target_t<void, L, A...>>(func),
1078 sizeof(func::lambda_target_t<void, L, A...>) );
1100 template<
typename R,
typename I,
typename... A>
1102 bind_capval(
const I& data, R(*func)(I&, A...)) noexcept {
1103 return function<R(A...)>( std::make_shared<func::capval_target_t<R, I, A...>>(data, func),
1104 sizeof(func::capval_target_t<R, I, A...>) );
1124 template<
typename I,
typename... A>
1126 bind_capval(
const I& data,
void(*func)(I&, A...)) noexcept {
1127 return function<void(A...)>( std::make_shared<func::capval_target_t<void, I, A...>>(data, func),
1128 sizeof(func::capval_target_t<void, I, A...>) );
1149 template<
typename R,
typename I,
typename... A>
1151 bind_capval(I&& data, R(*func)(I&, A...)) noexcept {
1152 return function<R(A...)>( std::make_shared<func::capval_target_t<R, I, A...>>(std::move(data), func),
1153 sizeof(func::capval_target_t<R, I, A...>) );
1173 template<
typename I,
typename... A>
1175 bind_capval(I&& data,
void(*func)(I&, A...)) noexcept {
1176 return function<void(A...)>( std::make_shared<func::capval_target_t<void, I, A...>>(std::move(data), func),
1177 sizeof(func::capval_target_t<void, I, A...>) );
1196 template<
typename R,
typename I,
typename... A>
1198 bind_capref(I* data_ptr, R(*func)(I*, A...)) noexcept {
1199 return function<R(A...)>( std::make_shared<func::capref_target_t<R, I, A...>>(data_ptr, func),
1200 sizeof(func::capref_target_t<R, I, A...>) );
1218 template<
typename I,
typename... A>
1220 bind_capref(I* data_ptr,
void(*func)(I*, A...)) noexcept {
1221 return function<void(A...)>( std::make_shared<func::capref_target_t<void, I, A...>>(data_ptr, func),
1222 sizeof(func::capref_target_t<void, I, A...>) );
1240 template<
typename R,
typename... A>
1242 bind_std(uint64_t
id, std::function<R(A...)> func)
noexcept {
1243 return function<R(A...)>( std::make_shared<func::std_target_t<R, A...>>(id, func),
1244 sizeof(func::std_target_t<R, A...>) );
1261 template<
typename... A>
1263 bind_std(uint64_t
id, std::function<
void(A...)> func)
noexcept {
1264 return function<void(A...)>( std::make_shared<func::std_target_t<void, A...>>(id, func),
1265 sizeof(func::std_target_t<void, A...>) );
func::capref_target_t implementation for functions using a reference to a captured value,...
R operator()(A... args) override
bool operator==(const target_t< R, A... > &rhs) const noexcept override
capref_target_t(I *_data_ptr, R(*_function)(I *, A...)) noexcept
func::capval_target_t implementation for functions using a copy of a captured value,...
capval_target_t(const I &_data, R(*_function)(I &, A...)) noexcept
Utilizes copy-ctor from 'const I& _data'.
bool operator==(const target_t< R, A... > &rhs) const noexcept override
capval_target_t(I &&_data, R(*_function)(I &, A...)) noexcept
Utilizes move-ctor from moved 'I&& _data'.
R operator()(A... args) override
func::free_target_t implementation for free functions, identifiable as func::target_type::free via ja...
free_target_t(R(*_function)(A...)) noexcept
bool operator==(const target_t< R, A... > &rhs) const noexcept override
R operator()(A... args) override
func::lambda_target_t implementation for lambda closures, identifiable as func::target_type::lambda v...
R operator()(A... args) override
lambda_target_t(L function_) noexcept
bool operator==(const target_t< R, A... > &rhs) const noexcept override
func::member_target_t implementation for class member functions, identifiable as func::target_type::m...
R operator()(A... args) override
member_target_t(C1 *_base, R(C0::*_member)(A...), std::enable_if_t< std::is_base_of_v< C0, C1 >, bool >=true) noexcept
Construct a target_t<R, A...> instance from given this base-pointer and member-function.
bool operator==(const target_t< R, A... > &rhs) const noexcept override
func::null_target_t implementation for no function.
R operator()(A...) override
bool operator==(const target_t< R, A... > &rhs) const noexcept override
func::std_target_t implementation for std::function instances, identifiable as func::target_type::std...
R operator()(A... args) override
std_target_t(uint64_t _id, std::function< R(A...)> _function) noexcept
bool operator==(const target_t< R, A... > &rhs) const noexcept override
std_target_t(uint64_t _id) noexcept
func::target_t pure-virtual interface for jau::function.
target_t & operator=(const target_t &o) noexcept=default
target_t(const target_t &o) noexcept=default
target_t(target_type ttype_) noexcept
virtual R operator()(A... args)=0
target_t(target_t &&o) noexcept=default
target_t & operator=(target_t &&o) noexcept=default
virtual ~target_t() noexcept
virtual bool operator==(const target_t< R, A... > &rhs) const noexcept=0
constexpr target_type type() const noexcept
Return the func::target_type of this invocation function wrapper.
func::target_t< R, A... > target_type
The target function type, i.e.
function(C1 *base, R(C0::*mfunc)(A...)) noexcept
Member function constructor.
function & operator=(function &&o) noexcept=default
constexpr bool operator!=(const function< R(A...)> &rhs) const noexcept
constexpr size_t target_size() const noexcept
Returns the size of the target type
constexpr func::target_type type() const noexcept
Return the jau::func::type of this instance.
function(L func) noexcept
Lambda function constructor.
std::string toString() const
constexpr bool operator==(const function< R(A...)> &rhs) const noexcept
function(uint64_t id, std::function< R(A...)> func) noexcept
std::function constructor
function(I &&data, R(*func)(I &, A...)) noexcept
Capture by value (move) function constructor.
function(const I &data, R(*func)(I &, A...)) noexcept
Capture by value (copy) function constructor.
function(R(*func)(A...)) noexcept
Free function constructor.
constexpr bool is_null() const noexcept
Returns true if this instance does not hold a callable target function, i.e.
function() noexcept
Null function constructor.
function(std::shared_ptr< target_type > _funcPtr, size_t asize_) noexcept
target_type constructor
R result_type
The target function return type R.
function & operator=(const function &o) noexcept=default
constexpr R operator()(A... args) const
function(function &&o) noexcept=default
jau::type_info signature() const noexcept
Returns signature of this function prototype R(A...) w/o underlying target function details.
function(const function &o) noexcept=default
function(I *data_ptr, R(*func)(I *, A...)) noexcept
Capture by reference function constructor.
function(std::nullptr_t) noexcept
Null function constructor.
constexpr R operator()(A... args)
Class template jau::function is a general-purpose static-polymorphic function wrapper.
Generic type information using either Runtime type information (RTTI) or Compile time type informatio...
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
std::string to_string(const alphabet &v) noexcept
jau::type_info make_ctti() noexcept
Constructs a jau::type_info instance based on given type T using template Compile Time Type Informati...
jau::function< R(A...)> bind_member(C1 *base, R(C0::*mfunc)(A...)) noexcept
Bind given class instance and non-void member function to an anonymous function using func_member_tar...
target_type
func::target_type identifier for the target function delegate_t<R, A...> object, exposed by jau::func...
jau::function< R(A...)> bind_free(R(*func)(A...)) noexcept
Bind given non-void free-function to an anonymous function using func::free_target_t.
jau::function< R(A...)> bind_std(uint64_t id, std::function< R(A...)> func) noexcept
Bind given non-void std::function to an anonymous function using func::std_target_t.
jau::function< R(A...)> bind_capval(const I &data, R(*func)(I &, A...)) noexcept
Bind given data by copying the captured value and the given non-void function to an anonymous functio...
jau::function< R(A...)> bind_capref(I *data_ptr, R(*func)(I *, A...)) noexcept
Bind given data by passing the captured reference (pointer) to the value and non-void function to an ...
constexpr uint32_t number(const func::target_type rhs) noexcept
@ null
Denotes a func::null_target_t.
@ lambda
Denotes a func::lambda_target_t.
@ capval
Denotes a func::capval_target_t.
@ capref
Denotes a func::capref_target_t.
@ member
Denotes a func::member_target_t.
@ free
Denotes a func::free_target_t.
@ std
Denotes a func::std_target_t.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
bool operator==(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
bool operator!=(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept