Skip to content

Instantly share code, notes, and snippets.

@jpf91 jpf91/gist:1099229
Created Jul 22, 2011

Embed
What would you like to do?
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.