Boost logo

Boost :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2007-08-21 14:35:58


AMDG

Sorry about responding to a digest. Gmane is down.

Alexander Nasonov wrote:

>
> How about this?
>
> int BOOST_LOCAL_FUNCTION( (v) ) (int i)
> {
> return v = i;
> } BOOST_LOCAL_FUNCTION_END
>
> <snip>

Nice way to handle the return type.

You would need a base class and dynamic allocation right?
Also, I need to be able to specify the name of the function somehow
and I am not sure how this can be accomplished with that interface.
Here's what I have right now:

template<class Tuple, class F>
struct local_function;

#define BOOST_LOCAL_FUNCTION_DEF(z, n, data)\
    template<class Tuple class R BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n,
class T)>\
    struct local_function<Tuple, R(BOOST_PP_ENUM_PARAMS_Z(z, n, T))> {\
        Tuple tuple;\
        R (*f)(BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, T));\
        local_function(const Tuple& t) : tuple(t) {}\
        R operator()(BOOST_PP_ENUM_SHIFTED_BINARY_PARAMS_Z(z, n, T, t)) {\
            return(f(tuple BOOST_PP_ENUM_SHIFTED_TRAILING_PARAMS_Z(z, n,
t)))\
        }\
    };

BOOST_PP_REPEAT(BOOST_PP_INC(BOOST_LOCAL_FUNCTION_MAX_ARITY),
BOOST_LOCAL_FUNCTION_DEF, ~)

So that

int BOOST_LOCAL_FUNCTION(name, (i)(j), (int x)(int y)) {
    //body here
} BOOST_LOCAL_FUNCTION_END

expands to

int result_tag;
typedef BOOST_TYPEOF(result_tag) result_type;
typedef BOOST_TYPEOF(i) type_0;
typedef BOOST_TYPEOF(j) type_1;
typedef boost::tuple<type_0, type_1> tuple_type;
typedef local_function<tuple_type, result_type(tuple_type, int x, int
y)> function_type;
function_type name(make_tuple(i, j));
{
    function_type* function_ptr = &name;
    struct body {
        result_type impl(tuple_type& args, int x, int y) {
            type_0& i = boost::get<0>(args);
            type_1 j = boost::get<1>(args);
            {
                //body here
            }
        }
    };
    function_ptr->f = &body::impl;
}

Note that i and j are passed by value rather than by reference. Otherwise,
it is not safe to have a copy of the function around after control
leaves the scope
it is defined in.

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk