69 #ifndef LIB_SCOPED_COLLECTION_H 70 #define LIB_SCOPED_COLLECTION_H 79 #include <type_traits> 85 using LERR_(INDEX_BOUNDS);
86 using LERR_(CAPACITY);
101 ,
size_t siz =
sizeof(I)
125 return * std::launder (reinterpret_cast<I*> (&buf_));
138 template<
class TY,
typename...ARGS>
143 &&
sizeof(TY) <= siz,
144 "ElementHolder buffer too small");
146 return *
new(&buf_) TY (std::forward<ARGS> (args)...);
163 , capacity_(maxElements)
177 , capacity_(maxElements)
178 , elements_(new ElementHolder[maxElements])
180 populate_by (builder);
192 ScopedCollection (
size_t maxElements,
void (TY::*builder) (ElementHolder&), TY *
const instance)
194 , capacity_(maxElements)
195 , elements_(new ElementHolder[maxElements])
197 populate_by (builder,instance);
204 template<
typename TY>
207 template<
typename IT>
210 template<
typename IT>
214 return PullFrom<IT> (iter);
220 REQUIRE (level_ <= capacity_,
"Storage corrupted");
226 elements_[level_].destroy();
236 while (level_ < capacity_)
238 elements_[level_].template create<I>();
244 WARN (progress,
"Failure while populating ScopedCollection. " 245 "All elements will be discarded");
261 while (level_ < capacity_)
263 ElementHolder& storageFrame (elements_[level_]);
264 builder (storageFrame);
269 WARN (progress,
"Failure while populating ScopedCollection. " 270 "All elements will be discarded");
281 populate_by (
void (TY::*builder) (ElementHolder&), TY *
const instance)
283 while (level_ < capacity_)
285 ElementHolder& storageFrame (elements_[level_]);
286 (instance->*builder) (storageFrame);
291 WARN (progress,
"Failure while populating ScopedCollection. " 292 "All elements will be discarded");
313 template<
class TY =I,
typename...ARGS>
317 __ensureSufficientCapacity();
318 TY& newElm = elements_[level_].template create<TY>(std::forward<ARGS> (args)...);
328 operator[] (
size_t index)
const 331 return elements_[index].accessObj();
333 throw error::Logic (
"Attempt to access not (yet) existing object in ScopedCollection" 334 , LERR_(INDEX_BOUNDS));
349 size_t size ()
const {
return level_; }
350 size_t capacity ()
const {
return capacity_; }
351 bool empty ()
const {
return 0 == level_; }
364 typedef std::unique_ptr<ElementHolder[]> ElementStorage;
368 ElementStorage elements_;
373 __ensureSufficientCapacity()
375 if (level_ >= capacity_)
376 throw error::State (
"ScopedCollection exceeding the initially defined capacity" 390 ElementHolder* & storageLocation =
reinterpret_cast<ElementHolder* &
> (pos);
397 const ElementHolder* & storageLocation =
reinterpret_cast<const ElementHolder* &
> (pos);
402 template<
typename POS>
407 if ((pos) && (pos < src->_access_end()))
416 I* _access_begin()
const {
return & elements_[0].accessObj(); }
417 I* _access_end()
const {
return & elements_[level_].accessObj(); }
435 template<
class I,
size_t siz>
442 storage.template create<I>();
446 template<
class I,
size_t siz>
447 template<
typename TY>
454 storage.template create<TY>();
467 template<
class I,
size_t siz>
468 template<
typename IT>
473 using ElementType =
typename meta::ValueTypeBinding<IT>::value_type;
483 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.