Movable resource handles
Matt Elkins via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jan 27 15:20:27 PST 2016
Hi all -- I am very new to D, coming from a background heavy on
C++ as well as some other languages. Consequently, I am trying to
get my head around D idioms and could use some help.
In a pet project of mine I have to deal with a lot (both many
kinds and many instances) of handles to non-memory resources.
These handles need deterministic destruction and are generally
not copyable (at least, I don't want destructors run twice), but
I do want to be able to move them ala C++'s move
constructor/assignment. In C++ I could use std::unique_ptr with a
custom deleter or roll my own variant of the same, but I am not
sure how to do this in D. std.typecons.Unique doesn't appear to
fit the bill, and my attempts to roll my own generic wrapper are
producing results more akin to std::auto_ptr than std::unique_ptr
(sorry for all the C++ references, it's the paradigm I know).
So to summarize, I want a struct (or something) to achieve these
semantics:
* Stores a handle to a resource
* On destruction, cleans up the resource (custom deleter)
* Prohibits copying or in some other fashion prevents
double-destruction
* Allows implicit moving (that is, allows destructive copy when
the moved-from object is an rvalue)
* Is lightweight (in particular, does not touch the heap)
I have been able to implement a generic struct that addresses all
of those except for allowing moving. Note that moving needs to be
implicit -- that is, not require calling something like release()
if it is an rvalue.
I apologize if this has been asked before; I found a related
discussion from a few years ago
(http://forum.dlang.org/post/jtogeq$2ndj$1@digitalmars.com), but
it was focused on performance and didn't seem to address my
specific concern.
Thanks in advance! And thanks for such a neat language!
More information about the Digitalmars-d
mailing list