From 6b44326c49bb009c42356518494f346828c71d86 Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Wed, 7 Sep 2016 13:21:16 +0300 Subject: [PATCH] life cycle in D, RAII and non scoped object creations, perhaps illustraion of allocation could be interesting as well --- src/main/d/lifetime.d | 117 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/main/d/lifetime.d diff --git a/src/main/d/lifetime.d b/src/main/d/lifetime.d new file mode 100644 index 00000000000..042eefc50a4 --- /dev/null +++ b/src/main/d/lifetime.d @@ -0,0 +1,117 @@ +/** + * The D language investigations: + * here life events except perhaps allocator stage () + * # dmd -v -vgc -I/opt/local//libexec/dmd-bootstrap/include/druntime/import -I/opt/local/libexec/dmd-bootstrap/include/phobos -L-L/opt/local/libexec/dmd-bootstrap/lib lifetime.d + * Expected output for + * bash-3.2$ dmd --version + * DMD64 D Compiler v2.070-devel + * Copyright (c) 1999-2015 by Digital Mars written by Walter Bright + * + + * bash-3.2$ ./lifetime +A::static_this: start +A::static_this: end +main:start +(4557316096)lifetime.A::this: start +(4557316096)lifetime.A::this: end +(4557316112)lifetime.A::this: start +(4557316112)lifetime.A::this: end +inner scope started +(140734540366608)lifetime.A::this: start +(140734540366608)lifetime.A::this: end +(140734540366608)lifetime.A::~this: start +(140734540366608)lifetime.A::~this: end +inner scope ended +(4557316096)lifetime.A::~this: start +(4557316096)lifetime.A::~this: end +scopedAllocations::called +scopedAllocations::start +(140734540366432)lifetime.A::this: start +(140734540366432)lifetime.A::this: end +(140734540366464)lifetime.A::this: start +(140734540366464)lifetime.A::this: end +(140734540366496)lifetime.A::this: start +(140734540366496)lifetime.A::this: end +scopedAllocations::end +(140734540366496)lifetime.A::~this: start +(140734540366496)lifetime.A::~this: end +(140734540366464)lifetime.A::~this: start +(140734540366464)lifetime.A::~this: end +(140734540366432)lifetime.A::~this: start +(140734540366432)lifetime.A::~this: end +scopedAllocations::returned +main:end +A::~static this: start +A::~static this: end +(4557316112)lifetime.A::~this: start +(4557316112)lifetime.A::~this: end + */ +import object; +import std.string; +import std.stdio; +import std.typecons; + +class A { + static this() { + writeln("A::static_this: start"); + writeln("A::static_this: end"); + } + this() { + trace("this: start", this); + trace("this: end", this); + } + + ~this() { + trace("~this: start", this); + trace("~this: end", this); + } + + static ~this() { + writeln("A::~static this: start"); + writeln("A::~static this: end"); + } +} + +void trace(string msg, Object obj) { + write("("); + write(obj.toHash()); + write(")"); + write(obj.classinfo.toString()); + write("::"); + writeln(msg); +} + + +void scopedAllocations() { + writeln("scopedAllocations::start"); + auto a = scoped!A(); + auto b = scoped!A(); + auto c = scoped!A(); + writeln("scopedAllocations::end"); +} + +void main() { + writeln("main:start"); + auto a = new A(); + auto c = new A(); + auto d = Object.factory("A"); + { + writeln("inner scope started"); + auto b = scoped!A(); + } + writeln("inner scope ended"); + delete a; + writeln("scopedAllocations::called"); + scopedAllocations(); + writeln("scopedAllocations::returned"); + writeln("main:end"); +} + +extern(C) { + /** + * ABI compatibility, known bug in 2.70 :) + */ + void __dmd_personality_v0(){} + void _d_throwdwarf(){} + void __dmd_begin_catch(){} +}