Boost logo

Boost Users :

From: Tony Juricic (tonygeek_at_[hidden])
Date: 2005-02-07 01:02:17


Douglas Gregor wrote:
> This operation is not possible with Signals: you can't get at the
> individual slots once they're connected. Essentially, Signals was
> designed with the idea that comparing function objects for equality when
> connecting/disconnecting is a bad idea, because it breaks down when the
> function objects you build are non-trivial (because it becomes hard to
> duplicate the function object when calling disconnect()).
>
> Doug

Well, I managed to get the functionality that I want but it feels kind
of stupid-I probably shouldn't be using signals to solve this problem.

vector<Callback> callbacks;
boost::signals::connection myConnection;

void Signal::Connect(Callback f)
{
        if(myConnection.connected())
        {
        // walk callback vector and for each element
                
                if(element.functor.obj_ptr==f.functor.obj_ptr)
                   return ; // don't want to connect again
                

        }

        myConnection=mySignal.connect(f)
        // store callback f in a vector
}

In my particular case Callback is a wrapper around class member function
  and I am only concerned about not connecting the same object twice.

I create Callback in class method like:

Callback f;
f=boost::ref(*this);
Signal::Connect(f);

It really feels very kludgy, in particular because I wanted to connect
different types, having identical member function signature.

Doing it old-fashioned way I would have every connectable type derive
from the same base class that has callback as the member function with
desired signature.

Unfortunately, that is exactly what I was trying to avoid and what
prompted me to try use signals.

I guess I am overengineering the solution with improper tool.

Tony


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net