Skip to content

Instantly share code, notes, and snippets.

@jpf91
Created July 22, 2011 12:32
Show Gist options
  • Save jpf91/1099360 to your computer and use it in GitHub Desktop.
Save jpf91/1099360 to your computer and use it in GitHub Desktop.
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