Boost logo

Boost :

From: Preston A. Elder (prez_at_[hidden])
Date: 2007-09-26 15:33:33


(repost, I accidentally posted this to boost::build list ... ugh).

I have written code to solve this problem:

I have class foo, like so:

class foo
{
    boost::function<void (foo *)> func_ptr;

    void call()
    {
        func_ptr(this);
    }
};

I want func_ptr to be either a free-standing function (or class static), OR
a class member function of a derived class, so:

void bar(foo *f) { ... }

OR:

class baz : public foo
{
public:
    baz()
    {
       func_ptr = &baz::doit;
    }

    void doit() { }
};

The obvious problem with the latter is that it needs to be downcast
implicitly, and some kind of exception thrown if the wrong type is passed.

I have created a boost::function proxy class to handle exactly this
problem. It uses a lot of boost::function's own tricks with the
pre-processor and such. I also had to use another suggestion I saw on
this list to work around the fact boost::function does not accept member
function pointer syntax (argh! That took a lot to figure out).

My solution is this:
http://www.neuromancy.net/fisheye/browse/mantra/trunk/Mantra-I/mantra/utils/mem_fn_proxy_template.h?r=403

Which is included by a boost preprocessor iteration here:
http://www.neuromancy.net/fisheye/browse/mantra/trunk/Mantra-I/mantra/utils/mem_fn_proxy.h?r=403

As I mention, I use a lot of boost::function's own tricks. I think this
would be a lot cleaner if done as part of boost::function, rather than me
re-implementing the same macros and such.

I have made sure my implementation works, and more importantly, has all
the tests you would expect (ie. that the return value arguments except the
class are the same, that the class is derived from the original function
typedef's first argument, and so on).

Here is my test case:
http://www.neuromancy.net/fisheye/browse/mantra/trunk/Mantra-I/test/scheduler/hello_world_task.cpp?r=403

Any way, feedback is as always appreciated, but if someone could look at
this for an addition to boost::function it would really be great

PreZ


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