Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Click here to view the latest version of this page.
PrevUpHomeNext

Adapting Functions

BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY
BOOST_PHOENIX_ADAPT_FUNCTION
BOOST_PHOENIX_ADAPT_CALLABLE_NULLARY
BOOST_PHOENIX_ADAPT_CALLABLE

If you want to adapt already existing functions or function objects it will become a repetetive task. Therefor the following boilerplate macros are provided to help you adapt already exsiting functions, thus reducing the need to phoenix.modules.bind functions.

Description

BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY is a macro that can be used to generate all the necessary boilerplate to make an arbitrary nullary function a lazy function.

[Note] Note

These macros generate no global objects. The resulting lazy functions are real functions that create the lazy function expression object

Synopsis
BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(
    RETURN_TYPE
  , LAZY_FUNCTION
  , FUNCTION
)
Semantics

The above macro generates all necessary code to have a nullary lazy function LAZY_FUNCTION which calls the nullary FUNCTION that has the return type RETURN_TYPE

Header
#include <boost/phoenix/function/adapt_function.hpp>
Example
namespace demo
{
    int foo()
    {
        return 42;
    }
}

BOOST_PHOENIX_ADAPT_FUNCTION_NULLARY(int, foo, demo::foo)

int main()
{
    using boost::phoenix::placeholders::_1;

    assert((_1 + foo())(1) == 43);
}
Description

BOOST_PHOENIX_ADAPT_FUNCTION is a macro that can be used to generate all the necessary boilerplate to make an arbitrary function a lazy function.

Synopsis
BOOST_PHOENIX_ADAPT_FUNCTION(
    RETURN_TYPE
  , LAZY_FUNCTION
  , FUNCTION
  , FUNCTION_ARITY
)
Semantics

The above macro generates all necessary code to have a lazy function LAZY_FUNCTION which calls FUNCTION that has the return type RETURN_TYPE with FUNCTION_ARITY number of arguments.

Header
#include <boost/phoenix/function/adapt_function.hpp>
Example
namespace demo
{
    int plus(int a, int b)
    {
        return a + b;
    }

    template <typename T>
    T
    plus(T a, T b, T c)
    {
        return a + b + c;
    }
}

BOOST_PHOENIX_ADAPT_FUNCTION(int, plus, demo::plus, 2)

BOOST_PHOENIX_ADAPT_FUNCTION(
    typename remove_reference<A0>::type
  , plus
  , demo::plus
  , 3
)

int main()
{
    using boost::phoenix::arg_names::arg1;
    using boost::phoenix::arg_names::arg2;

    int a = 123;
    int b = 256;

    assert(plus(arg1, arg2)(a, b) == a+b);
    assert(plus(arg1, arg2, 3)(a, b) == a+b+3);
}
Description

BOOST_PHOENIX_ADAPT_CALLABLE_NULLARY is a macro that can be used to generate all the necessary boilerplate to make an arbitrary nullary function object a lazy function.

Synopsis
BOOST_PHOENIX_ADAPT_CALLABLE_NULLARY(
    LAZY_FUNCTION
  , CALLABLE
)
Semantics

The above macro generates all necessary code to create LAZY_FUNCTION which creates a lazy function object that represents a nullary call to CALLABLE. The return type is specified by CALLABLE conforming to the Boost.Result Of protocol.

Header
#include <boost/phoenix/function/adapt_callable.hpp>
Example
namespace demo
{
    struct foo
    {
        typedef int result_type;

        result_type operator()() const
        {
            return 42;
        }
    }
}

BOOST_PHOENIX_ADAPT_CALLABLE_NULLARY(foo, demo::foo)

int main()
{
    using boost::phoenix::placeholders::_1;

    assert((_1 + foo())(1) == 43);
}
Description

BOOST_PHOENIX_ADAPT_CALLABLE is a macro that can be used to generate all the necessary boilerplate to make an arbitrary function object a lazy function.

Synopsis
BOOST_PHOENIX_ADAPT_CALLABLE(
    LAZY_FUNCTION
  , FUNCTION_NAME
  , FUNCTION_ARITY
)
Semantics

The above macro generates all necessary code to create LAZY_FUNCTION which creates a lazy function object that represents a call to CALLABLE with FUNCTION_ARITY arguments. The return type is specified by CALLABLE conforming to the Boost.Result Of protocol.

Header
#include <boost/phoenix/function/adapt_callable.hpp>
Example
namespace demo
{
    struct plus
    {
        template <typename Sig>
        struct result;

        template <typename This, typename A0, typename A1>
        struct result<This(A0, A1)>
            : remove_reference<A0>
        {};

        template <typename This, typename A0, typename A1, typename A2>
        struct result<This(A0, A1, A2)>
            : remove_reference<A0>
        {};

        template <typename A0, typename A1>
        A0 operator()(A0 const & a0, A1 const & a1) const
        {
            return a0 + a1;
        }

        template <typename A0, typename A1, typename A2>
        A0 operator()(A0 const & a0, A1 const & a1, A2 const & a2) const
        {
            return a0 + a1 + a2;
        }
    };
}

BOOST_PHOENIX_ADAPT_CALLABLE(plus, demo::plus, 2)

BOOST_PHOENIX_ADAPT_CALLABLE(plus, demo::plus, 3)

int main()
{
    using boost::phoenix::arg_names::arg1;
    using boost::phoenix::arg_names::arg2;

    int a = 123;
    int b = 256;

    assert(plus(arg1, arg2)(a, b) == a+b);
    assert(plus(arg1, arg2, 3)(a, b) == a+b+3);
}

PrevUpHomeNext