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.string : format; class Foo { public: this() { } void echo() const { writeln("FooBar"); } } void ensure(T)(T obj, string filename = __FILE__, uint line = __LINE__) { assert(obj !is null, format("Object is Null @ %s on line %d.", filename, line)); } struct scoped_ptr(T) { private: T* _ptr; public: @disable this(); @disable this(typeof(null)); @disable this(this); this(ref T obj, string filename = __FILE__, uint line = __LINE__) { ensure(obj, filename, line); this._ptr = &obj; } ~this() { writeln("DESTROY SCOPED"); if (this._ptr !is null) { *this._ptr = null; } } ref scoped_ptr!(T) opAssign(ref T obj) { this._ptr = &obj; return this; } bool IsValid() const { return *this._ptr !is null; } @property inout(T) Value() inout { return *this._ptr; } alias Value this; } class Test { private: scoped_ptr!(Foo) _sc_f; public: this(ref Foo f) { writeln("this"); this._sc_f = f; writeln("this"); } ~this() { writeln("DTOR TEST"); } void echo() const { this._sc_f.echo(); } } void main() { Foo f1 = new Foo(); { writeln("begin scope"); assert(f1 !is null); scoped_ptr!(Foo) sc = f1; //assert(sc.IsValid()); //sc.echo(); //writeln("end scope"); } assert(f1 is null); Foo f3 = new Foo(); Test t = new Test(f3); t.echo(); }
Result:
Success
/
Return code: 0
/
Compilation time:
1.844
seconds
/
Run time:
0.018
seconds
Application output:
begin scope
DESTROY SCOPED
this
this
FooBar
DTOR TEST
DESTROY SCOPED
Username
Message
Add comment
Paste info
Author:
Namespace
Views:
185
Private:
no
Expires:
Never
Uploaded:
21.07.12 22:51
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