Menu
Home
Create new Paste
Log in
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
struct scope_ptr(T : Object) { private: T* _ptr; public: @disable this(typeof(null)); @disable this(this); this(ref T obj, string filename = __FILE__, uint line = __LINE__) { if (obj is null) throw new Exception("Ptr of type " ~ T.stringof ~ " is null.", filename, line); this._ptr = &obj; } this(T* ptr, string filename = __FILE__, uint line = __LINE__) { if (ptr is null) throw new Exception("Ptr of type " ~ T.stringof ~ " is null.", filename, line); this._ptr = ptr; } ~this() { destroy(*this._ptr); *this._ptr = null; this._ptr = null; } bool Valid() const { return *this._ptr !is null; } bool opCast() const { return this.Valid(); } @property inout(T*) Access(string filename = __FILE__, uint line = __LINE__) inout { if (this._ptr is null) throw new Exception("Ptr of type " ~ T.stringof ~ " is null.", filename, line); return this._ptr; } alias Access this; } void main() { import std.stdio; class Foo { private: void print() const { writeln("I'm Foo."); } } Foo f2 = new Foo(); { scope_ptr!(Foo) sp = f2; } writeln(f2 is null); // prints true writeln(f2); // prints null f2.print(); // works fine if (f2) { writeln("if #1"); // will not printed } if (f2 !is null) { writeln("if #1"); // will not printed } }
Result:
Success
/
Return code: 0
/
Compilation time:
0.597
seconds
/
Run time:
0.002
seconds
Disassembly
Application output:
true
null
I'm Foo.
Username
Message
Add comment
Paste info
Author:
Namespace
Views:
209
Private:
no
Expires:
Never
Uploaded:
12.08.12 0:22
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