【问题标题】:Pass boost::signal as boost::function将 boost::signal 作为 boost::function 传递
【发布时间】:2010-07-21 05:10:27
【问题描述】:

我有一个类,其信号成员用 boost::function 封装。

是否可以使用此 API 添加另一个信号作为处理程序?

class Foo
{
public:
  VOID AddHandler(boost::function<VOID()> handler)
  {
     m_signal.connect(handler);
  }

private:
  boost::signal<VOID()> m_signal;
};

boost::signal<VOID()> signal;

VOID SignalCaller()
{
    signal();
}

int main( )
{ 
   Foo foo;
   //foo.AddHandler(signal); // I want to
   foo.AddHandler(&SignalCaller); // I have to
}

【问题讨论】:

    标签: c++ boost boost-signals


    【解决方案1】:

    使用在信号类型中声明的类型“slot_type”

    class Foo
    {
    public:
        typedef boost::signal0<void> Signal;
        typedef Signal::slot_type Slot;
    
        //allowed any handler type which is convertible to Slot
        void AddHandler(Slot handler)
        {
            m_signal.connect(handler);
        }
    private:
      Signal m_signal;
    };
    
    void f()
    {
        std::cout << "f() called";
    }
    
    //usage
        Foo foo;
        foo.AddHandler(signal);
        foo.AddHandler(&f);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多