/*========================================================================== * weakref.d * Written in the D Programming Language (http://www.digitalmars.com/d) */ /*************************************************************************** * Creates a weak reference to a class instance. * * A weak reference lets you hold onto a pointer to an object without * preventing the garbage collector from collecting it. * If the garbage collector collects the object then the weak pointer will * become 'null'. Thus one should always check a weak pointer for null * before doing anything that depends upon it having a value. * * Tested with: * DMD 1.025 / Phobos 1.025 * DMD 1.025 / Tango 0.99.4 * * Usage example: --- class Something {} WeakRef!(Something) wa; { auto a = new Something(); wa = new WeakRef!(Something)(a); std.gc.fullCollect(); // Reference 'a' prevents collection so wa.ptr is non-null writefln(cast(void*)wa.ptr); } // 'a' now out of scope but won't be collected yet writefln(cast(void*)wa.ptr); // probably non-null still // but this will cause the Something to which 'a' refers to be collected std.gc.fullCollect(); // 'a' is gone so wa.ptr is now null writefln(cast(void*)wa.ptr); // null --- * * * Author: William V. Baxter III * Contributors: * Date: 21 Jan 2008 * Copyright: (C) 2008 William Baxter * License: Public Domain where allowed by law, ZLIB/PNG otherwise. */ //=========================================================================== module weakref; version(Tango) { alias char[] string; private { alias void delegate(Object) DisposeEvt; extern (C) void rt_attachDisposeEvent( Object obj, DisposeEvt evt ); extern (C) void rt_detachDisposeEvent( Object obj, DisposeEvt evt ); } } class WeakRef(T : Object) { private: size_t cast_ptr_; void unhook(Object o) { debug { writefln("Unhook! [%s]",o); } if (cast(size_t)cast(void*)o == cast_ptr_) { version(Tango) { rt_detachDisposeEvent(o, &unhook); } else { o.notifyUnRegister(&unhook); } cast_ptr_ = 0; } } public: this(T tptr) { cast_ptr_ = cast(size_t)cast(void*)tptr; version(Tango) { rt_attachDisposeEvent(tptr, &unhook); } else { tptr.notifyRegister(&unhook); } } ~this() { T p = ptr(); if (p) { version(Tango) { rt_detachDisposeEvent(p, &unhook); } else { p.notifyUnRegister(&unhook); } } } T ptr() { return cast(T)cast(void*)cast_ptr_; } WeakRef dup() { return new WeakRef(ptr()); } } static import std.gc; import std.stdio; unittest { class Something { int value; this(int v) { value = v; } ~this() { writefln("something %d destroyed", value); } string toString() { return "Isn't that something (" ~ std.string.toString(value) ~") !"; } } auto a = new Something(1); auto wa = new WeakRef!(Something)(a); std.gc.fullCollect(); writefln("a = ", a); writefln("wa = ", wa.ptr); assert(a is wa.ptr); delete a; std.gc.fullCollect(); writefln("a = ", a); writefln("wa = ", wa.ptr); assert(wa.ptr is null); } //--- Emacs setup --- // Local Variables: // c-basic-offset: 4 // indent-tabs-mode: nil // End: