Lumiera  0.pre.03
»edit your freedom«
function.hpp File Reference

Go to the source code of this file.

Description

Metaprogramming tools for transforming functor types.

Sometimes it is necessary to build and remould a function signature, e.g. for creating a functor or a closure based on an existing function of function pointer. This is a core task of functional programming, but sadly C++ in its current shape is still lacking in this area. (C++11 significantly improved this situation). As an pragmatic fix, we define here a collection of templates, specialising them in a very repetitive way for up to 9 function arguments. Doing so enables us to capture a function, access the return type and argument types as a typelist, eventually to manipulate them and re-build a different signature, or to create specifically tailored bindings.

If the following code makes you feel like vomiting, please look away, and rest assured: you aren't alone.

Todo:
get rid of the repetitive specialisations and use variadic templates to represent the arguments /////////////////////////////////TICKET #994
See also
control::CommandDef usage example
function-closure.hpp generic function application
typelist.hpp
tuple.hpp

Definition in file function.hpp.

#include "lib/meta/typelist.hpp"
#include "lib/meta/util.hpp"
#include <functional>

Classes

struct  _DetectSingleArgFunction< FUN >
 
struct  _Fun< FUN, SEL >
 Helper for uniform access to function signature types. More...
 
struct  _Fun< FUN(C::*)>
 allow also to probe plain member fields, which may hold a functor More...
 
struct  _Fun< FUN, enable_if< has_FunctionOperator< FUN > > >
 Specialisation for function objects and lambdas. More...
 
struct  _Fun< RET(ARGS...) noexcept >
 Specialisation to strip noexcept from the signature. More...
 
struct  _Fun< RET(ARGS...)>
 Specialisation for a bare function signature. More...
 
struct  _Fun< RET(C::*)(ARGS...) const >
 Specialisation to handle member pointer to const function; indirectly this specialisation also handles lambdas, as redirected by the main template (via decltype) More...
 
struct  _Fun< RET(C::*)(ARGS...) noexcept >
 Specialisation to deal with member pointer to noexcept function. More...
 
struct  _Fun< RET(C::*)(ARGS...)>
 Specialisation to deal with member pointer to function. More...
 
struct  _Fun< SIG & >
 Specialisation when using a function reference. More...
 
struct  _Fun< SIG && >
 Specialisation for passing a rvalue reference. More...
 
struct  _Fun< SIG * >
 Specialisation for using a function pointer. More...
 
struct  _Fun< SIG const & >
 Specialisation to strip spurious const for type analysis. More...
 
struct  BuildFunType< RET, ARGS >
 Build function types from given Argument types. More...
 
struct  BuildFunType< RET, Types< A1 > >
 
struct  BuildFunType< RET, Types< A1, A2 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4, A5 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4, A5, A6 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4, A5, A6, A7 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4, A5, A6, A7, A8 > >
 
struct  BuildFunType< RET, Types< A1, A2, A3, A4, A5, A6, A7, A8, A9 > >
 
struct  BuildFunType< RET, Types<> >
 
struct  has_Sig< FUN, SIG, bool >
 Meta-function to check that some function like entity offers the expected signature. More...
 
struct  has_Sig< FUN, X, false >
 catch-all to prevent compilation failure for anything not function-like. More...
 
struct  InstancePlaceholder< TAR >
 Placeholder marker for a special argument position to be supplied later. More...
 

Typedefs

template<typename FUN >
using _FunArg = typename _DetectSingleArgFunction< FUN >::Arg
 abbreviation for referring to a function's single Argument type
 
template<typename FUN >
using _FunRet = typename _Fun< FUN >::Ret
 abbreviation for referring to a function's return type
 
template<typename FUN , uint a>
using has_Arity = std::bool_constant< _Fun< FUN >::ARITY==a >
 
template<typename FUN >
using is_BinaryFun = has_Arity< FUN, 2 >
 
template<typename FUN >
using is_NullaryFun = has_Arity< FUN, 0 >
 
template<typename FUN >
using is_UnaryFun = has_Arity< FUN, 1 >
 

Macros

#define ASSERT_MEMBER_FUNCTOR(_EXPR_, _SIG_)
 Macro for a compile-time check to verify some member is present and comprises something invokable with a specific signature. More...
 
#define ASSERT_VALID_SIGNATURE(_FUN_, _SIG_)
 Macro for a compile-time check to verify the given generic functors or lambdas expose some expected signature. More...
 

Functions

template<typename SIG , typename FUN >
constexpr auto isFunMember (FUN)
 Helper to pick up a member field for verification. More...
 
template<class W , class TUP >
constexpr auto lateBindInstance (W &instance, TUP &&invocation)
 Fix-up the arguments for a member-function invocation, allowing to inject the actual this instance into an existing argument sequence. More...
 
template<class W , class TAR >
constexpr TAR * maybeInject (W &instance, InstancePlaceholder< TAR >)
 
template<class W , typename X >
constexpr X maybeInject (W &, X &&x)
 (default case: fall-through)
 

Namespaces

 lib
 Implementation namespace for support and library code.
 

Macro Definition Documentation

◆ ASSERT_VALID_SIGNATURE

#define ASSERT_VALID_SIGNATURE (   _FUN_,
  _SIG_ 
)
Value:
"Function " STRINGIFY(_FUN_) " unsuitable, expected signature: " STRINGIFY(_SIG_));
Meta-function to check that some function like entity offers the expected signature.
Definition: function.hpp:232
#define STRINGIFY(TOKEN)
this macro wraps its parameter into a cstring literal
Definition: util.hpp:508

Macro for a compile-time check to verify the given generic functors or lambdas expose some expected signature.

Remarks
typically used when configuring a template with custom adapters.

Definition at line 247 of file function.hpp.

Referenced by Detector4StructuralChanges< PAR, LIS, assign >::~Detector4StructuralChanges().

◆ ASSERT_MEMBER_FUNCTOR

#define ASSERT_MEMBER_FUNCTOR (   _EXPR_,
  _SIG_ 
)
Value:
static_assert (lib::meta::isFunMember<_SIG_>(_EXPR_), \
"Member " STRINGIFY(_EXPR_) " unsuitable, expect function signature: " STRINGIFY(_SIG_));
#define STRINGIFY(TOKEN)
this macro wraps its parameter into a cstring literal
Definition: util.hpp:508

Macro for a compile-time check to verify some member is present and comprises something invokable with a specific signature.

Remarks
typically used with generic types or bindings

Definition at line 273 of file function.hpp.

Referenced by vault::gear::activity::_verify_usable_as_ExecutionContext(), steam::engine::_verify_usable_as_InvocationAdapter(), and Worker< vault::gear::Scheduler::Setup >::isDead().


Class Documentation

◆ lib::meta::anonymous_namespace{function.hpp}::_DetectSingleArgFunction

struct lib::meta::anonymous_namespace{function.hpp}::_DetectSingleArgFunction
Class Members
typedef typename _Fun< FUN >::Sig Sig
typedef typename _Fun< Sig >::Head Arg
+ Collaboration diagram for _DetectSingleArgFunction< FUN >:

Function Documentation

◆ isFunMember()

constexpr auto lib::meta::isFunMember ( FUN  )
inline

Helper to pick up a member field for verification.

Template Parameters
SIGsignature of the function like entity expected
FUNaddress- or member-pointer, e.g. &Class::member
Returns
suitably parametrised has_Sig instance (which is bool convertible)
Remarks
intended for use with generic types, when expecting a somehow invokable member, irrespective if a static function, member function or functor object

Definition at line 263 of file function.hpp.

References lib::meta::isFunMember().

Referenced by lib::meta::isFunMember().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lateBindInstance()

constexpr auto lib::meta::lateBindInstance ( W &  instance,
TUP &&  invocation 
)
inline

Fix-up the arguments for a member-function invocation, allowing to inject the actual this instance into an existing argument sequence.

Remarks
invocation of a member function requires to supply the object instance as first element in the argument list; sometimes this poses a design challenge, since the actual instance may not be known at the point where the other arguments are prepared. As a remedy, the position of the instance pointer can be marked with the InstancePlaceholder, allowing to splice in the actual pointer when known.

Definition at line 318 of file function.hpp.

References lib::meta::lateBindInstance().

Referenced by LateBindInstance_test::demonstrateUsage(), lib::meta::lateBindInstance(), and LateBindInstance_test::verify_cornerCases().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: