From 27b36f067901422ab4b9f72fa407619aef9a385e Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 28 May 2024 18:07:08 +0200 Subject: [PATCH] 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 --- src/lib/several-builder.hpp | 5 ++++- src/lib/several.hpp | 44 +++++++++++++++++++++++++++++-------- wiki/thinkPad.ichthyo.mm | 25 ++++++++++++++++++++- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/lib/several-builder.hpp b/src/lib/several-builder.hpp index 96f6f95dd..dc96d2c86 100644 --- a/src/lib/several-builder.hpp +++ b/src/lib/several-builder.hpp @@ -46,8 +46,11 @@ #include "lib/several.hpp" #include "include/logging.h" +#include #include + using std::vector; +using std::move; @@ -75,7 +78,7 @@ namespace lib { Several build() { - UNIMPLEMENTED ("materialise into Several-Container"); + return move (*this); } }; diff --git a/src/lib/several.hpp b/src/lib/several.hpp index 743c55a62..364a032e9 100644 --- a/src/lib/several.hpp +++ b/src/lib/several.hpp @@ -57,6 +57,9 @@ namespace lib { Manager manager; size_t spread; + + template + auto access(); }; template @@ -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 (elm)); + } }; + + /** @internal perform an unsafe down-cast to access the storage area */ + template + auto + Bucket::access() + { + using Storage = ArrayBucket; + return static_cast (*this); + } + }//(End)implementation details @@ -80,7 +102,7 @@ namespace lib { */ template 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().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*; diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 5c1656df8..5426aa3f5 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -81846,15 +81846,38 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + - + + + + + + + + +

+ ...weil ich den Zieltyp zum Cast nicht dynamisch konstruieren kann +

+ + +
+ +
+ + + + +