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

Go to the source code of this file.

Description

Managing a collection of non-copyable polymorphic objects in compact storage.

This helper supports the frequently encountered situation where a service implementation internally manages a collection of implementation related sub-components with reference semantics. Typically, those objects are being used polymorphically, and often they are also added step by step. The storage holding all those child objects is allocated in one chunk and never adjusted.

usage patterns

The common ground for all usage of this container is to hold some elements with exclusive ownership; when the enclosing container goes out of scope, all the dtors of the embedded objects will be invoked. Frequently this side effect is the reason for using the container: we want to own some resource handles to be available exactly as long as the managing object needs and accesses them.

There are two different usage patterns for populating a ScopedCollection

  • the "stack style" usage creates an empty container (using the one arg ctor just to specify the maximum size). The storage to hold up to this number of objects is (heap) allocated right away, but no objects are created. Later on, individual objects are "pushed" into the collection by invoking #emplaceElement() to create a new element of the default type I) or #emplace<Type>(args) to create some subtype. This way, the container is being filled successively.
  • the "RAII style" usage strives to create all of the content objects right away, immediately after the memory allocation. This usage pattern avoids any kind of "lifecycle state". Either the container comes up sane and fully populated, or the ctor call fails and any already created objects are discarded.
    Note
    intentionally there is no operation to discard individual objects, all you can do is to #clear() the whole container.
    the container can hold instances of a subclass of the type defined by the template parameter I. But you need to ensure in this case that the defined buffer size for each element (2nt template parameter) is sufficient to hold any of these subclass instances. This condition is protected by a static assertion (compilation failure).
    Warning
    when using subclasses, a virtual dtor is mandatory
    deliberately not threadsafe
    See also
    ScopedCollection_test
    scoped-ptrvect.hpp quite similar, but using individual heap pointers

Definition in file scoped-collection.hpp.

#include "lib/error.hpp"
#include "lib/nocopy.hpp"
#include "lib/meta/trait.hpp"
#include "lib/iter-adapter.hpp"
#include <cstddef>
#include <type_traits>

Classes

class  ScopedCollection< I, siz >::ElementHolder
 Storage Frame to hold one Child object. More...
 
class  ScopedCollection< I, siz >::FillAll
 
class  ScopedCollection< I, siz >::FillWith< TY >
 fills the ScopedCollection with default constructed I-instances More...
 
class  ScopedCollection< I, siz >::FillWith< TY >
 fills the ScopedCollection with default constructed I-instances More...
 
class  ScopedCollection< I, siz >::PullFrom< IT >
 fills the ScopedCollection with default constructed TY-instances More...
 
class  ScopedCollection< I, siz >::PullFrom< IT >
 fills the ScopedCollection with default constructed TY-instances More...
 
class  ScopedCollection< I, siz >
 A fixed collection of non-copyable polymorphic objects. More...
 

Namespaces

 lib
 Implementation namespace for support and library code.