How would you implement this in D? (signals & slots)
Enjoys Math via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Feb 1 13:40:45 PST 2016
module signals_and_slots;
import std.algorithm: remove;
struct Slots(DelegateType, ArgTypes...) {
this() {
}
// How would you implement this?
void call(ArgTypes args) {
foreach (dg; delegates)
dg(args);
}
void connect(DelegateType slot) {
foreach (dg; delegates) {
if (dg == slot)
return;
}
delegates ~= slot;
}
void disconnect(DelegateType slot) {
for (uint k=0; k < delegates.length; k++) {
if (delegates[k] == slot)
delegates = delegates.remove(k);
}
}
private:
DelegateType[] delegates;
}
=============================================================
How do you implement this template called like:
void onColorChange(in Color) {
// do something with color
}
auto slots = Slots!(void delegate(in Color), Color);
slots.connect(&onColorChange);
auto color = Color(0.0, 1.0, 1.0, 1.0);
slots.call(color);
?
Thank you!
More information about the Digitalmars-d-learn
mailing list