I have two class variables that store a list of external slot functions depending on the state. These variables are grouped by map<[key type], boost::signals2::signal>. However I'm having trouble passing one slot function from one map to another map.
However the problem come from line 16-17. When I disconnect and/or erase the key from the original map, it seems to disconnect the newly connected signal as well. If I commented out both lines it works fine but I have extra keys in the key_update map. If I delete either of the lines it still doesn't work (ie. won't call the external function).
Current output:
1 2
print_update1 key = 1 update_id = a value= abc123
print_update2 key = 2 update_id = a value= abc123
If I comment out line 16-17:
1 2 3 4
print_update1 key = 1 update_id = a value= abc123
print_update2 key = 2 update_id = a value= abc123
print_update2 key = 2 update_id = a value= abc123
print_update2 key = 2 update_id = a value= abc123
If I comment out line 16 or line 17:
1 2
print_update1 key = 1 update_id = a value= abc123
print_update2 key = 2 update_id = a value= abc123
However I want to print 3 lines only, with key_update having only keys (1, 3) and update_id_update having only keys ("a")
Tried swap and it works if there is only one function per signal
Anyway, I tried storing the slot function upon initial connection (function init) in some map, and connect that to the other signal while erasing the original connected signal. That works finally.
Anyway, I think it make good sense that boost provide some functions for signal to pass its connected slots to another signal. This allows more flexible way to manage signals allocation.
boost provide some functions for signal to pass its connected slots to another signal.
But that doesn't happen. The signatur of key_update[from_key] fulfills the requirement to act as a slot hence connect can be applied. Not sure what happens when signal is actually erased.
Tried swap and it works if there is only one function per signalAre you sure?
You may test this in a parallel test project
I mean it will be great is boost allows something like
1 2 3 4 5 6 7 8 9
first_signal_map[id].connect([some external slot1]);
first_signal_map[id].connect([some external slot2]);
first_signal_map[id].connect([some external slot3]);
second_signal.connect([some external slot4]);
second_signal.take_over_slots(first_signal_map[id]); // wish list
first_signal_map[id].disconnect_all_slots();
first_signal_map.erase(id);
second_signal(); // still run the four external functions
Also in this example the "swap" function simply swap slots between the two signals, instead of passing them from one to another. Hence if I replace "take_over_slots" by "swap", slot4 will be lost.
The focus of the signal/slot mechanism is the connection.
When a signal dies everything is ok since the slot is not called anymore.
When a slot dies we have a problem since if it is called it's likely that the program crashes, therefore a slot (using the result of connect(...)) can disconnect itself. With scoped_connection it can be done automatically.
You cannot have two signals calling one slot since this would break the connnection mechanism. Hence you can swap signals but not assign one to another.
So signal is not designed to be used the way you do. What you rather want is a container (like vector or list) of functions. I'd suggest that you use boost function with a container instead.
Actually I wasn't trying to have two signals calling the same slot. Instead I'm trying to pass a slot from one signal to another, while the later signal may already have connected to other slots. Hence my "wish thinking" [take_over_slots] function, doing exactly that.
Instead I'm trying to pass a slot from one signal to another
You cannot do this directly because it breaks the connection concept. Move a slot is done like so:
1 2 3 4 5 6 7
signal sig1
signal sig2
connection conn = sig1.connect(func) //Now you have a connection from sig1 to func via conn
...
conn.disconnect()
conn = sig2.connect(func)
Since you don't want the connection scheme (or better: you don't use it) you better use boost functions: