开发者

Templating boost::bind to automatically handle multiple arguments for member function

I have a class with an "Attach" function that accepts a function object and stores it into a collection. The class itself is templated on the function signature. Something like this:

template<class Signature>
class Event
{
public:

 void Attach(boost::function<Signature> signature)
 {
  MySignatures.push_back(signature);
 }

private:

 std::list<boost::function<Signature>> MySignatures;
};

To demonstrate usage, consider the following class:


class Listening
{
public:

 int SomeFunction(int x, int y, int z); 
};

To pass the function on Listening into Event, I would need to write:


 Event<int(int, int, int)> myEvent;
 Listening myListening;

 myEvent.Attach(boost::bind(boost::mem_fn(&Listening::SomeFunction), &myListening, _1, _2, _3));

So instead of doing so for each case which may be prone to error, I write a set of macros, as follows:


 #define EventArgument0(x, y)  boost::bind(boost::mem_fn(x), y)
 #define EventArgument1(x, y)  boost::bind(boost::mem_fn(x), y, _1)
 #define EventArgument2(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2)
 #define EventArgument3(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2, _3)
 #define EventArgument4(x, y)  boost::bind(boost::mem_fn(x), y, _1, _2, _3, _4)

 etc.

and then I can write:


 myEvent.Attach(EventArgument3(&Listening::SomeFunction, &myListening));

which is much easier to read (I think). Now to my question: how can I instead write:


 myEvent.Attach(EventArgument(&Listening::SomeFunction, &MyListening));

or even better:


 myEvent.Attach(&Lis开发者_开发问答tening::SomeFunction, &myListening);

, such that the event Attach will magically bind correctly with the appropriate number of arguments as contained in <Signature> (in this example, int(int, int, int))? I'm open to any template meta-programming magic you have in mind here.

Thanks.

Edit: it turns out I don't need boost::mem_fn here, because boost::bind is equivalent, so in my macro I can use:

bind(&MyClass::Hello, myClass, _1, _2, _3);

,instead of:

bind(mem_fn(&MyClass::Hello), myClass, _1, _2, _3);

The question remains however: how to pass &MyClass::Hello to the event class and use template overloading to handle the _1, _2, _3, etc. implied by the function prototype used to template the Event class?


Overload Attach for different numbers of parameters in the member function:

template<typename R,typename T,typename U>
void Attach(R (T::*pmf)(),U* p))
{
    Attach(boost::bind(pmf,p));
}

template<typename R,typename T,typename U,typename A1>
void Attach(R (T::*pmf)(A1),U* p))
{
    Attach(boost::bind(pmf,p,_1));
}

template<typename R,typename T,typename U,typename A1,typename A2>
void Attach(R (T::*pmf)(A1,A2),U* p))
{
    Attach(boost::bind(pmf,p,_1,_2));
}

If you need to handle const member functions too then you'll need a second set of overloads.


Making Attach() a template would allow you to do what you're aiming for. The code gets messy but it let's you call it the way you want.

template<typename A1>
void Attach(A1 a1);

template<typename A1, typename A2>
void Attach(A1 a1, A2 a2);

template<typename A1, typename A2, typename A3>
void Attach(A1 a1, A2 a2, A3 a3);

template<typename A1, typename A3, typename A4>
void Attach(A1 a1, A2 a2, A3 a3, A4 a4);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜