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!

Reply via email to