使用 boost::function 和 boost::bind 确定函子中的对象和方法

发布于 2024-07-15 19:15:16 字数 1162 浏览 13 评论 0原文

我想获取指向对象的指针以及函子将从使用 boost::function 和 boost::bind 构造的函子调用哪个方法的指示。 这将使我能够自动确定必须执行的函子堆的顺序。

以下(伪)代码(请参阅POINTER_OFMETHOD_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小嗲 2024-07-22 19:15:16

我知道以下内容并不是对您问题的严格回答。

不要这样做。
请改用多态性。 这是我在当前项目代码中看到的最奇怪的事情之一:如果函数指针指向“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

向日葵 2024-07-22 19:15:16

不,我认为您无法获得设置为 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

一萌ing 2024-07-22 19:15:16

boost::function 对象是相等的,所以你应该能够做类似

if (vec[i] == boost::bind( &myClassA::DoIt, &classA, 10 ))
{
   // ... this Functor calls DoIt on classA with argument 10
}

我怀疑你正在寻找更通用的东西的事情。 深入研究 boost/function_equal.hpp 的实现细节(即 boost::bindfunction_equal_impl)可能会让您了解如何选择性地测试 boost::bind 参数的子集。

但我真的认为你最好使用基于多态性的解决方案,或者只是将函数对象与一些元数据聚合起来。

boost::function objects are equality comparable, so you should be able to do stuff like

if (vec[i] == boost::bind( &myClassA::DoIt, &classA, 10 ))
{
   // ... this Functor calls DoIt on classA with argument 10
}

I suspect you're looking for something more general though. Digging into the implementation details of boost/function_equal.hpp (ie boost::bind's function_equal_impl) might give you some idea how to selectively test a subset of the boost::bind arguments.

But I really think youd be better off with a polymorphism-based solution, or just aggregating the function objects with some metadata.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文