Skip to content

Commit

Permalink
Merge pull request #5812 from edi33416/signals_disconnectall
Browse files Browse the repository at this point in the history
Fix issue 2447 - add disconnectAll for std.signals
merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Oct 27, 2017
2 parents ee7c1c8 + f0d2655 commit b5e6365
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions std/signals.d
Expand Up @@ -221,6 +221,17 @@ mixin template Signal(T1...)
}
}

/***
* Disconnect all the slots.
*/
final void disconnectAll()
{
debug (signal) writefln("Signal.disconnectAll");
__dtor();
slots_idx = 0;
status = ST.idle;
}

/* **
* Special function called when o is destroyed.
* It causes any slots dependent on o to be removed from the list
Expand Down Expand Up @@ -706,3 +717,58 @@ version(none) // Disabled because of dmd @@@BUG5028@@@
assert( dot2.value == -22 );
}

@system unittest
{
import std.signals;

class Observer
{ // our slot
void watch(string msg, int value)
{
if (value != 0)
{
assert(msg == "setting new value");
assert(value == 1);
}
}
}

class Foo
{
int value() { return _value; }

int value(int v)
{
if (v != _value)
{
_value = v;
// call all the connected slots with the parameters
emit("setting new value", v);
}
return v;
}

// Mix in all the code we need to make Foo into a signal
mixin Signal!(string, int);

private :
int _value;
}

Foo a = new Foo;
Observer o = new Observer;
auto o2 = new Observer;

a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.connect(&o2.watch);
a.value = 1; // should call o.watch()
a.disconnectAll();
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.connect(&o2.watch);
a.value = 1; // should call o.watch()
destroy(o); // destroying o should automatically disconnect it
destroy(o2);
a.value = 7; // should not call o.watch()
}

0 comments on commit b5e6365

Please sign in to comment.