Boost logo

Boost :

Subject: [boost] [RFC] unique_val (handles, IDs, descriptors...)
From: Miguel Ojeda (miguel.ojeda.sandonis_at_[hidden])
Date: 2018-02-14 21:18:42


Hi all,

While working on a medium-sized project, I had to wrap quite some
handles, IDs, descriptors, etc. into RAII classes. Somehow, writing
those and making them movable seemed more verbose than needed. I
searched in the STL and Boost for something already implemented, but
nothing seemed to come up. I decided to write my own prototype to see
whether I was missing something important; but actually using it in my
project felt quite natural and simple.

At this point, I asked for a second opinion from a well-known expert
(thanks Scott!) and he agreed that it seemed like a reasonable idea
but, of course, he would also be surprised if others haven't come up
with something similar. So I polished it up a bit and uploaded it to:

    https://github.com/ojeda/unique_val

Below you have the introduction inlined [1].

If you have the time, please take a look (specially to the Rationale)
and let me know your comments. The code itself is very short and
straightforward.

If you already have this class/template in Boost somewhere that I
missed, please let me know! Otherwise, if you think this could be
interesting to put into Boost in some existing library (not sure
which) or as a new tiny library, I would be glad to make the effort to
expand it, clean it up, etc.

Thank you!
Miguel

[1]

"""
A single-header header-only library for representing unique values
(handles, IDs, descriptors...) that default to a value on move for
C++11, C++14 and C++17.

It simplifies writing resource-owning classes (RAII) with move
semantics support for resources with handles, IDs or descriptors that
are supposed to be used as unique values, not as unique pointers. For
instance, it can be used with non-pointer handlers (like the `int`
file/socket descriptors in POSIX) and with opaque pointer handlers
(like the `HWND` handles in the Win32 API).

As such, it can be used as an alternative to `std::unique_ptr` with or
without custom deleter, specially for resources that use a non-pointer
type or resources that are not simply heap-allocated.

It does not have any overhead compared to using the original type. It
supports booleans, integers, pointers and enumerations. The default
value can be different than the default-constructed value (i.e.
different than 0, nullptr, etc.).
"""


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk