Dive into C++14 - [3] - Generic "unique resource" wrapper
http://vittorioromeo.info
The third tutorial in the "Dive into C++14" series covers the implementation of a generic "unique resource" class, with the same ownership semantics as `std::unique_ptr`.
---
`std::unique_ptr` is not limited to pointer-like handles - by adapting other kinds of handles to the `NullablePointer` concept, it can still be used, as shown here:
http://en.cppreference.com/w/cpp/concept/NullablePointer
http://stackoverflow.com/a/11002936/598696
https://www.reddit.com/r/cpp/comments/3upl42/dive_into_c14_3_generic_unique_resource_wrapper/cxgropp
Nevertheless, for educational purposes and to understand the commonalities between resource types and handle types, we're going to implement our own `std::unique_ptr`-like generic "unique wrapper".
---
The tutorial shows the implementation of the resource class and the abstraction of the behavior of several resource types (heap-allocated pointers, OpenGL VBOs, int-based-handle APIs).
It also shows a simple implementation of "scope guards" as unique resources, to show how abstracting "uniqueness semantics" can be useful even in unexpected situations.
All the code is available on GitHub. The code segments are thoroughly commented and can be read as a tutorial even without watching the video, if you prefer it that way :)
https://github.com/SuperV1234/Tutorials
I am now on Patreon - your direct support would mean a lot to me and would help me spend more time working on my videos and my open-source projects.
https://www.patreon.com/vittorioromeo
---
Links in the video:
http://en.cppreference.com/w/cpp/language/ebo
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Empty_Base_Optimization
http://en.cppreference.com/w/cpp/algorithm/swap
http://stackoverflow.com/questions/6380862
http://open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189.pdf
http://manu343726.github.io/resurce-handlers-and-value-semantics/