使用 boost::function 和 boost::bind 确定函子中的对象和方法
我想获取指向对象的指针以及函子将从使用 boost::function 和 boost::bind 构造的函子调用哪个方法的指示。 这将使我能够自动确定必须执行的函子堆的顺序。
以下(伪)代码(请参阅POINTER_OF和METHOD_OF)显示了我想要做的事情:
class myClassA
{
public:
DoIt(int i) { return i+i; }
};
class myClassB
{
public:
DoItToo(double d) { return d*d; }
};
typedef boost::function0<int> Functor;
myClassA classA;
myClassB classB;
Functor funcA = boost::bind( &myClassA::DoIt, &classA, 10 );
Functor funcB = boost::bind( &myClassB::DoItToo, &classB, 12.34 );
// Create a vector containing some functors and try to determine the objects
// they are called upon and the methods they invoke
std::vector<Functor> vec;
vec.push_back( funcA );
vec.push_back( funcB );
for (int i = 0; i < vec.size();i++)
{
if (POINTER_OF(vec[i]) == &classA)
{
// This functor acts on classA
if (METHOD_OF(vec[i]) == &myClassA::DoIt)
{
// This functor calls the 'DoIt' method.
}
else if (METHOD_OF(vec[i]) == &myClassB::DoItToo)
{
// This functor calls the 'DoItToo' method.
}
}
// etc...
}
提前致谢!
I'd like to obtain the pointer to the object and an indication of which method the functor will call from a functor constructed using boost::function and boost::bind.
This will allow me to automatically determine the order in a which bunch of functors must be executed.
The following (pseudo) code (see POINTER_OF & METHOD_OF) shows what I'm trying to do:
class myClassA
{
public:
DoIt(int i) { return i+i; }
};
class myClassB
{
public:
DoItToo(double d) { return d*d; }
};
typedef boost::function0<int> Functor;
myClassA classA;
myClassB classB;
Functor funcA = boost::bind( &myClassA::DoIt, &classA, 10 );
Functor funcB = boost::bind( &myClassB::DoItToo, &classB, 12.34 );
// Create a vector containing some functors and try to determine the objects
// they are called upon and the methods they invoke
std::vector<Functor> vec;
vec.push_back( funcA );
vec.push_back( funcB );
for (int i = 0; i < vec.size();i++)
{
if (POINTER_OF(vec[i]) == &classA)
{
// This functor acts on classA
if (METHOD_OF(vec[i]) == &myClassA::DoIt)
{
// This functor calls the 'DoIt' method.
}
else if (METHOD_OF(vec[i]) == &myClassB::DoItToo)
{
// This functor calls the 'DoItToo' method.
}
}
// etc...
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道以下内容并不是对您问题的严格回答。
不要这样做。
请改用多态性。 这是我在当前项目代码中看到的最奇怪的事情之一:如果函数指针指向“someFunction” - 执行一些额外的操作。
您可以使用装饰器设计模式添加额外的行为,而无需对类进行太多更改。 这将使用 Decorator::DoIt 扩展你的 myClassA::DoIt 。
http://en.wikipedia.org/wiki/Decorator_pattern
I know that the following is not a strict answer to your question but.
Don't do this.
Use polymorphism instead. It is one of the strangest things I saw in my current project code: if function pointer points to "someFunction" - do some extra acctions.
You can add extra behavior without changing your classes much with Decorator design pattern. That will extend your myClassA::DoIt with Decorator::DoIt.
http://en.wikipedia.org/wiki/Decorator_pattern
不,我认为您无法获得设置为 boost 绑定(即 METHOD_OF)的 boost 函数的目标。 根据 这篇文章 boost 邮件列表 这是因为bind 的返回类型没有正式指定。
编辑:链接到与此问题有些相关的早期问题:降级-boostfunction-to-a-plain-function-pointer
No, I don't think you can get the target of a boost function that is set to a boost bind (ie METHOD_OF). According to this post on the boost mailing list this is because the return type of bind is not formally specified.
Edit: Link to an earlier question that is some-what related to this question: demote-boostfunction-to-a-plain-function-pointer
boost::function
对象是相等的,所以你应该能够做类似我怀疑你正在寻找更通用的东西的事情。 深入研究
boost/function_equal.hpp
的实现细节(即boost::bind
的function_equal_impl
)可能会让您了解如何选择性地测试boost::bind
参数的子集。但我真的认为你最好使用基于多态性的解决方案,或者只是将函数对象与一些元数据聚合起来。
boost::function
objects are equality comparable, so you should be able to do stuff likeI suspect you're looking for something more general though. Digging into the implementation details of
boost/function_equal.hpp
(ieboost::bind
'sfunction_equal_impl
) might give you some idea how to selectively test a subset of theboost::bind
arguments.But I really think youd be better off with a polymorphism-based solution, or just aggregating the function objects with some metadata.