Library: basic functionality of the Several-container working

...passes the simplest unit test
 * create a Several<int>
 * populate from `std::initializer_list`
 * random-access to elements

''next step would be to implement iteration''
This commit is contained in:
Fischlurch 2024-06-10 18:52:25 +02:00
parent 8534914c71
commit a3e8579e4a
4 changed files with 112 additions and 31 deletions

View file

@ -114,7 +114,7 @@ namespace lib {
using BucketAlloT = typename AlloT::template rebind_traits<Bucket>;
auto bucketAllo = adaptAllocator<Bucket>();
try { BucketAlloT::construct (bucketAllo, bucket, storageBytes, spread); }
try { BucketAlloT::construct (bucketAllo, bucket, cnt*spread, spread); }
catch(...)
{
AlloT::deallocate (baseAllocator(), loc, storageBytes);
@ -149,7 +149,7 @@ namespace lib {
for (size_t idx=0; idx<cnt; ++idx)
ElmAlloT::destroy (elmAllo, & bucket->subscript(idx));
}
size_t storageBytes = bucket->buffSiz;
size_t storageBytes = Bucket::requiredStorage (bucket->buffSiz);
std::byte* loc = reinterpret_cast<std::byte*> (bucket);
AlloT::deallocate (baseAllocator(), loc, storageBytes);
};
@ -313,16 +313,16 @@ namespace lib {
__ensureMark<TY> (VIRTUAL);
return [factory](ArrayBucket<I>* bucket){ unConst(factory).template destroy<I> (bucket); };
}
if (is_same_v<TY,E> and is_Subclass<E,I>())
{
__ensureMark<TY> (ELEMENT);
return [factory](ArrayBucket<I>* bucket){ unConst(factory).template destroy<E> (bucket); };
}
if (is_trivially_destructible_v<TY>)
{
__ensureMark<TY> (TRIVIAL);
return [factory](ArrayBucket<I>* bucket){ unConst(factory).template destroy<TY> (bucket); };
}
if (is_same_v<TY,E> and is_Subclass<E,I>())
{
__ensureMark<TY> (ELEMENT);
return [factory](ArrayBucket<I>* bucket){ unConst(factory).template destroy<E> (bucket); };
}
throw err::Invalid{_Fmt{"Unsupported kind of destructor for element type %s."}
% util::typeStr<TY>()};
}

View file

@ -48,14 +48,12 @@
** random access through references to a interface type. It can only be created
** and populated through a builder, and is immutable during lifetime, while it
** can hold non-const element data. The actual implementation data types and the
** allocator framework used are _not exposed in the front-end's type signature._
** employed allocator framework are _not exposed in the front-end's type signature._
** The container is single-ownership (move-asignable); some additional metadata
** and the data storage reside in an `ArrayBucket<I>`, managed by the allocator.
** and the data storage reside within an `ArrayBucket<I>`, managed by the allocator.
** In its simplest form, this storage is heap allocated and automatically deleted.
**
** @todo as of 2016, this concept seems very questionable: do we _really_ want
** to abstract over random access, or do we _actually_ want for-iteration??
** @warning WIP-WIP-WIP in rework 5/2025
** @warning WIP-WIP in rework 6/2025
** @see several-builder.hpp
*/
@ -98,7 +96,7 @@ namespace lib {
static size_t
requiredStorage (size_t cnt, size_t spread)
requiredStorage (size_t cnt, size_t spread =1)
{
return sizeof(ArrayBucket) - sizeof(storage)
+ cnt * spread;
@ -113,6 +111,7 @@ namespace lib {
std::byte* elm = storage;
size_t off = idx * spread;
elm += off;
ENSURE (storage <= elm and elm < storage+buffSiz);
return * std::launder (reinterpret_cast<I*> (elm));
}
@ -152,8 +151,15 @@ namespace lib {
ERROR_LOG_AND_IGNORE (progress, "clean-up Several data")
/// Move-Assignment allowed...
Several (Several&&) =default;
Several& operator= (Several&&) =default;
Several (Several&& rr)
{
std::swap (data_, rr.data_);
}
Several& operator= (Several&& rr)
{
std::swap (data_, rr.data_);
return *this;
}
size_t
size() const
@ -174,8 +180,8 @@ namespace lib {
return data_->subscript (idx);
}
I& front() { return operator[] (data_? data_->size_-1 : 0); }
I& back() { return operator[] (0); }
I& front() { return operator[] (0); }
I& back() { return operator[] (data_? data_->cnt-1 : 0); }
using iterator = I*;
using const_iterator = I const*;

View file

@ -117,7 +117,7 @@ namespace test{
/** @test TODO demonstrate basic behaviour
* @todo WIP 5/24 🔁 define implement
* @todo WIP 6/24 define 🔁 implement
*/
void
simpleUsage()
@ -131,7 +131,7 @@ namespace test{
/** @test TODO various ways to build an populate the container
* @todo WIP 5/24 🔁 define implement
* @todo WIP 6/24 🔁 define implement
*/
void
check_Builder()
@ -140,7 +140,7 @@ namespace test{
/** @test TODO proper handling of exceptions during population
* @todo WIP 5/24 🔁 define implement
* @todo WIP 6/24 🔁 define implement
*/
void
check_ErrorHandling()
@ -149,7 +149,7 @@ namespace test{
/** @test TODO verify access operations on the actual container
* @todo WIP 5/24 🔁 define implement
* @todo WIP 6/24 🔁 define implement
*/
void
check_ElementAccess()
@ -158,7 +158,7 @@ namespace test{
/** @test TODO demonstrate integration with a custom allocator
* @todo WIP 5/24 🔁 define implement
* @todo WIP 6/24 🔁 define implement
*/
void
check_CustomAllocator()

View file

@ -81711,19 +81711,28 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716861725635" ID="ID_1822785959" MODIFIED="1716861768215" TEXT="festlegen des Element-Spread">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1716861791476" ID="ID_1153264423" MODIFIED="1716861797994" TEXT="Zielkonflikt">
<node BACKGROUND_COLOR="#e0ceaa" COLOR="#360bbc" CREATED="1716861791476" ID="ID_1153264423" MODIFIED="1718034898633" TEXT="Zielkonflikt">
<icon BUILTIN="messagebox_warning"/>
<node CREATED="1716861799273" ID="ID_521198508" MODIFIED="1716861807996" TEXT="man m&#xf6;chte Elemente verdrahten"/>
<node CREATED="1716861808632" ID="ID_361446966" MODIFIED="1716861818586" TEXT="man k&#xf6;nnte die Storage noch dynamisch anpassen"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716901371443" ID="ID_679786160" MODIFIED="1716901388931" TEXT="Beschlu&#xdf;: durch eine explizit aufzurufende Builder-Methode">
<node BACKGROUND_COLOR="#c8c0b6" COLOR="#435e98" CREATED="1716901371443" ID="ID_679786160" MODIFIED="1718034853650" TEXT="Beschlu&#xdf;: durch eine explizit aufzurufende Builder-Methode">
<icon BUILTIN="yes"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716901390803" ID="ID_1798868375" MODIFIED="1716901421256" TEXT="diese reloziert ggfs. bestehende Elemente">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1716901390803" ID="ID_1798868375" MODIFIED="1718034849850" TEXT="diese reloziert ggfs. bestehende Elemente">
<icon BUILTIN="button_ok"/>
</node>
<node CREATED="1716901402394" ID="ID_1898535307" MODIFIED="1716901416153" TEXT="wenn man das nicht will, soll man&apos;s nicht machen">
<icon BUILTIN="smiley-oh"/>
</node>
<node CREATED="1718034825512" ID="ID_78041695" MODIFIED="1718034844185" TEXT="Einschr&#xe4;nkung: is_trivially_movable&lt;TY&gt;">
<icon BUILTIN="idea"/>
</node>
</node>
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1718034857705" ID="ID_725324060" MODIFIED="1718034872054" TEXT="Alignment beachten">
<icon BUILTIN="messagebox_warning"/>
<node CREATED="1718035817292" ID="ID_908227037" MODIFIED="1718035826853" TEXT="Verwendungen von sizeof(TY)">
<icon BUILTIN="idea"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716901426183" ID="ID_1212967398" MODIFIED="1716901442101" TEXT="Element-Zugriff und Iteration">
@ -83041,14 +83050,80 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1715625082614" ID="ID_1575150785" MODIFIED="1715625089779" TEXT="SeveralBuilder_test">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716857382440" ID="ID_46730448" MODIFIED="1716857402102" TEXT="simpleUsage">
<icon BUILTIN="flag-yellow"/>
<node CREATED="1717980996623" ID="ID_644007892" MODIFIED="1717981004874" TEXT="Several mit drei Zahlen"/>
<node CREATED="1717981006247" ID="ID_1037000737" MODIFIED="1717981014900" TEXT="wieder darauf zugreifen (subscript)">
<node BACKGROUND_COLOR="#fafe99" COLOR="#fa002a" CREATED="1717981016452" ID="ID_406273640" MODIFIED="1717981023372" STYLE="fork" TEXT="double free">
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1716857382440" ID="ID_46730448" MODIFIED="1718037989238" TEXT="simpleUsage">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1717980996623" ID="ID_644007892" MODIFIED="1718037983007" TEXT="Several mit sieben Fibonacci-Zahlen">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1717981006247" ID="ID_1037000737" MODIFIED="1718037985334" TEXT="wieder darauf zugreifen (subscript)">
<icon BUILTIN="button_ok"/>
<node COLOR="#435e98" CREATED="1717981016452" ID="ID_406273640" MODIFIED="1718035010797" STYLE="fork" TEXT="double free">
<font NAME="SansSerif" SIZE="12"/>
<icon BUILTIN="broken-line"/>
<node CREATED="1717983599417" ID="ID_572377876" MODIFIED="1717983613707" TEXT="Beobachtung">
<node CREATED="1717983614665" ID="ID_300848351" MODIFIED="1717983636400" TEXT="Aufregrufen ~SeveralBuilder() &#x27f6; ~Several()"/>
<node CREATED="1717983695604" ID="ID_1793124631" MODIFIED="1717983715053" TEXT="das ArrayBucket sieht aus als w&#xe4;re es uninitialisiert"/>
<node CREATED="1717983867684" ID="ID_255570177" MODIFIED="1717983893829" TEXT="der double-free wird dann aber im deallocate f&#xfc;r die storage-bytes entdeckt"/>
</node>
<node COLOR="#338800" CREATED="1718034918473" ID="ID_476774347" MODIFIED="1718035008717" TEXT="banal: mu&#xdf; den move-ctor explizit implementieren">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
...denn die default-Impl kopiert skalare Typen lediglich; hier m&#252;ssen wir sie aber wirklich austauschen, damit nur eine Instanz den aktiven Pointer h&#228;lt...
</p>
</body>
</html>
</richcontent>
<icon BUILTIN="button_ok"/>
</node>
</node>
<node COLOR="#435e98" CREATED="1718035012040" ID="ID_855064737" MODIFIED="1718037958682" TEXT="Problem mit Zellen-Zugriff">
<icon BUILTIN="broken-line"/>
<node CREATED="1718035039112" ID="ID_1308329051" MODIFIED="1718035045323" TEXT="back() liefert nicht das richtige Element"/>
<node CREATED="1718035045791" ID="ID_1798109493" MODIFIED="1718035049107" TEXT="Debugger....">
<node COLOR="#435e98" CREATED="1718035376451" ID="ID_63179085" MODIFIED="1718037484514" TEXT="Bug-1: storageBytes f&#xe4;lschlich als buffSiz verwendet">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
storageBytes wird nur in der Factory ben&#246;tigt und ist die Gr&#246;&#223;e der gesamten Allokation incl Admin-Overhead
</p>
</body>
</html>
</richcontent>
<icon BUILTIN="broken-line"/>
</node>
<node COLOR="#338800" CREATED="1718037328595" ID="ID_1441998477" MODIFIED="1718037494401" TEXT="Bug-1b: der gleiche Denkfehler steckt auch im Destruktor">
<icon BUILTIN="clanbomber"/>
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#435e98" CREATED="1718036083452" ID="ID_1947157945" MODIFIED="1718037527996" TEXT="problematisch: Element-dtor bekommt Vorrang vor trivialem dtor">
<richcontent TYPE="NOTE"><html>
<head>
</head>
<body>
<p>
....und das wird praktisch immer greifen, wenn Element-Typ und Interface-Typ identisch sind; es steht zu bef&#252;rchten da&#223; deshalb der <i>triviale destruktur praktisch nie zum Tragen kommt. </i>Anders herum best&#252;nde auch keine Gefahr, da&#223; ein erstes Element, das auch ein Subtyp sein k&#246;nnte, eine Entscheidung f&#252;r TRIVIAL f&#228;llen w&#252;rde, denn der ELEMENT-Fall fordert ja grade, da&#223; alle Elemente den gleichen Typ haben; also w&#228;re eine solche Festlegung auch in diesem Fall sogar vorteilhaft, da sie den Destruktor-Aufruf einspart
</p>
</body>
</html>
</richcontent>
<icon BUILTIN="messagebox_warning"/>
</node>
<node COLOR="#435e98" CREATED="1718037202167" ID="ID_1048518309" MODIFIED="1718037603875" TEXT="Bug-2: Code f&#xfc;r front() und back() vertauscht (und verwendet falsches Meta-Feld)">
<icon BUILTIN="broken-line"/>
</node>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#fdfdcf" COLOR="#ff0000" CREATED="1718037991596" ID="ID_1929820397" MODIFIED="1718037999077" TEXT="Iteration">
<icon BUILTIN="flag-pink"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716857382441" ID="ID_748929457" MODIFIED="1716857402102" TEXT="check_Builder">