views:

83

answers:

1

I have a class with signal member encapsulated with boost::function.

Is it possible to add another signal as a handler with this 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
}
+2  A: 

Edit:

use the type "slot_type" that is declared inside your signal type

void f()
{
    std::cout << "my_signal called";
}
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;
};
//usage
    Foo foo;
    foo.AddHandler(signal);
    foo.AddHandler(&f);

this doesn't work:

foo.AddHandler(&signal.operator())

Alsk
thx for interest, but it doesn't work for me.
Eugene
ah, it has tricky return type
Alsk
the answer is corrected
Alsk
thanks again, it works great!
Eugene