/usr/include/d2/4.6/std/signals.d is in libphobos2-4.6-dev 0.29.1-4.6.3-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | // Written in the D programming language.
/**
* Signals and Slots are an implementation of the Observer Pattern.
* Essentially, when a Signal is emitted, a list of connected Observers
* (called slots) are called.
*
* There have been several D implementations of Signals and Slots.
* This version makes use of several new features in D, which make
* using it simpler and less error prone. In particular, it is no
* longer necessary to instrument the slots.
*
* References:
* $(LUCKY A Deeper Look at Signals and Slots)$(BR)
* $(LINK2 http://en.wikipedia.org/wiki/Observer_pattern, Observer pattern)$(BR)
* $(LINK2 http://en.wikipedia.org/wiki/Signals_and_slots, Wikipedia)$(BR)
* $(LINK2 http://boost.org/doc/html/$(SIGNALS).html, Boost Signals)$(BR)
* $(LINK2 http://doc.trolltech.com/4.1/signalsandslots.html, Qt)$(BR)
*
* There has been a great deal of discussion in the D newsgroups
* over this, and several implementations:
*
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/announce/signal_slots_library_4825.html, signal slots library)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/Signals_and_Slots_in_D_42387.html, Signals and Slots in D)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/Dynamic_binding_--_Qt_s_Signals_and_Slots_vs_Objective-C_42260.html, Dynamic binding -- Qt's Signals and Slots vs Objective-C)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/Dissecting_the_SS_42377.html, Dissecting the SS)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/dwt/about_harmonia_454.html, about harmonia)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/announce/1502.html, Another event handling module)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/41825.html, Suggestion: signal/slot mechanism)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/13251.html, Signals and slots?)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/10714.html, Signals and slots ready for evaluation)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/digitalmars/D/1393.html, Signals & Slots for Walter)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/28456.html, Signal/Slot mechanism?)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/19470.html, Modern Features?)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/16592.html, Delegates vs interfaces)$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/16583.html, The importance of component programming (properties, signals and slots, etc))$(BR)
* $(LINK2 http://www.digitalmars.com/d/archives/16368.html, signals and slots)$(BR)
*
* Bugs:
* Slots can only be delegates formed from class objects or
* interfaces to class objects. If a delegate to something else
* is passed to connect(), such as a struct member function,
* a nested function or a COM interface, undefined behavior
* will result.
*
* Not safe for multiple threads operating on the same signals
* or slots.
* Macros:
* WIKI = Phobos/StdSignals
* SIGNALS=signals
*
* Copyright: Copyright Digital Mars 2000 - 2009.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: $(WEB digitalmars.com, Walter Bright)
* Source: $(PHOBOSSRC std/_signals.d)
*/
/* Copyright Digital Mars 2000 - 2009.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module std.signals;
import std.stdio;
import std.c.stdlib : calloc, realloc, free;
import core.exception : onOutOfMemoryError;
// Special function for internal use only.
// Use of this is where the slot had better be a delegate
// to an object or an interface that is part of an object.
extern (C) Object _d_toObject(void* p);
// Used in place of Object.notifyRegister and Object.notifyUnRegister.
alias void delegate(Object) DisposeEvt;
extern (C) void rt_attachDisposeEvent( Object obj, DisposeEvt evt );
extern (C) void rt_detachDisposeEvent( Object obj, DisposeEvt evt );
//debug=signal;
/************************
* Mixin to create a signal within a class object.
*
* Different signals can be added to a class by naming the mixins.
*
* Example:
---
import std.signals;
import std.stdio;
class Observer
{ // our slot
void watch(string msg, int i)
{
writefln("Observed msg '%s' and value %s", msg, i);
}
}
class Foo
{
int value() { return _value; }
int value(int v)
{
if (v != _value)
{ _value = v;
// call all the connected slots with the two 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;
}
void main()
{
Foo a = new Foo;
Observer o = new Observer;
a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.value = 4; // should call o.watch()
a.disconnect(&o.watch); // o.watch is no longer a slot
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.value = 6; // should call o.watch()
delete 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
* </pre>
*
*/
template Signal(T1...)
{
static import std.c.stdlib;
static import core.exception;
/***
* A slot is implemented as a delegate.
* The slot_t is the type of the delegate.
* The delegate must be to an instance of a class or an interface
* to a class instance.
* Delegates to struct instances or nested functions must not be
* used as slots.
*/
alias void delegate(T1) slot_t;
/***
* Call each of the connected slots, passing the argument(s) i to them.
*/
void emit( T1 i )
{
foreach (slot; slots[0 .. slots_idx])
{ if (slot)
slot(i);
}
}
/***
* Add a slot to the list of slots to be called when emit() is called.
*/
void connect(slot_t slot)
{
/* Do this:
* slots ~= slot;
* but use malloc() and friends instead
*/
auto len = slots.length;
if (slots_idx == len)
{
if (slots.length == 0)
{
len = 4;
auto p = std.c.stdlib.calloc(slot_t.sizeof, len);
if (!p)
core.exception.onOutOfMemoryError();
slots = (cast(slot_t*)p)[0 .. len];
}
else
{
len = len * 2 + 4;
auto p = std.c.stdlib.realloc(slots.ptr, slot_t.sizeof * len);
if (!p)
core.exception.onOutOfMemoryError();
slots = (cast(slot_t*)p)[0 .. len];
slots[slots_idx + 1 .. $] = null;
}
}
slots[slots_idx++] = slot;
L1:
Object o = _d_toObject(slot.ptr);
rt_attachDisposeEvent(o, &unhook);
}
/***
* Remove a slot from the list of slots to be called when emit() is called.
*/
void disconnect( slot_t slot)
{
debug (signal) writefln("Signal.disconnect(slot)");
for (size_t i = 0; i < slots_idx; )
{
if (slots[i] == slot)
{ slots_idx--;
slots[i] = slots[slots_idx];
slots[slots_idx] = null; // not strictly necessary
Object o = _d_toObject(slot.ptr);
rt_detachDisposeEvent(o, &unhook);
}
else
i++;
}
}
/* **
* Special function called when o is destroyed.
* It causes any slots dependent on o to be removed from the list
* of slots to be called by emit().
*/
void unhook(Object o)
{
debug (signal) writefln("Signal.unhook(o = %s)", cast(void*)o);
for (size_t i = 0; i < slots_idx; )
{
if (_d_toObject(slots[i].ptr) is o)
{ slots_idx--;
slots[i] = slots[slots_idx];
slots[slots_idx] = null; // not strictly necessary
}
else
i++;
}
}
/* **
* There can be multiple destructors inserted by mixins.
*/
~this()
{
/* **
* When this object is destroyed, need to let every slot
* know that this object is destroyed so they are not left
* with dangling references to it.
*/
if (slots)
{
foreach (slot; slots[0 .. slots_idx])
{
if (slot)
{ Object o = _d_toObject(slot.ptr);
rt_detachDisposeEvent(o, &unhook);
}
}
std.c.stdlib.free(slots.ptr);
slots = null;
}
}
private:
slot_t[] slots; // the slots to call from emit()
size_t slots_idx; // used length of slots[]
}
// A function whose sole purpose is to get this module linked in
// so the unittest will run.
void linkin() { }
unittest
{
class Observer
{
void watch(string msg, int i)
{
//writefln("Observed msg '%s' and value %s", msg, i);
captured_value = i;
captured_msg = msg;
}
void clear()
{
captured_value = 0;
captured_msg = "";
}
public:
int captured_value;
string captured_msg;
}
class Foo
{
int value() { return _value; }
int value(int v)
{
if (v != _value)
{ _value = v;
emit("setting new value", v);
}
return v;
}
mixin Signal!(string, int);
private:
int _value;
}
Foo a = new Foo;
Observer o = new Observer;
// check initial condition
assert(o.captured_value == 0);
assert(o.captured_msg == "");
// set a value while no observation is in place
a.value = 3;
assert(o.captured_value == 0);
assert(o.captured_msg == "");
// connect the watcher and trigger it
a.connect(&o.watch);
a.value = 4;
assert(o.captured_value == 4);
assert(o.captured_msg == "setting new value");
// disconnect the watcher and make sure it doesn't trigger
a.disconnect(&o.watch);
a.value = 5;
assert(o.captured_value == 4);
assert(o.captured_msg == "setting new value");
// reconnect the watcher and make sure it triggers
a.connect(&o.watch);
a.value = 6;
assert(o.captured_value == 6);
assert(o.captured_msg == "setting new value");
// delete the underlying object and make sure it doesn't cause
// a crash or other problems
delete o;
a.value = 7;
}
|