From 659441fa88d31668c248e23451ca58e2a2096978 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 25 Nov 2023 19:22:10 +0100 Subject: [PATCH] Chain-Load: verify (and bugfix) --- src/lib/lazy-init.hpp | 6 +- tests/library/lazy-init-test.cpp | 50 ++++++-- wiki/thinkPad.ichthyo.mm | 209 +++++++++++++++++++++++++++---- 3 files changed, 234 insertions(+), 31 deletions(-) diff --git a/src/lib/lazy-init.hpp b/src/lib/lazy-init.hpp index e8d6e296d..a51a61a33 100644 --- a/src/lib/lazy-init.hpp +++ b/src/lib/lazy-init.hpp @@ -260,8 +260,10 @@ namespace lib { template LazyInit (std::function& targetFunctor, INI&& initialiser, ARGS&& ...parentCtorArgs) : PAR(forward (parentCtorArgs)...) - , pendingInit_{prepareInitialiser (targetFunctor, forward (initialiser))} - { } + , pendingInit_{} + { + installInitialiser (targetFunctor, forward (initialiser)); + } LazyInit (LazyInit const& ref) diff --git a/tests/library/lazy-init-test.cpp b/tests/library/lazy-init-test.cpp index da13eb8f6..34a036839 100644 --- a/tests/library/lazy-init-test.cpp +++ b/tests/library/lazy-init-test.cpp @@ -340,13 +340,22 @@ namespace test{ { return [theFun = forward (fun2install)] (LazyDemo* self) - { + { // this runs when init is actually performed.... CHECK (self); - self->fun = [self, chain = move(theFun)] - (int i) - { - return chain (i + self->seed); // Note: binding to actual instance location - }; + if (self->fun) + // chain-up behind existing function + self->fun = [self, prevFun = move(self->fun), nextFun = move(theFun)] + (int i) + { + return nextFun (prevFun (i)); + }; + else + // build new function chain, inject seed from object + self->fun = [self, newFun = move(theFun)] + (int i) + { + return newFun (i + self->seed); // Note: binding to actual instance location + }; }; } @@ -359,12 +368,20 @@ namespace test{ } template - LazyDemo(FUN&& someFun) + LazyDemo (FUN&& someFun) : LazyInit{MarkDisabled()} , fun{} { installInitialiser(fun, buildInit (forward (someFun))); } + + template + LazyDemo&& + attach (FUN&& someFun) + { + installInitialiser(fun, buildInit (forward (someFun))); + return move(*this); + } }; /** @@ -373,7 +390,9 @@ namespace test{ * - the initialisation routine _adapts_ this function and links it with the current * object location; thus, invoking this function on a copy would crash / corrupt memory. * - however, as long as initialisation has not been triggered, LazyDemo instances can be - * copied; they may even be assigned to existing instances, overwriting their state. + * copied; they may even be assigned to existing instances, overwriting their state. + * - a second given function will be chained behind the first one; this happens immediately + * if the first function was already invoked (and this initialised) */ void verify_complexUsageWithCopy() @@ -404,6 +423,21 @@ namespace test{ CHECK (26 == d1.fun(22)); // seed value is picked up dynamically VERIFY_ERROR (LIFECYCLE, LazyDemo dx{move(d1)} ); + + // attach a further function, to be chained-up + d1.attach([](int i) + { + return i / 2; + }); + CHECK (d1.isInit()); + CHECK (d1.seed == 3); + CHECK (12 == d1.fun(21)); // 21+3+1=25 / 2 + CHECK (13 == d1.fun(22)); + CHECK (13 == d1.fun(23)); + d1.seed++; + CHECK (14 == d1.fun(23)); // 23+4+1=28 / 2 + CHECK (14 == d1.fun(24)); + CHECK (15 == d1.fun(25)); } }; diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 89c474778..12d1ec4c9 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -97365,6 +97365,147 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + + + + + + + + + + + + + + + +

+ //          CHECK (1 == invoked); +

+

+ //          CHECK (init); +

+ + +
+
+ + + + + + + + +

+ die neue maybeInvoke() +

+ + +
+ + + + + + + + + + + + + + +

+ previousInit +

+

+ Details:<error reading variable: Cannot access memory at address 0x3000000030> +

+ + +
+
+ + +
+
+ + + + + + + + + + + +

+       __shared_ptr(__shared_ptr&& __r) noexcept +

+

+       : _M_ptr(__r._M_ptr), _M_refcount() +

+

+       { +

+

+ _M_refcount._M_swap(__r._M_refcount); +

+

+ __r._M_ptr = 0; +

+

+       } +

+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

+ ... kein Problem mit der Logik +

+ + +
+ +
+
+
+
@@ -97445,7 +97586,7 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + @@ -97525,6 +97666,9 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + +
@@ -97550,9 +97694,6 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - @@ -97562,6 +97703,18 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + + + + + + + @@ -97639,9 +97792,10 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - + + + + @@ -97659,7 +97813,8 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + + @@ -97669,13 +97824,14 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + + - + @@ -97697,11 +97853,11 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + - + @@ -97723,13 +97879,13 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - - - + + + + - - + + @@ -97784,9 +97940,20 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + + + + + + + + + + + +