Boost logo

Boost :

From: Jody Hagins (jody-boost-011304_at_[hidden])
Date: 2004-09-24 06:59:38


On Thu, 23 Sep 2004 16:42:33 -0500
Doug Gregor <dgregor_at_[hidden]> wrote:

> > Repost since a week has gone by without response (which seems like
> > an eternity on this list ;-).
>
> Sorry, I've been (and am) on the road.

That's what you get for being so prompt otherwise ;->

> I'm not really sure how to answer the question. It seems _very_ odd to
>
> me to expect that both slot1 & slot2 be connected to some_signal
> through a single call to connect.

Right. Maybe a simple example would help describe my point. Also, I
think it should probably be noted specifically that copies do not
maintain connections. In addition, the docs should also show how to
implement a copy ctor so that a copy can make the same connections.
Currently, a slot does not really know about its connnections (unless it
is trackable) and even then it is not so obvious how a copy ctor can
access that information since the information is private (and a bit
difficult to navigate even if it were protected). The only way I have
been able to get it to work in this type scenario is to make the slot
object have an internal list of signals to which it is connected.
However, this means that I have do do other tricks to prevent that slot
from being connected to a signal in the normal way, since that would
allow an "unmanaged" connection.

#include "boost/signal.hpp"
#include <iostream>
#include <vector>
#include <algorithm>

struct Foo
  : public boost::signals::trackable
{
  void operator()(int x) const
  {
    std::cout << "Foo(" << x << ')' << std::endl;
  }
  void operator()(char const * s) const
  {
    std::cout << "Foo(" << s << ')' << std::endl;
  }
  // Other members, and possibly some data...
};

int
main(
    int,
    char *[])
{
  std::vector<Foo> foo;

  boost::signal<void (int)> sig1, sig2;

  // Here, we create an object, connect it to two signals, and then
  // place the object into a vector. The original gets the signal
  // but the copy that was inserted into the vector does not.
  {
    Foo f;
    sig1.connect(f);
    sig2.connect(f);
    foo.push_back(f);
    sig1(1);
    sig2(2);
  }

  // Or something a little more innocuous. Insert the object into the
  // vector first, and then make the connections. However, if the
  // vector ever grows (or is shuffled), the objects are no longer
  // connected to the signal.
  {
    Foo f;
    foo.push_back(f);
    sig1.connect(foo.back());
    sig2.connect(foo.back());
    sig1(11);
    sig2(12);
    std::random_shuffle(foo.begin(), foo.end());
    sig1(21);
    sig2(22);
  }
  sig1(31);
  sig2(32);

  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk