21

请原谅我在问这个基本问题时的无知,但我已经习惯使用 Python,而这种事情是微不足道的,以至于我完全忘记了如何在 C++ 中尝试这个。

我希望能够将回调传递给在后台执行慢速进程的函数,并在进程完成后调用它。此回调可以是自由函数、静态函数或成员函数。我还希望能够在其中注入一些任意参数作为上下文。(即,在某种程度上实现了一个非常可怜的人的协程。)最重要的是,这个函数将始终采用 std::string,它是进程的输出。我不介意这个参数在最终回调参数列表中的位置是否固定。

我觉得答案将涉及 boost::bind 和 boost::function 但我无法计算出创建任意可调用对象所必需的精确调用(同时将它们咖喱化为仅采用单个字符串),将它们存储在后台进程中,并使用字符串参数正确调用可调用对象。

4

3 回答 3

18

回调应存储为boost::function<void, std::string>. 然后,您可以boost::bind通过绑定其他参数来使用将任何其他函数签名“转换”为这样的对象。

例子

我没有尝试编译这个,但无论如何它应该显示一般的想法

void DoLongOperation(boost::function<void, const std::string&> callback)
{
  std::string result = DoSomeLengthyStuff();
  callback(result);
}


void CompleteRoutine1(const std::string&);
void CompleteRoutine2(int param, const std::string&);

// Calling examples
DoLongOperation(&CompleteRoutine1); // Matches directly
DoLongOperation(boost::bind(&CompleteRoutine2, 7, _1)); // int parameter is bound to constant.

// This one is thanks to David Rodríguez comment below, but reformatted here:
struct S 
{ 
  void f( std::string const & );
};

int main() 
{ 
  S s;
  DoLongOperation( boost::bind( &S::f, &s, _1 ) ); 
}
于 2010-03-18T16:28:01.117 回答
1

听起来您想使用观察者模式

于 2010-03-18T16:27:55.923 回答
0

最简单的方法:

class Callback
{
public:
  virtual ~Callback() {}
  virtual Callback* clone() const = 0;

  // Better to wrap the call (logging, try/catch, etc)
  void execute(const std::string& result) { this->executeImpl(result); }

protected:
  // Don't make sense to have them public
  Callback() {}
  Callback(const Callback&) {}
  Callback& operator=(const Callback&) { return *this; }

private:
  virtual void executeImpl(const std::string& result) = 0;
};

// Example
class Example: public Callback
{
public:
  Example(int a, int b): Callback(), mA(a), mB(b) {}
  virtual Example* clone() const { return new Example(*this); }

private:
  virtual void executeImpl(const std::string& result) {}

  int mA;
  int mB;
};

然后,您可以将回调类(通过指针/引用)传递给进程。该类根据需要具有状态,并且可以在必要时复制(如果没有,则删除克隆)。

于 2010-03-18T16:43:19.633 回答