2023-10-11 21:06:56 +02:00
|
|
|
|
/*
|
2025-06-07 23:59:57 +02:00
|
|
|
|
LateBindInstance(Test) - verify rewriting of member function invocation
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2023, Hermann Vosseler <Ichthyostega@web.de>
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
|
|
|
|
|
/** @file late-bind-instance-test.cpp
|
|
|
|
|
|
** unit test \ref LateBindInstance_test
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-06 21:34:20 +01:00
|
|
|
|
#include "test/run.hpp"
|
|
|
|
|
|
#include "test/test-helper.hpp"
|
|
|
|
|
|
#include "test/tracking-dummy.hpp"
|
Library: deep dive into type reconciliation and tuple handling
This commit is the aggregated result of extended research
that is only loosely related to current development topics.
One theme was the construction of nested data structures using tuples,
which faces some difficulties due to tuple manipulations being limited
almost entirely to compile-time processing. However, the barrier between
compile-time and runtime can be overcome with the help of a dispatcher
table -- which leads to the secondary problem to handle the diverse
tuple elements with a common, generic functor. Such a functor can be
constructed, if and only if those element types are ''reconcilable''
The STDLIB provides two metafunctions, `std::common_type` and
`std::common_refernce`, which however pose some challenges for the
task at hand, since they include additional processing. In the end,
a custom version of the ''basic type reconciliation'' was implemented
and tested, and then used as foundation to build a dispatcher table
for runtime-indexed access and iteration over tuples.
As an aside, several deeply hidden bugs in our library code were resolved,
and the tuple-helpers, tuple string rendering and associated utilities
were regrouped and modernised.
As result of this research work, the following components are provided:
- meta/common-result
- meta/fun-trampoline
- meta/tuple-index-adaptor
2026-04-07 06:32:23 +02:00
|
|
|
|
#include "lib/meta/tuple-helper.hpp"
|
2023-10-11 21:06:56 +02:00
|
|
|
|
#include "lib/meta/function.hpp"
|
|
|
|
|
|
#include "lib/format-cout.hpp"
|
|
|
|
|
|
#include "lib/format-util.hpp"
|
|
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace meta {
|
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
using std::move;
|
|
|
|
|
|
using std::tuple;
|
|
|
|
|
|
using std::string;
|
Library: deep dive into type reconciliation and tuple handling
This commit is the aggregated result of extended research
that is only loosely related to current development topics.
One theme was the construction of nested data structures using tuples,
which faces some difficulties due to tuple manipulations being limited
almost entirely to compile-time processing. However, the barrier between
compile-time and runtime can be overcome with the help of a dispatcher
table -- which leads to the secondary problem to handle the diverse
tuple elements with a common, generic functor. Such a functor can be
constructed, if and only if those element types are ''reconcilable''
The STDLIB provides two metafunctions, `std::common_type` and
`std::common_refernce`, which however pose some challenges for the
task at hand, since they include additional processing. In the end,
a custom version of the ''basic type reconciliation'' was implemented
and tested, and then used as foundation to build a dispatcher table
for runtime-indexed access and iteration over tuples.
As an aside, several deeply hidden bugs in our library code were resolved,
and the tuple-helpers, tuple string rendering and associated utilities
were regrouped and modernised.
As result of this research work, the following components are provided:
- meta/common-result
- meta/fun-trampoline
- meta/tuple-index-adaptor
2026-04-07 06:32:23 +02:00
|
|
|
|
using lib::meta::joinTupleParen;
|
2026-03-06 21:34:20 +01:00
|
|
|
|
using ::test::Tracker;
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************************//**
|
|
|
|
|
|
* @test verify a scheme to supply the actual instance for member function invocation.
|
|
|
|
|
|
* @remark in the end this is just a value replacement in a tuple, where the position
|
|
|
|
|
|
* is marked with a placeholder type. This scheme is used for starting threads,
|
|
|
|
|
|
* where the actual thread instance is not known yet at the point where the
|
|
|
|
|
|
* arguments for the thread-function are constructed
|
|
|
|
|
|
* @see lib::launchDetached
|
|
|
|
|
|
*/
|
|
|
|
|
|
class LateBindInstance_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
2023-10-11 21:06:56 +02:00
|
|
|
|
demonstrateUsage();
|
|
|
|
|
|
verify_forwarding();
|
|
|
|
|
|
verify_cornerCases();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test demonstrate the usage of lib::meta::lateBindInstance():
|
|
|
|
|
|
* - construct an argument tuple
|
|
|
|
|
|
* - mark the position of the `this`-ptr with a placeholder
|
|
|
|
|
|
* - rewrite the arguments later when the actual instance is known
|
|
|
|
|
|
* - member function can now be invoked, since the instance was injected.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
demonstrateUsage()
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
uint randomLimit = 2 + rani(98);
|
2023-10-11 21:06:56 +02:00
|
|
|
|
|
|
|
|
|
|
auto plannedArgs
|
|
|
|
|
|
= tuple{InstancePlaceholder<LateBindInstance_test>{}
|
|
|
|
|
|
,randomLimit
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// the actual instance may be available only later...
|
|
|
|
|
|
Test& instanceRef = *this;
|
|
|
|
|
|
|
|
|
|
|
|
// now rewrite the argument tuple to inject the instance-ptr
|
|
|
|
|
|
auto preparedArgs = lateBindInstance (instanceRef, plannedArgs);
|
|
|
|
|
|
|
|
|
|
|
|
uint res = std::apply (&LateBindInstance_test::theMember, preparedArgs);
|
|
|
|
|
|
CHECK (res < randomLimit);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint
|
|
|
|
|
|
theMember (uint limit)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
return rani (limit);
|
2023-10-11 21:06:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test verify that the rewriting process does not incur unnecessary data copies
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_forwarding()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& log = Tracker::log;
|
|
|
|
|
|
log.clear (this);
|
|
|
|
|
|
|
|
|
|
|
|
InstancePlaceholder<LateBindInstance_test> marker;
|
|
|
|
|
|
Tracker t1(11);
|
|
|
|
|
|
|
|
|
|
|
|
log.event("construct tuple");
|
|
|
|
|
|
tuple tup(t1, marker, Tracker{23}, 55);
|
|
|
|
|
|
|
|
|
|
|
|
Test& instanceRef = *this;
|
|
|
|
|
|
|
|
|
|
|
|
log.event("invoke lateBindInstance");
|
|
|
|
|
|
auto tup1r = lateBindInstance (instanceRef, tup);
|
|
|
|
|
|
log.event("got result");
|
|
|
|
|
|
|
|
|
|
|
|
// before invocation the Tracker instances are created (obviously..)
|
|
|
|
|
|
CHECK (log.verifyCall("ctor").arg(11)
|
|
|
|
|
|
.beforeCall("ctor-copy").arg("Track{11}")
|
|
|
|
|
|
.beforeEvent("invoke lateBindInstance"));
|
|
|
|
|
|
CHECK (log.verifyCall("ctor").arg(23)
|
|
|
|
|
|
.beforeCall("ctor-move").arg("Track{23}")
|
|
|
|
|
|
.beforeCall("dtor").arg(Tracker::DEFUNCT)
|
|
|
|
|
|
.beforeEvent("invoke lateBindInstance"));
|
|
|
|
|
|
|
|
|
|
|
|
// but there is no copy operation after the invocation
|
|
|
|
|
|
CHECK (log.ensureNot("ctor-copy")
|
|
|
|
|
|
.afterEvent("invoke lateBindInstance"));
|
|
|
|
|
|
|
|
|
|
|
|
// both Tracker instances are moved two times
|
|
|
|
|
|
// - once to create the maybeInject-invocation and
|
|
|
|
|
|
// - once to consolidate the result
|
|
|
|
|
|
// for each instance one moved-away temporary is destroyed
|
|
|
|
|
|
CHECK (log.verifyEvent("invoke lateBindInstance")
|
|
|
|
|
|
.beforeCall("ctor-move").arg("Track{11}")
|
|
|
|
|
|
.beforeCall("ctor-move").arg("Track{11}")
|
|
|
|
|
|
.beforeCall("dtor").arg(Tracker::DEFUNCT)
|
|
|
|
|
|
.beforeEvent("got result"));
|
|
|
|
|
|
CHECK (log.verifyEvent("invoke lateBindInstance")
|
|
|
|
|
|
.beforeCall("ctor-move").arg("Track{23}")
|
|
|
|
|
|
.beforeCall("ctor-move").arg("Track{23}")
|
|
|
|
|
|
.beforeCall("dtor").arg(Tracker::DEFUNCT)
|
|
|
|
|
|
.beforeEvent("got result"));
|
|
|
|
|
|
|
|
|
|
|
|
cout << "____Tracker-Log_______________\n"
|
|
|
|
|
|
<< util::join(Tracker::log, "\n")
|
|
|
|
|
|
<< "\n───╼━━━━━━━━━━━╾──────────────"<<endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-12 02:10:50 +02:00
|
|
|
|
/** @test replacement happens uniformly on all kinds of tuples
|
2023-10-11 21:06:56 +02:00
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_cornerCases()
|
|
|
|
|
|
{
|
|
|
|
|
|
long dummy{555};
|
|
|
|
|
|
InstancePlaceholder<long> marker;
|
Library: deep dive into type reconciliation and tuple handling
This commit is the aggregated result of extended research
that is only loosely related to current development topics.
One theme was the construction of nested data structures using tuples,
which faces some difficulties due to tuple manipulations being limited
almost entirely to compile-time processing. However, the barrier between
compile-time and runtime can be overcome with the help of a dispatcher
table -- which leads to the secondary problem to handle the diverse
tuple elements with a common, generic functor. Such a functor can be
constructed, if and only if those element types are ''reconcilable''
The STDLIB provides two metafunctions, `std::common_type` and
`std::common_refernce`, which however pose some challenges for the
task at hand, since they include additional processing. In the end,
a custom version of the ''basic type reconciliation'' was implemented
and tested, and then used as foundation to build a dispatcher table
for runtime-indexed access and iteration over tuples.
As an aside, several deeply hidden bugs in our library code were resolved,
and the tuple-helpers, tuple string rendering and associated utilities
were regrouped and modernised.
As result of this research work, the following components are provided:
- meta/common-result
- meta/fun-trampoline
- meta/tuple-index-adaptor
2026-04-07 06:32:23 +02:00
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple<>{})) == "()"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{42})) == "(42)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{1,2,3})) == "(1,2,3)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{marker,2,3})) == "(↗555,2,3)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{1,marker,3})) == "(1,↗555,3)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{1,2,marker})) == "(1,2,↗555)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{marker})) == "(↗555)"_expect);
|
|
|
|
|
|
CHECK (joinTupleParen(lateBindInstance (dummy, tuple{string{"1"}
|
|
|
|
|
|
,"2"
|
|
|
|
|
|
,marker
|
|
|
|
|
|
,tuple(2,3)
|
|
|
|
|
|
, 5.5
|
|
|
|
|
|
})) == "(1,2,↗555,«tuple<int, int>»──(2,3),5.5)"_expect);
|
2023-10-11 21:06:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (LateBindInstance_test, "unit meta");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace lib::meta::test
|