Created
July 22, 2011 12:32
-
-
Save jpf91/1099360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct File | |
{ | |
private struct Impl | |
{ | |
uint refs = uint.max / 2; | |
//Other 'by-reference' members ... | |
this(uint r) | |
{ | |
refs = r; | |
} | |
} | |
private Impl * p; | |
this() | |
{ | |
p = new Impl(1); | |
} | |
~this() | |
{ | |
if (!p) return; | |
if (p.refs == 1) close(); | |
else --p.refs; | |
} | |
this(this) | |
{ | |
if (!p) return; | |
assert(p.refs); | |
++p.refs; | |
} | |
/** | |
Assigns a file to another. The target of the assignment gets detached | |
from whatever file it was attached to, and attaches itself to the new | |
file. | |
*/ | |
void opAssign(File rhs) | |
{ | |
swap(p, rhs.p); | |
} | |
void detach() | |
{ | |
if (!p) return; | |
if (p.refs == 1) close(); | |
else --p.refs; | |
p = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment