Library: implement access to storage and subscript
...not sure if this approach works out OK, since we can not make a safe downcast to a size known at runtime
This commit is contained in:
parent
f6e4358259
commit
27b36f0679
3 changed files with 63 additions and 11 deletions
|
|
@ -46,8 +46,11 @@
|
|||
#include "lib/several.hpp"
|
||||
#include "include/logging.h"
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
using std::move;
|
||||
|
||||
|
||||
|
||||
|
|
@ -75,7 +78,7 @@ namespace lib {
|
|||
Several<I>
|
||||
build()
|
||||
{
|
||||
UNIMPLEMENTED ("materialise into Several-Container");
|
||||
return move (*this);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -57,6 +57,9 @@ namespace lib {
|
|||
|
||||
Manager manager;
|
||||
size_t spread;
|
||||
|
||||
template<class I>
|
||||
auto access();
|
||||
};
|
||||
|
||||
template<class I, size_t bytes>
|
||||
|
|
@ -65,8 +68,27 @@ namespace lib {
|
|||
{
|
||||
alignas(I)
|
||||
std::byte storage[bytes];
|
||||
|
||||
I&
|
||||
subscript (size_t idx)
|
||||
{
|
||||
std::byte* elm = storage;
|
||||
size_t off = idx * spread;
|
||||
elm += off;
|
||||
return * std::launder (reinterpret_cast<I*> (elm));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @internal perform an unsafe down-cast to access the storage area */
|
||||
template<class I>
|
||||
auto
|
||||
Bucket::access()
|
||||
{
|
||||
using Storage = ArrayBucket<I, sizeof(I)>;
|
||||
return static_cast<Storage&> (*this);
|
||||
}
|
||||
|
||||
}//(End)implementation details
|
||||
|
||||
|
||||
|
|
@ -80,7 +102,7 @@ namespace lib {
|
|||
*/
|
||||
template<class I>
|
||||
class Several
|
||||
: util::MoveOnly
|
||||
: util::MoveAssign
|
||||
{
|
||||
protected:
|
||||
size_t size_{0};
|
||||
|
|
@ -91,20 +113,24 @@ namespace lib {
|
|||
size_t
|
||||
size() const
|
||||
{
|
||||
UNIMPLEMENTED ("determine storage size");
|
||||
return size_;
|
||||
}
|
||||
|
||||
bool
|
||||
empty() const
|
||||
{
|
||||
return not (size_ and data_);
|
||||
}
|
||||
|
||||
I&
|
||||
operator[] (size_t i)
|
||||
operator[] (size_t idx)
|
||||
{
|
||||
UNIMPLEMENTED ("subscript");
|
||||
REQUIRE (data_);
|
||||
return data_->access<I>().subscript (idx);
|
||||
}
|
||||
|
||||
I&
|
||||
back()
|
||||
{
|
||||
UNIMPLEMENTED ("storage access");
|
||||
}
|
||||
I& front() { return operator[] (size_-1); }
|
||||
I& back() { return operator[] (0); }
|
||||
|
||||
using iterator = I*;
|
||||
using const_iterator = I const*;
|
||||
|
|
|
|||
|
|
@ -81846,15 +81846,38 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716907249314" ID="ID_1711986050" MODIFIED="1716907418033" TEXT="Bucket">
|
||||
<node CREATED="1716907252393" ID="ID_198747165" MODIFIED="1716907259884" TEXT="der Descriptor"/>
|
||||
<node CREATED="1716907260424" ID="ID_1362772717" MODIFIED="1716907268555" TEXT="liegt am Anfang der Element-Storage"/>
|
||||
<node CREATED="1716912140198" ID="ID_9984568" MODIFIED="1716912197368" TEXT="ermöglicht beliebigen Down-cast auf gegebene Storage-Größe">
|
||||
<linktarget COLOR="#a9b4c1" DESTINATION="ID_9984568" ENDARROW="Default" ENDINCLINATION="97;4;" ID="Arrow_ID_396222637" SOURCE="ID_581123494" STARTARROW="None" STARTINCLINATION="0;-15;"/>
|
||||
</node>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1716906944780" ID="ID_1643896091" MODIFIED="1716907418033" TEXT="ArrayBucket<I,bytes>">
|
||||
<node CREATED="1716907277198" ID="ID_104268426" MODIFIED="1716907281769" TEXT="Subklasse von Bucket"/>
|
||||
<node CREATED="1716907312241" ID="ID_835337350" MODIFIED="1716907333394" TEXT="umspannt das Storage-Array"/>
|
||||
<node CREATED="1716907356091" ID="ID_1876919425" MODIFIED="1716907403909" TEXT="enthält den subscript-Code"/>
|
||||
<node COLOR="#338800" CREATED="1716907356091" ID="ID_1876919425" MODIFIED="1716912200686" TEXT="enthält den subscript-Code">
|
||||
<icon BUILTIN="button_ok"/>
|
||||
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1716912163674" ID="ID_581123494" MODIFIED="1716912308218" TEXT="muß hier einen offenen Downcast machen">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
<head>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
...weil ich den Zieltyp zum Cast nicht dynamisch konstruieren kann
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
</richcontent>
|
||||
<arrowlink DESTINATION="ID_9984568" ENDARROW="Default" ENDINCLINATION="97;4;" ID="Arrow_ID_396222637" STARTARROW="None" STARTINCLINATION="0;-15;"/>
|
||||
<icon BUILTIN="messagebox_warning"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node CREATED="1716909012702" ID="ID_1876994172" MODIFIED="1716909016713" TEXT="const-correctness">
|
||||
<node CREATED="1716909019405" ID="ID_1406909807" MODIFIED="1716909042294" TEXT="ein const Several<X> ist ein Several<const X>"/>
|
||||
</node>
|
||||
</node>
|
||||
</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">
|
||||
|
|
|
|||
Loading…
Reference in a new issue