60 #ifndef LIB_SCOPED_COLLECTION_H 61 #define LIB_SCOPED_COLLECTION_H 70 #include <type_traits> 76 using LERR_(INDEX_BOUNDS);
77 using LERR_(CAPACITY);
92 ,
size_t siz =
sizeof(I)
116 return * std::launder (reinterpret_cast<I*> (&buf_));
129 template<
class TY,
typename...ARGS>
134 &&
sizeof(TY) <= siz,
135 "ElementHolder buffer too small");
137 return *
new(&buf_) TY (std::forward<ARGS> (args)...);
154 , capacity_(maxElements)
168 , capacity_(maxElements)
169 , elements_(new ElementHolder[maxElements])
171 populate_by (builder);
183 ScopedCollection (
size_t maxElements,
void (TY::*builder) (ElementHolder&), TY *
const instance)
185 , capacity_(maxElements)
186 , elements_(new ElementHolder[maxElements])
188 populate_by (builder,instance);
195 template<
typename TY>
198 template<
typename IT>
201 template<
typename IT>
205 return PullFrom<IT> (iter);
211 REQUIRE (level_ <= capacity_,
"Storage corrupted");
217 elements_[level_].destroy();
227 while (level_ < capacity_)
229 elements_[level_].template create<I>();
235 WARN (progress,
"Failure while populating ScopedCollection. " 236 "All elements will be discarded");
252 while (level_ < capacity_)
254 ElementHolder& storageFrame (elements_[level_]);
255 builder (storageFrame);
260 WARN (progress,
"Failure while populating ScopedCollection. " 261 "All elements will be discarded");
272 populate_by (
void (TY::*builder) (ElementHolder&), TY *
const instance)
274 while (level_ < capacity_)
276 ElementHolder& storageFrame (elements_[level_]);
277 (instance->*builder) (storageFrame);
282 WARN (progress,
"Failure while populating ScopedCollection. " 283 "All elements will be discarded");
304 template<
class TY =I,
typename...ARGS>
308 __ensureSufficientCapacity();
309 TY& newElm = elements_[level_].template create<TY>(std::forward<ARGS> (args)...);
319 operator[] (
size_t index)
const 322 return elements_[index].accessObj();
324 throw error::Logic (
"Attempt to access not (yet) existing object in ScopedCollection" 325 , LERR_(INDEX_BOUNDS));
340 size_t size ()
const {
return level_; }
341 size_t capacity ()
const {
return capacity_; }
342 bool empty ()
const {
return 0 == level_; }
355 typedef std::unique_ptr<ElementHolder[]> ElementStorage;
359 ElementStorage elements_;
364 __ensureSufficientCapacity()
366 if (level_ >= capacity_)
367 throw error::State (
"ScopedCollection exceeding the initially defined capacity" 381 ElementHolder* & storageLocation =
reinterpret_cast<ElementHolder* &
> (pos);
388 const ElementHolder* & storageLocation =
reinterpret_cast<const ElementHolder* &
> (pos);
393 template<
typename POS>
398 if ((pos) && (pos < src->_access_end()))
407 I* _access_begin()
const {
return & elements_[0].accessObj(); }
408 I* _access_end()
const {
return & elements_[level_].accessObj(); }
426 template<
class I,
size_t siz>
433 storage.template create<I>();
437 template<
class I,
size_t siz>
438 template<
typename TY>
445 storage.template create<TY>();
458 template<
class I,
size_t siz>
459 template<
typename IT>
464 using ElementType =
typename meta::ValueTypeBinding<IT>::value_type;
474 storage.template create<ElementType> (*iter_);
A fixed collection of non-copyable polymorphic objects.
TY & create(ARGS &&...args)
place object of type TY, forwarding ctor arguments
ScopedCollection(size_t maxElements, void(TY::*builder)(ElementHolder &), TY *const instance)
variation of RAII-style: using a builder function, which is a member of some object.
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error...
Helper template(s) for creating Lumiera Forward Iterators.
Any copy and copy construction prohibited.
TY & emplace(ARGS &&...args)
push new entry at the end of this container and build object of type TY in place there ...
Implementation namespace for support and library code.
void populate_by(CTOR builder)
init all elements at once, invoking a builder functor for each.
Storage Frame to hold one Child object.
Derived specific exceptions within Lumiera's exception hierarchy.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
ScopedCollection(size_t maxElements, CTOR builder)
creating a ScopedCollection in RAII-style: The embedded elements will be created immediately.
friend bool checkPoint(const ScopedCollection *src, POS &pos)
Iteration-logic: detect iteration end.
void populate()
init all elements default constructed
Helpers for type detection, type rewriting and metaprogramming.
Lumiera error handling (C++ interface).
void populate_by(void(TY::*builder)(ElementHolder &), TY *const instance)
variation of element initialisation, invoking a member function of some manager object for each colle...
static PullFrom< IT > pull(IT iter)
fills by copy-constructing values pulled from the iterator IT
fills the ScopedCollection with default constructed TY-instances
fills the ScopedCollection with default constructed I-instances
I & emplaceElement()
push a new element of default type to the end of this container
Adapter for building an implementation of the »Lumiera Forward Iterator« concept. ...
friend void iterNext(const ScopedCollection *, I *&pos)
Iteration-logic: switch to next position.