[signals]: Making copies of a slot

AFICT, the rationale for copying a slot is missing, hence the following questions... boost::signal<void (int)> some_signal; Some_Slot slot1; some_signal.connect(slot1); Some_Slot slot2(slot1); some_signal(42); The above code will only signal slot1. If slot2 is a copy of slot1, should it also get the signal? I know it does not work that way, but I was wondering why that decision was made. Also, if I *do* want the copy to receive the same signals, how can I do so? Thanks!

Repost since a week has gone by without response (which seems like an eternity on this list ;-). On Thu, 16 Sep 2004 10:03:01 -0400 Jody Hagins <jody-boost-011304@atdesk.com> wrote:
AFICT, the rationale for copying a slot is missing, hence the following questions...
boost::signal<void (int)> some_signal; Some_Slot slot1; some_signal.connect(slot1); Some_Slot slot2(slot1); some_signal(42);
The above code will only signal slot1. If slot2 is a copy of slot1, should it also get the signal? I know it does not work that way, but I was wondering why that decision was made. Also, if I *do* want the copy to receive the same signals, how can I do so?
Thanks!

On Sep 23, 2004, at 5:54 AM, Jody Hagins 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.
AFICT, the rationale for copying a slot is missing, hence the following questions...
boost::signal<void (int)> some_signal; Some_Slot slot1; some_signal.connect(slot1); Some_Slot slot2(slot1); some_signal(42);
The above code will only signal slot1. If slot2 is a copy of slot1, should it also get the signal? I know it does not work that way, but I was wondering why that decision was made. Also, if I *do* want the copy to receive the same signals, how can I do so?
Just connect both slots to the signal? some_signal.connect(slot2); 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. Doug

On Thu, 23 Sep 2004 16:42:33 -0500 Doug Gregor <dgregor@cs.indiana.edu> 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; }
participants (2)
-
Doug Gregor
-
Jody Hagins