26#ifndef JAU_FUNCTIONAL_HPP_
27#define JAU_FUNCTIONAL_HPP_
78 template<
typename R,
typename... A>
113 template<
typename R,
typename... A>
139 template<
typename R,
typename C0,
typename C1,
typename... A>
143 R(C0::*member)(A...);
156 std::enable_if_t<std::is_base_of_v<C0, C1>,
bool> =
true) noexcept
158 base(_base), member(_member) { }
161 return (base->*member)(args...);
166 return this == prhs ||
168 base == prhs->base &&
169 member == prhs->member
182 template<
typename R,
typename... A>
190 function(_function) { }
193 return (*function)(args...);
198 return this == prhs ||
200 function == prhs->function
214 template<
typename R,
typename L,
typename...A>
221 constexpr size_t detail_size()
const noexcept {
return sizeof(function); }
230 return function(args...);
235 return this == prhs ||
237 detail_size() == prhs->detail_size() &&
239 0 == ::memcmp((
void*)&function, (
void*)&prhs->function,
sizeof(function))
253 template<
typename R,
typename I,
typename... A>
256 R(*function)(I&, A...);
263 function(_function), data(_data) { }
268 function(_function), data(
std::move(_data)) { }
271 return (*function)(data, args...);
276 return this == prhs ||
278 function == prhs->function &&
293 template<
typename R,
typename I,
typename... A>
296 R(*function)(I*, A...);
302 function(_function), data_ptr(_data_ptr) { }
305 return (*function)(data_ptr, args...);
310 return this == prhs ||
312 function == prhs->function &&
313 data_ptr == prhs->data_ptr
329 template<
typename R,
typename... A>
333 std::function<R(A...)> function;
335 constexpr size_t detail_size()
const noexcept {
return sizeof(id)+
sizeof(function); }
340 id(_id), function(_function) { }
344 id(_id), function() { }
347 return function(args...);
352 return this == prhs ||
355 detail_size() && prhs->detail_size()
365 return static_cast<uint32_t
>(rhs);
378 template<
typename Signature>
392 template<
typename R,
typename... A>
399 std::shared_ptr<target_type> target_func;
400 size_t target_func_size;
415 : target_func(
std::make_shared<
func::null_target_t<R, A...>>() ),
416 target_func_size( sizeof(
func::null_target_t<R, A...>))
440 explicit function(std::shared_ptr<target_type> _funcPtr,
size_t asize_) noexcept
441 : target_func( _funcPtr ),
442 target_func_size( asize_ )
454 : target_func(
std::make_shared<
func::free_target_t<R, A...>>(
func) ),
455 target_func_size( sizeof(
func::free_target_t<R, A...>))
469 std::enable_if_t<!std::is_same_v<L, std::shared_ptr<target_type>> &&
470 !std::is_pointer_v<L> &&
471 !std::is_same_v<L, R(A...)> &&
472 !std::is_same_v<L,
function<R(A...)>>
491 template<
typename C0,
typename C1>
493 : target_func(
std::make_shared<
func::member_target_t<R, C0,
C1, A...>>(base, mfunc) ),
494 target_func_size( sizeof(
func::member_target_t<R, C0,
C1, A...>))
516 : target_func(
std::make_shared<
func::capval_target_t<R, I, A...>>(data,
func) ),
517 target_func_size( sizeof(
func::capval_target_t<R, I, A...>))
539 : target_func(
std::make_shared<
func::capval_target_t<R, I, A...>>(
std::move(data),
func) ),
540 target_func_size( sizeof(
func::capval_target_t<R, I, A...>))
560 : target_func(
std::make_shared<
func::capref_target_t<R, I, A...>>(data_ptr,
func) ),
561 target_func_size( sizeof(
func::capref_target_t<R, I, A...>))
595 explicit constexpr operator bool() const noexcept {
return !
is_null(); }
603 constexpr size_t target_size() const noexcept {
return target_func_size; }
607 std::to_string( target_func_size ) +
" + shared_ptr " +
608 std::to_string(
sizeof( target_func ) ) +
" + extra " +
609 std::to_string(
sizeof( target_func_size ) ) +
" -> " +
610 std::to_string(
sizeof( *
this ) + target_func_size ) +
" ) ";
614 return (*target_func)(args...);
617 return (*target_func)(args...);
621 return target_func->operator==(*rhs.target_func);
643 template<
typename Rl,
typename... Al,
template <
typename...>
class Fl = function,
644 typename Rr,
typename... Ar,
template <
typename...>
class Fr = function,
645 std::enable_if_t< !std::is_same_v< Fl<Rl(Al...)>, Fr<Rr(Ar...)> >
647 bool operator==(
const function<Rl(Al...)>& lhs,
const function<Rr(Ar...)>& rhs)
noexcept
669 template<
typename Rl,
typename... Al,
template <
typename...>
class Fl =
function,
670 typename Rr,
typename... Ar,
template <
typename...>
class Fr =
function,
671 std::enable_if_t< std::is_same_v< Fl<Rl(Al...)>, Fr<Rr(Ar...)> >
674 {
return lhs.operator==( rhs ); }
690 template<
typename Rl,
typename... Al,
template <
typename...>
typename Fl =
function,
691 typename Rr,
typename... Ar,
template <
typename...>
typename Fr =
function>
693 {
return !( lhs == rhs ); }
710 template<
typename R,
typename C0,
typename C1,
typename... A>
711 inline jau::function<R(A...)>
731 template<
typename R,
typename C,
typename... A>
732 inline jau::function<R(A...)>
733 bind_member(C *base, R(C::*mfunc)(A...)) noexcept {
752 template<
typename C0,
typename C1,
typename... A>
753 inline jau::function<void(A...)>
772 template<
typename C,
typename... A>
773 inline jau::function<void(A...)>
774 bind_member(C *base,
void(C::*mfunc)(A...)) noexcept {
791 template<
typename R,
typename... A>
792 inline jau::function<R(A...)>
809 template<
typename... A>
810 inline jau::function<void(A...)>
832 template<
typename R,
typename L,
typename... A>
833 inline jau::function<R(A...)>
834 bind_lambda(L
func)
noexcept {
851 template<
typename L,
typename... A>
852 inline jau::function<void(A...)>
853 bind_lambda(L
func)
noexcept {
877 template<
typename R,
typename I,
typename... A>
878 inline jau::function<R(A...)>
901 template<
typename I,
typename... A>
902 inline jau::function<void(A...)>
926 template<
typename R,
typename I,
typename... A>
927 inline jau::function<R(A...)>
950 template<
typename I,
typename... A>
951 inline jau::function<void(A...)>
973 template<
typename R,
typename I,
typename... A>
974 inline jau::function<R(A...)>
995 template<
typename I,
typename... A>
996 inline jau::function<void(A...)>
1017 template<
typename R,
typename... A>
1018 inline jau::function<R(A...)>
1019 bind_std(uint64_t
id, std::function<R(A...)>
func)
noexcept {
1038 template<
typename... A>
1039 inline jau::function<void(A...)>
1040 bind_std(uint64_t
id, std::function<
void(A...)>
func)
noexcept {
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.
jau::type_info make_ctti(bool identity_instance=false) 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...
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.
target_type
func::target_type identifier for the target function delegate_t<R, A...> object, exposed by jau::func...
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