Menu
Home
Create new Paste
Log in
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
import std.stdio; import std.conv : to; struct Ref(T) { private: T _obj; static ubyte[string] _counter; public: this(T obj) { assert(obj); this._obj = obj; _counter[T.stringof]++; } static ubyte[string] GetCounter() { return _counter; } @property inout(T) GetObj() inout { return this._obj; } alias GetObj this; U opCast(U : Object)() const { return *(cast(U*) &this._obj); } } mixin template TRef(Type) { private: Ref!(Type) _reference; void InitRef() { this._reference = Ref!(Type)(this); } public: @property inout(Ref!(Type)) GetRef() inout { return this._reference; } } class Foo { private: string _msg; public: mixin TRef!(Foo); alias GetRef this; this(string msg) { InitRef(); this.Set(msg); } void Set(string msg) { this._msg = msg; } void echo() const { writeln(this._msg); } /* U opCast(U)() const { writeln("object cast"); return new U(this._msg); } */ } void foo(Foo f) { writeln("\n --- begin foo\n"); f.echo(); f.Set("Foo call"); f.echo(); writeln("\n --- end foo \n"); } void bar(Ref!(Foo) rf) { writeln("\n --- begin bar\n"); rf.echo(); writeln("####"); rf.Set("bar call"); rf.echo(); Foo f = rf; f.echo(); writeln("\n --- end bar\n"); } void quatz(const Foo f) { f.echo(); } void quatz2(const Ref!(Foo) rf) { rf.echo(); write("rf: "); const Foo f = rf; write("f2: "); Foo f2 = cast(Foo) rf; write("f3: "); Foo f3 = cast(Foo) f; f.echo(); f2.echo(); f3.echo(); } void main() { Foo f = new Foo("foobar"); foo(f); bar(f); Foo f2; //bar(f2); f.Set("barfoo"); quatz(f); quatz2(f); writeln(f.GetCounter()); writeln(Ref!Foo.sizeof); writeln("end"); }
Result:
Compilation error
/
Return code: 9 (Killed)
/
Compilation time:
5.409
seconds
Username
Message
Add comment
Paste info
Author:
Guest
Views:
164
Private:
no
Expires:
Never
Uploaded:
24.06.12 0:26
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.062
Pointer size:
m64
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason