How the signal should be named. The ref returning function will be named like this, the actual struct instance will have an underscore prefixed.
Specifies how the full functionality (emit) of the signal should be protected. Default is private. If Protection.none is given, private is used for the Signal member variable and the ref returning accessor method will return a Signal instead of a RestrictedSignal. The protection of the accessor method is specified by the surrounding protection scope:
public: // Everyone can access mysig now: // Result of mixin(signal!int("mysig", Protection.none)) ref Signal!int mysig() { return _mysig;} private Signal!int _mysig;
This mixin generator does not work with templated types right now because of:
10502
You might want to use the Signal struct directly in this
case. Ideally you write the code, the mixin would generate, manually
to ensure an easy upgrade path when the above bug gets fixed:
ref RestrictedSignal!(SomeTemplate!int) mysig() { return _mysig;} private Signal!(SomeTemplate!int) _mysig;
import std.stdio; class MyObject { // Mixin signal named valueChanged, with default "private" protection. // (Only MyObject is allowed to emit the signal) mixin(signal!(string, int)("valueChanged")); int value() @property { return _value; } int value(int v) @property { if (v != _value) { _value = v; // call all the connected slots with the two parameters _valueChanged.emit("setting new value", v); } return v; } private: int _value; } class Observer { // our slot void watch(string msg, int i) { writefln("Observed msg '%s' and value %s", msg, i); } } void watch(string msg, int i) { writefln("Globally observed msg '%s' and value %s", msg, i); } void main() { auto a = new MyObject; Observer o = new Observer; a.value = 3; // should not call o.watch() a.valueChanged.connect!"watch"(o); // o.watch is the slot a.value = 4; // should call o.watch() a.valueChanged.disconnect!"watch"(o); // o.watch is no longer a slot a.value = 5; // so should not call o.watch() a.valueChanged.connect!"watch"(o); // connect again // Do some fancy stuff: a.valueChanged.connect!Observer(o, (obj, msg, i) => obj.watch("Some other text I made up", i+1)); a.valueChanged.strongConnect(&watch); a.value = 6; // should call o.watch() destroy(o); // destroying o should automatically disconnect it a.value = 7; // should not call o.watch() }
which should print: <pre> Observed msg 'setting new value' and value 4 Observed msg 'setting new value' and value 6 Observed msg 'Some other text I made up' and value 7 Globally observed msg 'setting new value' and value 6 Globally observed msg 'setting new value' and value 7 </pre>
string mixin for creating a signal.
It creates a Signal instance named "_name", where name is given as first parameter with given protection and an accessor method with the current context protection named "name" returning either a ref RestrictedSignal or ref Signal depending on the given protection.