3

我有一个带有“附加”函数的类,它接受一个函数对象并将其存储到一个集合中。类本身是在函数签名上模板化的。像这样的东西:

template<class Signature>
class Event
{
public:

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

private:

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

为了演示用法,请考虑以下类:


class Listening
{
public:

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

要将函数传递给ListeningEvent我需要编写:


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

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

因此,我没有对每个可能容易出错的情况都这样做,而是编写了一组宏,如下所示:


 #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.

然后我可以写:


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

这更容易阅读(我认为)。现在我的问题是:我该如何写:


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

甚至更好:


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

, 这样事件 Attach 将神奇地正确绑定到 <Signature> 中包含的适当数量的参数(在本例中为int(int, int, int))?我对您在这里想到的任何模板元编程魔法持开放态度。

谢谢。

编辑:原来我在这里不需要boost::mem_fn,因为boost::bind是等价的,所以在我的宏中我可以使用:

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

,代替:

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

然而问题仍然存在:如何传递&MyClass::Hello给事件类并使用模板重载来处理用于模板类的函数原型所隐含的_1, _2,等?_3Event

4

2 回答 2

2

制作Attach()模板可以让你做你想做的事。代码变得杂乱无章,但它让你可以随意调用它。

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);
于 2010-08-19T14:32:26.097 回答
2

Attach成员函数中不同数量参数的重载:

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));
}

如果您还需要处理const成员函数,那么您将需要第二组重载。

于 2010-08-19T15:45:47.780 回答