Library: logic to accept further elements

- verifies if new element can just fit in
- otherwise ensure the storage adjustments are basically possible
- throw exception in case the new element can not be accommodated
- else request possible storage adjustments
- and finally let the allocator place the new element
This commit is contained in:
Fischlurch 2024-06-08 17:35:14 +02:00
parent 0a788570a9
commit 446f133c09
3 changed files with 80 additions and 24 deletions

View file

@ -156,6 +156,8 @@ namespace lib {
using Fac::Fac; // pass-through ctor
const bool isDisposable{false}; ///< memory must be explicitly deallocated
bool canExpand(size_t){ return false; }
Bucket*
realloc (Bucket* data, size_t demand)
@ -217,12 +219,12 @@ namespace lib {
using lib::meta::is_Subclass;
template<class I, class E>
struct MemStrategy
struct HandlingStrategy
{
bool disposable :1 ;
bool wild_move :1 ;
MemStrategy (bool unmanaged)
HandlingStrategy (bool unmanaged)
: disposable{unmanaged or (is_trivially_destructible_v<E> and
is_trivially_destructible_v<I>)}
, wild_move{false}
@ -257,7 +259,7 @@ namespace lib {
bool
canDestroy()
{
return disposable
return (disposable and is_trivially_destructible_v<TY>)
or (is_trivially_destructible_v<TY> and is_trivially_destructible_v<I>)
or (has_virtual_destructor_v<I> and is_Subclass<TY,I>())
or (is_same_v<TY,E> and is_Subclass<E,I>());
@ -267,8 +269,7 @@ namespace lib {
bool
canDynGrow()
{
return is_same_v<TY,E>
or (is_trivially_move_constructible_v<TY> and wild_move);
return is_same_v<TY,E> or wild_move;
}
auto
@ -299,7 +300,7 @@ namespace lib {
{
using Coll = Several<I>;
MemStrategy<I,E> memStrategy_{POL::isDisposable};
HandlingStrategy<I,E> handling_{POL::isDisposable};
public:
SeveralBuilder() = default;
@ -397,31 +398,34 @@ namespace lib {
void
emplaceElm (ARGS&& ...args)
{
if (not memStrategy_.template canDestroy<TY>())
if (not handling_.template canDestroy<TY>())
throw err::Invalid{_Fmt{"Unable to handle destructor for element type %s"}
% util::typeStr<TY>()};
// mark acceptance of trivially movable arbitrary data types
if (not memStrategy_.template markWildMovePossibility<TY>())
if (not handling_.template markWildMovePossibility<TY>())
throw err::Invalid{_Fmt{"Unable to trivially move element type %s, "
"while other elements in this container are trivially movable."}
% util::typeStr<TY>()};
////////////////////////////////////OOO check here for suitable spread
// ensure sufficient element capacity or the ability to adapt element spread
if (Coll::spread() < sizeof(TY) and not (Coll::empty() or handling_.wild_move))
throw err::Invalid{_Fmt{"Unable to place element of type %s (size=%d)"
"into container for element size %d."}
% util::typeStr<TY>() % sizeof(TY) % Coll::spread()};
////////////////////////////////////OOO check here for sufficient space
// ensure sufficient storage or verify the ability to re-allocate
if (not (Coll::hasReserve(sizeof(TY))
or POL::canExpand(sizeof(TY))
or handling_.template canDynGrow<TY>()))
throw err::Invalid{_Fmt{"Unable to accommodate further element of type %s "}
% util::typeStr<TY>()};
size_t elmSiz = sizeof(TY);
size_t newCnt = Coll::size()+1;
adjustStorage (newCnt, requiredSpread(elmSiz));
adjustStorage (newCnt, max (elmSiz, Coll::spread()));
Coll::data_->cnt = newCnt;
POL::template createAt<TY> (Coll::data_, Coll::size(), forward<ARGS> (args)...);
}
size_t
requiredSpread (size_t elmSiz)
{
return util::max (Coll::spread(), elmSiz);
POL::template createAt<TY> (Coll::data_, newCnt-1, forward<ARGS> (args)...);
}
};

View file

@ -188,6 +188,15 @@ namespace lib {
return data_? data_->spread : sizeof(I);
}
bool
hasReserve (size_t extraSize) const
{
if (extraSize > spread())
extraSize += (extraSize - spread())*size();
return data_
and data_->buffSiz >= size()*spread() + extraSize;
}
private:
void
discardData()

View file

@ -82444,6 +82444,9 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<arrowlink COLOR="#ec1c03" DESTINATION="ID_584325941" ENDARROW="Default" ENDINCLINATION="167;18;" ID="Arrow_ID_1090340504" STARTARROW="None" STARTINCLINATION="178;0;"/>
<arrowlink COLOR="#ec1c03" DESTINATION="ID_1523639170" ENDARROW="Default" ENDINCLINATION="167;18;" ID="Arrow_ID_15683997" STARTARROW="None" STARTINCLINATION="178;0;"/>
<icon BUILTIN="flag-pink"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717858632242" HGAP="33" ID="ID_230351736" MODIFIED="1717858646313" TEXT="korrekte Methode f&#xfc;r Verschiebung w&#xe4;hlen" VSHIFT="23">
<icon BUILTIN="flag-yellow"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#f0d5c5" COLOR="#990033" CREATED="1717723649872" ID="ID_1412900866" MODIFIED="1717723682406" TEXT="Deleter einbinden?">
@ -82575,6 +82578,33 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717857952662" ID="ID_151747312" MODIFIED="1717858054385" TEXT="adjustStorage">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1717858103112" ID="ID_665641447" MODIFIED="1717858118107" TEXT="Einstieg f&#xfc;r Storage-Anpassungen">
<node CREATED="1717858205276" ID="ID_1237762599" MODIFIED="1717858215918" TEXT="wird bei Bedarf aufgerufen"/>
<node CREATED="1717858386974" ID="ID_319057950" MODIFIED="1717858400465" TEXT="bekommt nur noch die geforderte Zielkapazit&#xe4;t"/>
<node CREATED="1717858221198" ID="ID_13569379" MODIFIED="1717858362700" TEXT="keine Pr&#xfc;fung der Handhabbarkeit mehr">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
die grunds&#228;tzliche M&#246;glichkeit der Anpassung ist bereits vorher sichergestellt, denn der veranlassende API-call w&#228;re sonst schon per Exception abgebrochen worden
</p>
</body>
</html>
</richcontent>
<icon BUILTIN="yes"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717857964116" ID="ID_1398927698" MODIFIED="1717858080950" TEXT="spread-Erweiterung korrekt handhaben">
<linktarget COLOR="#494890" DESTINATION="ID_1398927698" ENDARROW="Default" ENDINCLINATION="-414;28;" ID="Arrow_ID_834912184" SOURCE="ID_1036114720" STARTARROW="None" STARTINCLINATION="389;36;"/>
<icon BUILTIN="yes"/>
<node CREATED="1717858131668" ID="ID_1076555932" MODIFIED="1717858160950" TEXT="mu&#xdf; vorher f&#xfc;r den notwendigen Platz sorgen"/>
<node CREATED="1717858161497" ID="ID_114984038" MODIFIED="1717858172964" TEXT="kann danach die bestehenden Elemente verschieben"/>
</node>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716914864769" ID="ID_966642586" MODIFIED="1716914906125" TEXT="Builder-Operationen">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1717634297656" ID="ID_330412196" MODIFIED="1717688579115" TEXT="Move-builder und Container per Slice ausgeben">
@ -82673,7 +82703,7 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node CREATED="1717796446579" ID="ID_229190267" MODIFIED="1717796449135" TEXT="unmanaged"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717805473588" ID="ID_1414521671" MODIFIED="1717806898610" TEXT="Logik f&#xfc;r Element-Konstruktion">
<node COLOR="#435e98" CREATED="1717805473588" ID="ID_1414521671" MODIFIED="1717860659461" TEXT="Logik f&#xfc;r Element-Konstruktion">
<icon BUILTIN="info"/>
<node COLOR="#338800" CREATED="1717805494018" ID="ID_1249779598" MODIFIED="1717810959882" TEXT="Element-Typ ist akzepabel">
<icon BUILTIN="button_ok"/>
@ -82707,15 +82737,28 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node CREATED="1717809142579" ID="ID_528651025" MODIFIED="1717809223883" TEXT="Vorsicht: auch Ausschlu&#xdf; pr&#xfc;fen falls wild-move-Flag gesetzt"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717806413974" ID="ID_174948167" MODIFIED="1717810968198" TEXT="spread">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1717806413974" ID="ID_174948167" MODIFIED="1717859049170" TEXT="spread">
<icon BUILTIN="button_ok"/>
<node CREATED="1717806419241" ID="ID_756384981" MODIFIED="1717806423193" TEXT="ist ausreichend"/>
<node CREATED="1717806423863" ID="ID_1855708298" MODIFIED="1717806461147" TEXT="&#x2228; container leer &#x27f9; Spread redefinieren"/>
<node CREATED="1717806462116" ID="ID_88555748" MODIFIED="1717806508162" TEXT="&#x2228; Fall-5 &#x27f9; Spread rejustieren"/>
<node COLOR="#338800" CREATED="1717857894087" ID="ID_1190583889" MODIFIED="1717859039840" TEXT="hier wird nur die M&#xf6;glichkeit gepr&#xfc;ft">
<icon BUILTIN="yes"/>
<node CREATED="1717857941328" ID="ID_1036114720" MODIFIED="1717858080950" TEXT="adjustStorage macht die Arbeit">
<arrowlink COLOR="#494890" DESTINATION="ID_1398927698" ENDARROW="Default" ENDINCLINATION="-414;28;" ID="Arrow_ID_834912184" STARTARROW="None" STARTINCLINATION="389;36;"/>
</node>
</node>
</node>
<node COLOR="#338800" CREATED="1717806648902" ID="ID_31178019" MODIFIED="1717860648985" TEXT="storage">
<icon BUILTIN="button_ok"/>
<node CREATED="1717806656110" ID="ID_620814998" MODIFIED="1717806658963" TEXT="ist ausreichend">
<node COLOR="#338800" CREATED="1717860614915" ID="ID_1797884883" MODIFIED="1717860632353" TEXT="Pr&#xfc;froutine in Basis-Container">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1717860636528" ID="ID_1467740898" MODIFIED="1717860647333" TEXT="auch eventuelle Spread-Erweiterung mit einrechnen">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717806648902" ID="ID_31178019" MODIFIED="1717810970218" TEXT="storage">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1717806656110" ID="ID_620814998" MODIFIED="1717806658963" TEXT="ist ausreichend"/>
<node CREATED="1717806666956" ID="ID_1772310323" MODIFIED="1717806751117" TEXT="&#x2228; Strategy::canDynGrow&lt;TY&gt;">
<node CREATED="1717806757128" ID="ID_1234267874" MODIFIED="1717806762203" TEXT="TY == E (oder I)"/>
<node CREATED="1717806781292" ID="ID_1519435147" MODIFIED="1717806785968" TEXT="&#x2228; Fall-5"/>