pstade

PrevUpHomeNext

Function Adaptors

ambiN
compose
curryN
uncurry
fuse
unfuse
indirect
lazy
memoize
mono
perfect
pipable
regular
ret
tagged

Function Adaptor is a higher-order function that takes a "base" Function Object and returns an "adapted" Function Object. Function Adaptor provides, if possible, two interfaces: A normal higher-order Function Object, and a Metafunction with the corresponding macro for static initialization. An adapted non-local Function Object with static storage duration is statically initialized if the base Function Object type is a POD type and an expression passed to the macro is a constant expression. Also, Function Adaptors don't modify Default Constructible and Assignable-ness of base Function Object unless otherwise specified.

[Note] Note

If a macro argument is not guaranteed to contain no commas and recursions, you must use PSTADE_EGG_XXX_L and PSTADE_EGG_XXX_R instead of PSTADE_EGG_XXX.

Description

ambi family helps you to makes an Ambi Function Object.

Header
  • <pstade/egg/ambi.hpp>
Model of
Notation
  • fc(d) is an object of result_of_ambi++N<_typeof(d)>::type initialized using d or an initializer.
Valid expressions

Valid expression

Semantics

result_of_ambi++N<_PFo>::type

An Ambi Major Function Object type.

PSTADE_EGG_AMBI_L init PSTADE_EGG_AMBI_R

A braced initializer of result_of_ambi++N<_PFo>::type

PSTADE_EGG_AMBI(I)

PSTADE_EGG_AMBI_L I PSTADE_EGG_AMBI_R

fc(d)(a1,...,aN)

_const(d)(_arg_list(a, by_perfect))

ambi++N(d)

fc(d)

Preconditions
  • 1 <= N && N <= PSTADE_EGG_MAX_ARITY.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding expression is a valid expression.
Invariants
  • result_of_ambi++N<_PFo>::type is a POD type if and only if _PFo is a POD type.
Example

struct base_my_plus
{
    typedef int result_type;

    int operator()(int x, int y) const
    {
        return x + y;
    }
};

typedef result_of_ambi2<base_my_plus>::type T_my_plus;
T_my_plus const my_plus = PSTADE_EGG_AMBI({});

void test_ambi()
{
    BOOST_CHECK( my_plus(1, 2) == 3 );
    BOOST_CHECK( ( 1|my_plus(2) ) == 3 );

    std::plus<int> std_plus;
    BOOST_CHECK( ambi2(std_plus)(1, 2) == 3);
    BOOST_CHECK( ( 1|ambi2(std_plus)(2) ) == 3);
}

See also
Description

compose is a Function Adaptor for function composition.

Header
  • <pstade/egg/compose.hpp>
Model of
Notation
  • fc(d1, d2, R0, _Stg) is an object of result_of_compose<_typeof(d1),_typeof(d2), R0, _Stg>::type initialized using d1 and d1 or these initializer.
Valid expressions

Valid expression

Semantics

result_of_compose<_Dco1, _Dco2, R0 = _default, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_COMPOSE_L init1 PSTADE_EGG_COMPOSE_M init2 PSTADE_EGG_COMPOSE_R

A braced initializer of result_of_compose<_Dco1, _Dco2, R0, _Stg>::type

PSTADE_EGG_COMPOSE(I1, I2)

PSTADE_EGG_COMPOSE_L I1 PSTADE_EGG_COMPOSE_M I2 PSTADE_EGG_COMPOSE_R

fc(d1, d2, R0, Stg)(a1,...,aN)

_const(d1)(_const(d2)(_arg_list(a, _Stg)))

X_compose<R0 = _default>

A Major Function Object type

X_compose<R0>()(d1, d2)

fc(d1, d2, R0, _default)

compose

X_compose<R0>()

Preconditions
  • 0 <= N && N <= _TUPLE_MAX_SIZE.
  • There is a d1_ and d2_ such that _Dco1 d1_ = init1; _Dco2 d2_ = init2; is a valid expression.
  • The corresponding expression is a valid expression.
  • _deduce_r0(R0, d1(d2())) is a valid expression which specifies the nullary return type.
[Note] Note

The last precondition means that fc(d1, d2, R0, _Stg) is NOT nullary-callable if R0 is _default.

Invariants
  • result_of_compose<_Dco1, _Dco2, R0, _Stg>::type is a POD type if and only if _Dco1 and _Dco2 is a POD type.
Example

...

See also
Description

curry family turns a base Function Object into curried one.

Header
  • <pstade/egg/curry.hpp>
Model of
Notation
  • fc(d) is an object of result_of_curry++N<_typeof(d)>::type initialized using d or an initializer.
  • b is a1,...,a(N-1).
Valid expressions

Valid expression

Semantics

result_of_curry++N<_PFo>::type

A Major Function Object type

PSTADE_EGG_CURRY++N++_L init PSTADE_EGG_CURRY++N++_R

A braced initializer of result_of_curry++N<_PFo>::type

PSTADE_EGG_CURRY++N(I)

PSTADE_EGG_CURRY++N++_L I PSTADE_EGG_CURRY++N++_R

fc(d)(a1)...(aN)

_const(d)(_arg_list(b, by_cref),_arg_list(aN, by_perfect))

curry++N(d)

fc(d)

Preconditions
  • N is the arity of _PFo such that 2 <= N && N <= PSTADE_EGG_MAX_ARITY.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding expression is a valid expression.
Invariants
  • result_of_curry++N<_PFo>::type is a POD type if and only if _PFo is a POD type.
[Note] Note

An element of b is copied unless it is an array.

Example

typedef result_of_curry2<T_my_plus>::type T_curried_plus;
T_curried_plus const curried_plus
    =
PSTADE_EGG_CURRY2_L PSTADE_EGG_AMBI({}) PSTADE_EGG_CURRY2_R 1
    ;

void test_curry()
{
    BOOST_CHECK( curried_plus(4)(9) == my_plus(4, 9) );
    BOOST_CHECK( curry2(my_plus)(4)(9) == 13 );
}

1

A macro invocation must be sandwiched using _L and _R.

See also
Description

uncurry reverses curry.

Header
  • <pstade/egg/uncurry.hpp>
Model of
Notation
  • fc(d, _Stg) is an object of result_of_uncurry<_typeof(d), _Stg>::type initialized using d or an initializer.
  • parens(b1,...,bN) is (b1)(b2),...,(bN).
Valid expressions

Valid expression

Semantics

result_of_uncurry<_PFo, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_UNCURRY_L init PSTADE_EGG_UNCURRY_R

A braced initializer of result_of_uncurry<_PFo>::type

PSTADE_EGG_UNCURRY(I)

PSTADE_EGG_UNCURRY_L I PSTADE_EGG_UNCURRY_R

fc(d, _Stg)(a1,...,aN)

_const(d)(parens(_arg_list(a, _Stg)))

uncurry(d)

fc(d, _default)

Preconditions
  • 2 <= N && N <= PSTADE_EGG_MAX_ARITY.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding expression is a valid expression.
Invariants
  • result_of_uncurry<_PFo>::type is a POD type if and only if _PFo is a POD type.
See also
Description

fuse converts the base Function Object into a unary Function Object which takes a tuple.

Header
  • <pstade/egg/fuse.hpp>
Model of
Notation
  • fc(d) is an object of result_of_fuse<_typeof(d)>::type initialized using d or an initializer.
  • t_ is _arg_list(t, by_perfect).
Valid expressions

Valid expression

Semantics

result_of_fuse<_PFo>::type

A Major Function Object type

PSTADE_EGG_FUSE_L init PSTADE_EGG_FUSE_R

A braced initializer of result_of_fuse<_PFo>::type

PSTADE_EGG_FUSE(I)

PSTADE_EGG_FUSE_L I PSTADE_EGG_FUSE_R

fc(d)(t)

_const(d)(boost::get<0>(t_),...,boost::get<N-1>(t_))

fc(d)(t0)

_const(d)()

fuse(d)

fc(d)

Preconditions
  • 1 <= N && N <= _TUPLE_MAX_SIZE.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • t0 is an empty tuple.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_fuse<_PFo>::type is a POD type if and only if _PFo is a POD type.
See also
Description

unfuse reverses fuse.

Header
  • <pstade/egg/unfuse.hpp>
Model of
Notation
  • fc(d, R0, _Stg) is an object of result_of_unfuse<_typeof(d), _default, R0, _Stg>::type initialized using d or an initializer.
  • ref(b1,...,bN) is b1&,...,bN&.
Valid expressions

Valid expression

Semantics

result_of_unfuse<_PFo, _default, R0 = _default, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_UNFUSE_L init PSTADE_EGG_UNFUSE_M PSTADE_EGG_UNFUSE_DEFAULT_PACK PSTADE_EGG_UNFUSE_R

A braced initializer of result_of_unfuse<_PFo, _default, R0, _Stg>::type

PSTADE_EGG_UNFUSE(I)

PSTADE_EGG_UNFUSE_L I PSTADE_EGG_UNFUSE_M PSTADE_EGG_UNFUSE_DEFAULT_PACK PSTADE_EGG_UNFUSE_R

fc(d, R0, _Stg)(a1,...,aN)

_const(d)( boost::tuple<ref(_meta_arg_list(a, by_ref))>(_arg_list(a, _Stg)) )

X_unfuse<R0 = _default>

A Major Function Object type

X_unfuse<R0>()(d)

fc(d, R0, _default)

unfuse

X_unfuse<>()

Preconditions
  • 0 <= N && N <= _TUPLE_MAX_SIZE.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
  • _deduce_r0(R0, _decltype(d( boost::tuple<>() )) is a valid expression which specifies the nullary return type.
Invariants
  • result_of_unfuse<_PFo>::type is a POD type if and only if _PFo is a POD type.
Example

...

See also
Description

indirect takes a pointer-like object then calls it after dereferencing.

Header
  • <pstade/egg/indirect.hpp>
Model of
Notation
  • fc(p, _Stg) is an object of result_of_indirect<_typeof(p), _Stg>::type initialized using p or an initializer.
Valid expressions

Valid expression

Semantics

result_of_indirect<Pco, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_INDIRECT_L init PSTADE_EGG_INDIRECT_R

A braced initializer of result_of_indirect<Pco, _Stg>::type

PSTADE_EGG_INDIRECT(P)

PSTADE_EGG_INDIRECT_L P PSTADE_EGG_INDIRECT_R

fc(p, _Stg)(a1,...,aN)

(*p)(_arg_list(a, _Stg))

indirect(p)

fc(p, _default)

Preconditions
  • boost::pointee<Pco>::type is Polymorphic Function Object type.
  • There is a p_ such that Pco p_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_indirect<Pco, _Stg>::type is a POD type if and only if Pco is a POD type.
Example

result_of_uncurry<
    result_of_indirect<T_curried_plus const *>::type
>::type const another_plus
    =
PSTADE_EGG_UNCURRY_L
    1PSTADE_EGG_INDIRECT(&curried_plus)
PSTADE_EGG_UNCURRY_R
    ;

1

&curried_plus is an address constant expression, so that another_plus can be statically initialized.

See also
Description

Boost.Phoenix is able to make a lambda expression without a "bind function". lazy turns a bindable Function Object into such one which can be used with Boost.Lambda.

Header
  • <pstade/egg/lazy.hpp>
Model of
Notation
  • fc(f) is an object of result_of_lazy<_typeof(f)>::type initialized using f or an initializer.
Valid expressions

Valid expression

Semantics

result_of_lazy<F>::type

A Major Function Object type

PSTADE_EGG_LAZY_L init PSTADE_EGG_LAZY_M PSTADE_EGG_LAZY_DEFAULT_BIND PSTADE_EGG_LAZY_R

A braced initializer of result_of_lazy<F>::type

PSTADE_EGG_LAZY(I)

PSTADE_EGG_LAZY_L I PSTADE_EGG_LAZY_M PSTADE_EGG_LAZY_DEFAULT_BIND PSTADE_EGG_LAZY_R

fc(f)(a1,...,aN)

Polymorphic boost::lambda::bind(f, _arg_list(a, by_cref))

lazy(f)

fc(f)

Preconditions
  • 0 <= N && N <= PSTADE_EGG_MAX_LINEAR_ARITY-1.
  • There is a f_ such that F f_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_lazy<F>::type is a POD type if and only if F is a POD type.
Example

result_of_lazy<base_my_plus>::type const my_Plus = PSTADE_EGG_LAZY({});

void test_lazy()
{
    namespace bll = boost::lambda;

    1BOOST_CHECK( my_Plus(bll::_1, 3)(bll::make_const(2)) == 2+3 );

    int two = 2, four = 4;
    BOOST_CHECK( my_Plus(my_Plus(bll::_1, 3), my_Plus(bll::_2, bll::_1))
        (two, four) == (2+3)+(4+2) );
}

1

Boost1.35 or later won't require make_const.

See also
Description

memoize stores the result of function for later reuse.

Header
  • <pstade/egg/memoize.hpp>
Model of
Notation
  • fixed is an unspecified unary Function Object which represents _pfo itself.
Valid expressions

Valid expression

Semantics

memoize(_pfo)

A Major Function Object

memoize(_pfo)(a1)

_const(_pfo)(fixed, _arg_list(_arg_list(a, by_cref), by_value))

[Note] Note

memoize can't offer the way to statically initialize a memoized Function Object.

Preconditions
Example

struct T_fib
{
    typedef int result_type;

    template<class Fixed>
    int operator()(Fixed f, int x) const
    {
        return x <= 1 ? 1 : f(x-1) + f(x-2);
    }

    int operator()(int x) const
    {
        return (*this)(*this, x);
    }
};

T_fib const fib = {};

void test_memoize()
{
    BOOST_CHECK( fib(30) == 1346269 );
    BOOST_CHECK( memoize(fib)(30) == 1346269 );
}

See also
Description

mono turns a base Function Object into "monomorphic" one which contains no templates.

Header
  • <pstade/egg/mono.hpp>
Model of
  • ...
Notation
  • fc(f) is an object of result_of_mono<_typeof(f), Sig>::type initialized using f or an initializer.
  • N is an arity of Sig.
  • R is a return type of Sig.
  • ParamOf(Sig, I) is an imaginary operator which returns an Ith parameter type of Sig.
  • arglist is b1,...,bN such that ParamOf(Sig, 0) b1 = a1; ... ParamOf(Sig, N-1) bN = aN;.
Valid expressions

Valid expression

Semantics

result_of_mono<Fun, Sig>::type

A Major Function Object type

PSTADE_EGG_MONO_L init PSTADE_EGG_MONO_R

A braced initializer of result_of_mono<Fun, Sig>::type

PSTADE_EGG_MONO(I)

PSTADE_EGG_MONO_L I PSTADE_EGG_MONO_R

fc(f)(a1,...,aN)

_const(f)(arglist)

X_mono<Sig>

A Major Function Object type

X_mono<Sig>()(f)

fc(f)

egg::mono<Sig>(f)

X_mono<Sig>()(f)

Preconditions
  • Fun is a Function Object type.
  • Sig is a function type.
  • There is a f_ such that Fun f_ = init; is a valid expression.
  • If R is _default, Fun is a Polymorphic Function Object type; R specifies a return type of Fun, otherwise.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_mono<Fun, Sig>::type is a POD type if and only if Fun is a POD type.
  • result_of_mono<Fun, Sig>::type is Adaptable if and only if N is 1 or 2.
Example

BOOST_CHECK( std::not1( egg::mono<bool(int)>(bll::_1 != 12) ) (12) );
BOOST_CHECK( std::bind1st( egg::mono<boost::use_default(int, int)>(bll::_1 == bll::_2), 12 ) (12) );

See also
Description

perfect performs the "perfect forwarding".

Header
  • <pstade/egg/perfect.hpp>
Model of
Notation
  • fc(d) is an object of result_of_perfect<_typeof(d)>::type initialized using d or an initializer.
Valid expressions

Valid expression

Semantics

result_of_perfect<_PFo>::type

A Major Function Object type

PSTADE_EGG_PERFECT_L init PSTADE_EGG_PERFECT_R

A braced initializer of result_of_perfect<_PFo>::type

PSTADE_EGG_PERFECT(I)

PSTADE_EGG_PERFECT_L I PSTADE_EGG_PERFECT_R

fc(d)(a1,...,aN)

_const(d)(_arg_list(a, by_perfect))

perfect(d)

fc(d)

Preconditions
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_perfect<_PFo>::type is a POD type if and only if _PFo is a POD type.
Example
BOOST_CHECK( perfect(boost::lambda::_1)(12) == 12 );
See also
  • ...
Description

pipable adapts a base Function Object into Pipable Function Object.

Header
  • <pstade/egg/pipable.hpp>
Model of
Notation
  • fc(d, _Stg) is an object of result_of_pipable<_typeof(d), _Stg>::type initialized using d or an initializer.
Valid expressions

Valid expression

Semantics

result_of_pipable<_PFo, _Stg = _default>::type

A Pipable Function Object type

PSTADE_EGG_PIPABLE_L init PSTADE_EGG_PIPABLE_R

A braced initializer of result_of_pipable<_PFo, _Stg>::type

PSTADE_EGG_PIPABLE(I)

PSTADE_EGG_PIPABLE_L I PSTADE_EGG_PIPABLE_R

a|fc(d, _Stg)(b1,...,bN)

_const(d)(_arg_list(a, by_perfect), _arg_list(b, _Stg))

pipable(d)

fc(d, _default)

Preconditions
  • 1 <= N && N <= _TUPLE_MAX_SIZE-1.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_pipable<_PFo, _Stg>::type is a POD type if and only if _PFo is a POD type.
Example

struct base_multiplies
{
    typedef int result_type;

    int operator()(int x) const
    {
        return x * x;
    }

    int operator()(int x, int y) const
    {
        return x * y;
    }

    int operator()(int x, int y, int z) const
    {
        return x * y * z;
    }
};

result_of_pipable<base_multiplies>::type const multiplies = PSTADE_EGG_PIPABLE({});

void test_pipable()
{
    BOOST_CHECK( ( 2|multiplies ) == 2 * 2 );
    BOOST_CHECK( ( 2|multiplies() ) == 2 * 2 );
    BOOST_CHECK( ( 2|multiplies(3)|multiplies(4) ) == 2 * 3 * 4 );
    BOOST_CHECK( ( 2|multiplies(3, 4) ) == 2 * 3 * 4 );

    // `|=` seems an "apply" operator.
    BOOST_CHECK( ( multiplies|=2 ) == 2 * 2 );
    BOOST_CHECK( ( multiplies()|=2 ) == 2 * 2 );
    BOOST_CHECK( ( multiplies|=multiplies|=2) == (2 * 2) * (2 * 2) );

    BOOST_CHECK( ( multiplies(3)|=2 ) == 2 * 3 );
    BOOST_CHECK( ( multiplies(3, 4)|= 2 ) == 2 * 3 * 4 );
}

See also
Description

A Boost.Lambda functor is neither Default Constructible nor Assignable. An iterator holding such a functor can't conform to even Input Iterator. regular converts it into comfortable one for iterators.

Header
  • <pstade/egg/regular.hpp>
Model of
Notation
  • ...
Valid expressions

Valid expression

Semantics

regular(f)

A Major Function Object which is Default Constructible and Assignable.

regular(f)(a1,...,aN)

_const(f)(_arg_list(a, by_perfect))

Preconditions
Example

...

See also
Description

ret is akin to boost::lambda::ret in the context of Boost.ResultOf.

Header
  • <pstade/egg/ret.hpp>
Model of
  • ...
Notation
  • fc(f, R, _Stg) is an object of result_of_ret<_typeof(f), R, _Stg>::type initialized using f or an initializer.
Valid expressions

Valid expression

Semantics

result_of_ret<Fun, R = _default, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_RET_L init PSTADE_EGG_RET_R

A braced initializer of result_of_ret<Fun, R, _Stg>::type

PSTADE_EGG_RET(I)

PSTADE_EGG_RET_L I PSTADE_EGG_RET_R

fc(f, R, _Stg)(a1,...,aN)

_const(f)(_arg_list(a, _Stg))

X_ret<R>

A Major Function Object type

X_ret<R>()(f)

fc(f)(R, _default)

egg::ret<R>

X_ret<R>()

Preconditions
Invariants
  • result_of_ret<Fun, R, _Stg>::type is a POD type if and only if Fun is a POD type.
Example

...

See also
  • ...
Description

tagged makes a new Function Object type.

Header
  • <pstade/egg/tagged.hpp>
Model of
  • ...
Notation
  • fc(d, Tag, _Stg) is an object of result_of_tagged<_typeof(d), Tag, _Stg>::type initialized using d or an initializer.
Valid expressions

Valid expression

Semantics

result_of_tagged<_PFo, Tag, _Stg = _default>::type

A Major Function Object type

PSTADE_EGG_TAGGED_L init PSTADE_EGG_TAGGED_R

A braced initializer of result_of_tagged<_PFo, Tag, _Stg>::type

PSTADE_EGG_TAGGED(I)

PSTADE_EGG_TAGGED_L I PSTADE_EGG_TAGGED_R

fc(d, Tag, _Stg)(a1,...,aN)

_const(d)(_arg_list(a, _Stg))

tag_of<F>::type

Tag if F is _typeof(fc(d, Tag, _Stg)), unspecified type otherwise.

is_tagged_with<F, Tag>

boost::is_same<tag_of<F>::type, Tag>

Preconditions
  • Tag is any (possibly incomplete) type.
  • There is a d_ such that _PFo d_ = init; is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • result_of_tagged<_PFo, Tag, _Stg>::type is a POD type if and only if _PFo is a POD type.
  • result_of_tagged<_PFo, Tag1, _Stg>::type is the same type as result_of_tagged<_PFo, Tag2, _Stg>::type if and only if Tag1 is the same type as Tag2.
Example

...

See also
Copyright 2007 Shunsuke Sogame

PrevUpHomeNext