2010-07-21 55 views
3

我有一个类,信号成员封装了boost :: function。传递boost :: signal作为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 
} 

回答

7

使用类型“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); 
+0

THX利息内部声明,但它并没有为我工作。 – Eugene 2010-07-21 10:08:29

+0

啊,它有棘手的返回类型 – Alsk 2010-07-21 13:33:52

+0

的回答是纠正 – Alsk 2010-07-21 13:42:04