Library: consider to align this with the »Factory« concept
even while at first sight only a ''deleter instance'' is required, it seems prudent to rearrange the code in accordance to the prospective Allocator / Object Factory concept, and rather try to incorporate the specifics of the memory layout into this generic view, thereby abstracting the actual allocator away. This can be achieved by using a standard-allocator for `std::byte` as the base allocator and treat each individual element allocator as a specialised cross-allocator (assuming that this cross adaptation is actually trivial in almost all cases)
This commit is contained in:
parent
bf74ba6292
commit
0e2ca6ee1c
2 changed files with 31 additions and 13 deletions
|
|
@ -73,36 +73,44 @@ namespace lib {
|
|||
|
||||
|
||||
|
||||
template<class I, class E>
|
||||
struct DeleterTrampoline
|
||||
template<class I, template<typename> class ALO>
|
||||
struct ElementFactory
|
||||
: private ALO<std::byte>
|
||||
{
|
||||
using Allo = std::allocator<E>;
|
||||
using Allo = ALO<std::byte>;
|
||||
using AlloT = std::allocator_traits<Allo>;
|
||||
|
||||
Allo& baseAllocator() { return *this; }
|
||||
|
||||
template<typename X>
|
||||
static auto
|
||||
auto
|
||||
adaptAllocator()
|
||||
{
|
||||
using XAllo = typename AlloT::template rebind_alloc<X>;
|
||||
if constexpr (std::is_constructible_v<XAllo, Allo>)
|
||||
return XAllo{Allo()};
|
||||
return XAllo{baseAllocator()};
|
||||
else
|
||||
return XAllo{};
|
||||
}
|
||||
|
||||
static void
|
||||
ElementFactory (Allo allo = Allo{})
|
||||
: Allo{std::move (allo)}
|
||||
{ }
|
||||
|
||||
template<class E>
|
||||
void
|
||||
destroy (ArrayBucket<I>* bucket, size_t size)
|
||||
{
|
||||
Allo allo{};
|
||||
REQUIRE (bucket);
|
||||
using ElmAlloT = typename AlloT::template rebind_traits<E>;
|
||||
auto elmAllo = adaptAllocator<E>();
|
||||
for (size_t i=0; i<size; ++i)
|
||||
AlloT::destroy (allo, & bucket->subscript(i));
|
||||
ElmAlloT::destroy (elmAllo, & bucket->subscript(i));
|
||||
|
||||
size_t storageBytes = sizeof(ArrayBucket<I>) - sizeof(ArrayBucket<I>::storage)
|
||||
+ size * sizeof(E);
|
||||
+ size * bucket->spread;
|
||||
|
||||
using BAlloT = typename AlloT::template rebind_traits<std::byte>;
|
||||
auto bAllo = adaptAllocator<std::byte>();
|
||||
BAlloT::deallocate (bAllo, bucket, storageBytes);
|
||||
AlloT::deallocate (baseAllocator(), bucket, storageBytes);
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -81975,6 +81975,11 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
<icon BUILTIN="idea"/>
|
||||
</node>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717710799094" ID="ID_310099304" MODIFIED="1717710881440" TEXT="ObjectFactory">
|
||||
<linktarget COLOR="#867587" DESTINATION="ID_310099304" ENDARROW="Default" ENDINCLINATION="-927;146;" ID="Arrow_ID_1881268863" SOURCE="ID_342640598" STARTARROW="None" STARTINCLINATION="-107;-6;"/>
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
<node CREATED="1717710834678" ID="ID_16117383" MODIFIED="1717710852024" TEXT="eine Variation der generischen StdFactory"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
|
|
@ -82231,7 +82236,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
<node CREATED="1717632444817" ID="ID_362040312" MODIFIED="1717632448869" TEXT="Speicher freigeben"/>
|
||||
</node>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717632477490" ID="ID_83921320" MODIFIED="1717632530750" TEXT="beide Aufgaben müssen in das Trampolin">
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717632477490" ID="ID_83921320" MODIFIED="1717710899555" TEXT="beide Aufgaben müssen in das Trampolin">
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
<node CREATED="1717632816553" ID="ID_719956571" MODIFIED="1717632856947" TEXT="geht leider nicht anders...">
|
||||
<richcontent TYPE="NOTE"><html>
|
||||
|
|
@ -82251,6 +82256,11 @@ Date:   Thu Apr 20 18:53:17 2023 +0200<br/>
|
|||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1717710755310" ID="ID_342640598" MODIFIED="1717710895137" TEXT="sinnvollerweise mit einer Allocator-Factory hinterlegen">
|
||||
<arrowlink COLOR="#867587" DESTINATION="ID_310099304" ENDARROW="Default" ENDINCLINATION="-927;146;" ID="Arrow_ID_1881268863" STARTARROW="None" STARTINCLINATION="-107;-6;"/>
|
||||
<icon BUILTIN="flag-yellow"/>
|
||||
<node CREATED="1717710901355" ID="ID_718524980" MODIFIED="1717710916991" TEXT="das Meiste ist nämlich generische boilerplate"/>
|
||||
</node>
|
||||
</node>
|
||||
<node CREATED="1717543083078" ID="ID_530261806" MODIFIED="1717543114805" TEXT="getMover()"/>
|
||||
</node>
|
||||
|
|
|
|||
Loading…
Reference in a new issue