Lumiera  0.pre.03
»edit your freedom«
scoped-holder.hpp File Reference

Go to the source code of this file.

Description

Some wrappers for coping with ownership problems.

Working with collections of objects, especially in conjunction with polymorphism, can be challenging when we are bound to care for lifecycle and ownership for the contained classes. There are several solutions, including the boost::ptr_container library or lib::ScopedPtrVect, the use of shared_ptr or even a garbage collector. Sometimes circumstances rather call for a very simple or lightweight solution though.

ScopedPtrHolder is a simple extension to std::unique_ptr, enabling to use it within STL containers if we stick to a specific protocol. The idea is to permit copying as long as the unique_ptr is empty. This can be used to allow for extension of the STL container on demand, i.e. to handle the typical situation of a registry which is initialised lazily, but only released in a controlled fashion.

ScopedHolder implements a similar concept for in-place storage of noncopyable objects within STL containers.

While the added copy operations (secured with the "empty" requirement) are enough to use those holders within fixed sized STL containers, supporting dynamic growth (like in std::vector::resize() ) additionally requires a facility to transfer the lifecycle management control between holder instances. This is the purpose of the transfer_control friend function.

Deprecated:
this is a pre C++11 concept and superseded by rvalue references
See also
scoped-holder-test.cpp
scoped-holder-transfer.hpp use in std::vector
scoped-ptrvect.hpp simple pointer-holding collection

Definition in file scoped-holder.hpp.

#include "lib/error.hpp"
#include <memory>

Classes

class  ScopedHolder< TY >
 Inline buffer holding and owning an object similar to unique_ptr. More...
 
class  ScopedPtrHolder< B >
 Extension to std::unique_ptr, allowing copy operations on empty pointers (i.e. More...
 

Namespaces

 lib
 Implementation namespace for support and library code.