pstade

PrevUpHomeNext

Function Builders

function
function_facade
deferred
generator
automatic
make_function

Function builders build Major Function Object type, which can be used with boost::lambda::bind and boost::result_of.

Description

function is the "kernel" class template which builds a Little Function into Major Function Object type.

Header
  • <pstade/egg/function.hpp>
Synopsys
template<class Little, class Stg = _default>
struct function
{
    typedef Little little_type;
    typedef function function_type;

    Little lit; // exposition only
    Little little() const { return lit; }

    // unspecified
    // ...
};
Notation
  • f is an object of function<_Lit, _Stg>.
Valid expressions

Valid expression

Semantics

function<_Lit, _Stg>

A Major Function Object aggregate type

f(a1,...,aN)

f.little().call<_Lit::apply<_Lit const, _meta_arg_list(a, _Stg)>::type>(_arg_list(a, _Stg))

f()

f.little().call<_Lit::nullary_result_type>()

Preconditions
  • The corresponding semantics is a valid expression.
Invariants
Example

struct little_second_argument
{
    template<class Myself, class A1, class A2>
    struct apply
    {
        1typedef A2 &type;
    };

    2template<class Result, class A1, class A2>
    Result call(A1 &a1, A2 &a2) const
    {
        return a2;
    }
};

typedef function<little_second_argument, by_perfect> T_second_argument;
T_second_argument const second_argument = {{}}; 3

void test_function()
{
    int i = 2;
    BOOST_CHECK( &(second_argument(1, i)) == &i );
    BOOST_CHECK( second_argument(1, i) == 2 );
}

1

A2 is possibly cv-qualifed but not a reference type.

2

Result is A2 &.

3

A braced initialization is ok, because T_second_argument is POD.

See also
Description

function_facade creates a new Major Function Object type using "CRTP". Though a type built from function_facade can't be a POD type, it can have non-default constructors.

Header
  • <pstade/egg/function_facade.hpp>
Notation
  • ...
Valid expressions

Valid expression

Semantics

_lit

A Major Function Object

_lit(a1,...,aN)

_lit.call<_typeof(_lit)::apply<_typeof(_lit) const, _meta_arg_list(a, _Stg)>::type>(_arg_list(a, _Stg))

_lit()

_lit.call<R0>()

Preconditions
  • _lit is an object whose type is derived from function_facade<_typeof(_lit), R0 = _default, _Stg = _default>.
  • The corresponding semantics is a valid expression.
Example

template<class T>
struct plus_to
    : function_facade< plus_to<T> >
{
    template<class Myself, class A>
    struct apply
    {
        typedef T type;
    };

    template<class Result, class A>
    Result call(A &a) const
    {
        return m_x + a;
    }

    explicit plus_to(T x)
        : m_x(x)
    {}

private:
    T m_x;
};

1template<class T>
plus_to<T> make_plus_to(T x)
{
    return plus_to<T>(x);
}

void test_function_facade()
{
    BOOST_CHECK( make_plus_to(1)(3) == 4 );
}

1

plus_to<> is already a full-fledged function.

See also
Description

deferred is akin to boost::detail::functionN family. It is useful because operator() can use nested typedefs.

Header
  • <pstade/egg/deferred.hpp>
Notation
  • f is an object of deferred<Lam, _Stg>::type.
Valid expressions

Valid expression

Semantics

deferred<Lam, _Stg = _default>::type

A Major Function Object POD type

PSTADE_EGG_DEFERRED

A braced initializer of deferred<Lam, _Stg>::type

f(a1,...,aN)

boost::mpl::apply<Lam, _meta_arg_list(a, _Stg)>::type()(_arg_list(a, _Stg))

Preconditions
  • Lam is an MPL Lambda Expression which is neither reference nor cv-qualified.
  • 1 <= N && N <= BOOST_MPL_LIMIT_METAFUNCTION_ARITY, which has a default value 5.
  • boost::mpl::apply<Lam, _meta_arg_list(a, _Stg)>::type::result_type is a valid expression.
  • The corresponding semantics is a valid expression.
Invariants
  • Non-local object f with static storage duration is statically initialized if initialized using PSTADE_EGG_DEFERRED.
  • f is Default Constructible and Assignable.
[Caution] Caution

For some compiler bug, deferred randomly fails to work as a Polymorphic Function Object under msvc-8.0. Use PSTADE_EGG_DEFER and PSTADE_EGG_DEFER_BY if you need portability.

Example

template<class X>
struct base_my_identity
{
    typedef X &result_type;

    result_type operator()(X &x) const
    {
        return x;
    }
};

typedef
    deferred< base_my_identity<boost::mpl::_1> >::type
T_my_identity;

T_my_identity const my_identity = PSTADE_EGG_DEFERRED;

See also

See Object Generator section.

Description

automatic adds automatic conversion support to a cast form function.

Header
  • <pstade/egg/automatic.hpp>
Notation
  • u is an object of automatic<Lam, _Stg>::type.
  • f is an object of boost::mpl::apply<Lam, _typeof(to)>::type.
Valid expressions

Valid expression

Semantics

automatic<Lam, _Stg = _default>::type

A Major Function Object POD type

PSTADE_EGG_AUTOMATIC

A braced initializer of automatic<Lam, _Stg>::type

To to = u(a1,...,aN);

To to = boost::implicit_cast<_typeof(to)>( f(_arg_list(a, _Stg)) );

Preconditions
  • 0 <= N && N <= _TUPLE_MAX_SIZE.
  • Lam is an MPL Lambda Expression which is neither reference nor cv-qualified.
  • f is a Polymorphic Function Object.
  • There is a type X_ such that boost::is_convertible<X_, _typeof(to)>::value == false.
  • The corresponding semantics is a valid expression.
  • u is not placed in a default argument list.
[Note] Note

These valid expressions imply that the automatic deduction is available everywhere copy-initialization is invoked. For example, you can place u in return-statement. The last precondition comes from a bug of GCC.

Invariants
  • Non-local object u with static storage duration is statically initialized if initialized using PSTADE_EGG_AUTOMATIC.
  • u is Default Constructible and Assignable.
Example

template<class To>
struct X_lexical_cast
{
    typedef To result_type;

    template<class From>
    To operator()(From from) const
    {
        return boost::lexical_cast<To>(from);
    }
};

typedef
    automatic< X_lexical_cast<boost::mpl::_> >::type
T_lexicalize;

T_lexicalize const lexicalize = PSTADE_EGG_AUTOMATIC;

void test_automatic()
{
    std::string str = lexicalize(20);
    BOOST_CHECK( str == "20" );
}

See also
Description

make_function is the object generator of function<>. This is useful when you need a "self" Major Function Object in Little Functioncall.

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

Valid expression

Semantics

X_make_function<_Stg = _default>

A Major Function Object type

X_make_function<_Stg>()(_lit)

function<_typeof(_lit), _Stg> f = {_lit}; return f;

make_function

X_make_function<>()

Preconditions
  • The corresponding semantics is a valid expression.
Example

...

See also
Copyright © 2007 Shunsuke Sogame

PrevUpHomeNext