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
113 lines
3.4 KiB
C++
113 lines
3.4 KiB
C++
/*
|
||
FunTrampoline(Test) - demonstrate building a static dispatcher table
|
||
|
||
Copyright (C)
|
||
2026, Hermann Vosseler <Ichthyostega@web.de>
|
||
|
||
**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.
|
||
|
||
* *****************************************************************/
|
||
|
||
/** @file fun-trampoline-test.cpp
|
||
** unit test \ref FunTrampoline_test
|
||
*/
|
||
|
||
|
||
|
||
#include "test/run.hpp"
|
||
#include "test/test-helper.hpp"
|
||
#include "lib/meta/tuple-helper.hpp"
|
||
#include "lib/format-string.hpp"
|
||
#include "lib/format-obj.hpp"
|
||
|
||
#include "lib/meta/fun-trampoline.hpp"
|
||
|
||
|
||
using test::showType;
|
||
using std::operator""s;
|
||
using std::string;
|
||
using util::_Fmt;
|
||
|
||
|
||
namespace lib {
|
||
namespace meta {
|
||
namespace test {
|
||
|
||
namespace { // Builder to populate the trampoline table
|
||
|
||
template<typeseq SEQ>
|
||
struct Stringify
|
||
{
|
||
using Tuple = ElmTypes<SEQ>::Tup;
|
||
using Functor = string(*)(Tuple const&);
|
||
|
||
template<size_t idx>
|
||
constexpr auto
|
||
build()
|
||
{
|
||
using ElmT = std::tuple_element_t<idx, Tuple>;
|
||
|
||
return [](Tuple const& tuple) -> string
|
||
{
|
||
static _Fmt render{"%02d: «%s»%|20t||%s"};
|
||
return {render % idx
|
||
% util::showType<ElmT>()
|
||
% util::toString<ElmT> (getElm<idx> (tuple))};
|
||
};
|
||
}
|
||
};
|
||
}
|
||
|
||
|
||
|
||
/*************************************************************//**
|
||
* @test demonstrate the setup of a static functor trampoline.
|
||
* @see TupleIdxAdaptor_test
|
||
*/
|
||
class FunTrampoline_test : public Test
|
||
{
|
||
virtual void
|
||
run (Arg)
|
||
{
|
||
demonstrate_TupleAccessor();
|
||
}
|
||
|
||
|
||
|
||
/** @test build a trampoline table to access and handle tuple fields
|
||
* - [setup a builder](\ref Stringify) to populate the table.
|
||
* - use a typedef to provide the actual tuple type
|
||
* - without further ado, the trampoline is ready
|
||
*/
|
||
void
|
||
demonstrate_TupleAccessor()
|
||
{
|
||
auto tup = std::make_tuple (1, 2.3, '4', "⁵₅⁵", "😈"s);
|
||
|
||
using Tramp = TupStringer<decltype(tup)>;
|
||
CHECK (Tramp::size() == 5);
|
||
CHECK (Tramp::memSiz() == 5 * sizeof(void*));
|
||
|
||
CHECK (showType<Tramp::ResType>() == "string"_expect);
|
||
|
||
CHECK (Tramp::dispatch(0)(tup) == "00: «int» |1"_expect );
|
||
CHECK (Tramp::dispatch(1)(tup) == "01: «double» |2.3"_expect);
|
||
CHECK (Tramp::dispatch(2)(tup) == "02: «char» |4"_expect );
|
||
CHECK (Tramp::dispatch(3)(tup) == "03: «const char *»|⁵₅⁵"_expect);
|
||
CHECK (Tramp::dispatch(4)(tup) == "04: «string» |😈"_expect );
|
||
|
||
getElm<3>(tup) = "🚀💣🔥";
|
||
CHECK (Tramp::dispatch(3)(tup) == "03: «const char *»|🚀💣🔥"_expect);
|
||
}
|
||
|
||
template<tup Tup>
|
||
using TupStringer = FunTrampoline<Stringify, typename ElmTypes<Tup>::Seq>;
|
||
};
|
||
|
||
LAUNCHER (FunTrampoline_test, "unit common");
|
||
|
||
|
||
}}} // namespace lib::meta::test
|