2019-04-14 15:38:57 +02:00
|
|
|
|
/*
|
|
|
|
|
|
VerbVisitorDispatch(Test) - Setup to dispatch to arbitrary functions on a receiver interface
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2019, Hermann Vosseler <Ichthyostega@web.de>
|
2019-04-14 15:38:57 +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.
|
2019-04-14 15:38:57 +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
|
|
|
|
* *****************************************************************/
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
/** @file verb-visitor-dispatch-test.cpp
|
|
|
|
|
|
** Demonstrate the extended concept of a _verb language_ based on double dispatch.
|
|
|
|
|
|
** @see body-canvas-widget.hpp
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/verb-visitor.hpp"
|
|
|
|
|
|
#include "lib/format-string.hpp"
|
|
|
|
|
|
#include "lib/format-cout.hpp"
|
2019-04-17 18:32:21 +02:00
|
|
|
|
#include "lib/format-util.hpp"
|
2019-05-11 18:08:00 +02:00
|
|
|
|
#include "lib/meta/tuple-helper.hpp"
|
2023-06-22 20:23:55 +02:00
|
|
|
|
#include "lib/iter-explorer.hpp"
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
2019-05-11 18:08:00 +02:00
|
|
|
|
#include <tuple>
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
using util::_Fmt;
|
2019-04-17 18:32:21 +02:00
|
|
|
|
using util::join;
|
2019-04-14 15:38:57 +02:00
|
|
|
|
using std::vector;
|
2019-05-11 18:08:00 +02:00
|
|
|
|
using std::forward;
|
|
|
|
|
|
using std::make_tuple;
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
2019-05-11 18:08:00 +02:00
|
|
|
|
namespace { // Test Fixture
|
|
|
|
|
|
|
|
|
|
|
|
/** the "visitor" interface used by all VerbPacks in this test */
|
|
|
|
|
|
class Receiver
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
virtual ~Receiver() { } ///< this is an interface
|
|
|
|
|
|
|
|
|
|
|
|
virtual string woof (bool huge, uint cnt) =0;
|
|
|
|
|
|
virtual string honk (string) =0;
|
|
|
|
|
|
virtual string moo (size_t num) =0;
|
|
|
|
|
|
virtual string meh () =0;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using Token = VerbPack<Receiver, string, sizeof(string)>; // the argument list for honk(string) requires the most inline storage
|
2019-04-17 18:32:21 +02:00
|
|
|
|
using TokenSeq = vector<Token>;
|
2019-05-11 18:08:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* a concrete receiver of verb-tokens,
|
|
|
|
|
|
* which renders them verbosely
|
|
|
|
|
|
*/
|
|
|
|
|
|
class VerboseRenderer
|
|
|
|
|
|
: public Receiver
|
|
|
|
|
|
{
|
|
|
|
|
|
string
|
|
|
|
|
|
woof (bool huge, uint cnt) override
|
|
|
|
|
|
{
|
|
|
|
|
|
string woof;
|
|
|
|
|
|
while (0 < cnt--)
|
|
|
|
|
|
woof += isnil(woof)? string {huge? "Woof..":"haw-haw"}
|
|
|
|
|
|
: woof;
|
|
|
|
|
|
return woof;
|
|
|
|
|
|
}
|
|
|
|
|
|
string
|
|
|
|
|
|
honk (string theHonk) override
|
|
|
|
|
|
{
|
|
|
|
|
|
return theHonk+"-"+theHonk+"!";
|
|
|
|
|
|
}
|
|
|
|
|
|
string
|
|
|
|
|
|
moo (size_t num) override
|
|
|
|
|
|
{
|
|
|
|
|
|
return join (vector<string>{num, "Moo"}, "__");
|
|
|
|
|
|
}
|
|
|
|
|
|
string
|
|
|
|
|
|
meh() override
|
|
|
|
|
|
{
|
|
|
|
|
|
return "Meh?";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* another concrete receiver to report any invocation with arguments
|
|
|
|
|
|
*/
|
|
|
|
|
|
class DiagnosticRenderer
|
|
|
|
|
|
: public Receiver
|
|
|
|
|
|
{
|
|
|
|
|
|
string woof (bool huge, uint cnt) override { return report("woof", huge, cnt); }
|
|
|
|
|
|
string honk (string theHonk) override { return report("honk", theHonk); }
|
|
|
|
|
|
string moo (size_t num) override { return report("moo", num); }
|
|
|
|
|
|
string meh() override { return report("meh"); }
|
|
|
|
|
|
|
|
|
|
|
|
template<typename...ARGS>
|
|
|
|
|
|
string
|
|
|
|
|
|
report (Literal func, ARGS&&...args)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string(func)
|
|
|
|
|
|
+ meta::dump (make_tuple (forward<ARGS>(args)...));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}//(End)Test Fixture
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-17 18:32:21 +02:00
|
|
|
|
|
2019-04-14 15:38:57 +02:00
|
|
|
|
/***********************************************************************//**
|
2019-05-11 18:08:00 +02:00
|
|
|
|
* @test Demonstration/Concept: build pre-bound VerbPack objects to
|
|
|
|
|
|
* embody a specific operation on the "receiver" interface,
|
|
|
|
|
|
* then invoke them on a given concrete implementation.
|
2019-04-14 15:38:57 +02:00
|
|
|
|
*
|
|
|
|
|
|
* @see DiffListApplication_test
|
|
|
|
|
|
*/
|
|
|
|
|
|
class VerbVisitorDispatch_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2019-04-17 18:32:21 +02:00
|
|
|
|
TokenSeq tokens = build_and_copy_tokens();
|
2019-05-11 18:08:00 +02:00
|
|
|
|
apply_VerboseRenderer (tokens);
|
|
|
|
|
|
apply_different_receivers (tokens);
|
|
|
|
|
|
verify_copy_and_equality (tokens);
|
2019-04-14 15:38:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-17 18:32:21 +02:00
|
|
|
|
/** @test verify the correct individual dispatch
|
|
|
|
|
|
* through a computation specific for the given verb
|
|
|
|
|
|
*/
|
|
|
|
|
|
TokenSeq
|
|
|
|
|
|
build_and_copy_tokens ()
|
|
|
|
|
|
{
|
2019-05-11 18:08:00 +02:00
|
|
|
|
Token littleWoof(&Receiver::woof, "woof", 0, 3); // NOTE: argument type conversion
|
|
|
|
|
|
Token bigWoof(&Receiver::woof, "woof", true, 2);
|
|
|
|
|
|
Token quack(&Receiver::honk, "honk", "quaack");
|
|
|
|
|
|
Token honk(&Receiver::honk, "honk", "Hoonk");
|
|
|
|
|
|
Token moo(&Receiver::moo, "moo", 3);
|
2019-04-17 18:32:21 +02:00
|
|
|
|
Token meh(&Receiver::meh, "meh");
|
|
|
|
|
|
|
2019-05-11 18:08:00 +02:00
|
|
|
|
CHECK (sizeof(Token) == sizeof(string) // storage size reserved for the arguments (by declaration of type Token)
|
|
|
|
|
|
+ sizeof(void*) // additional overhead for the VTable for dispatch
|
|
|
|
|
|
+ sizeof(void(Receiver::*)()) // storage for the function pointer to the interface function
|
|
|
|
|
|
+ sizeof(Literal) // storage for the verb symbol (for diagnostics and equality comparisions)
|
|
|
|
|
|
);
|
2019-04-17 18:32:21 +02:00
|
|
|
|
|
2019-05-11 18:08:00 +02:00
|
|
|
|
// add LVal-copies to result sequence
|
|
|
|
|
|
return TokenSeq{{littleWoof, quack, honk, bigWoof, moo, meh}};
|
2019-04-17 18:32:21 +02:00
|
|
|
|
}
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
/** @test demonstrate the dispatching
|
|
|
|
|
|
* based on the concrete verb token.
|
|
|
|
|
|
* Here the implementation just prints
|
|
|
|
|
|
* the name of the invoked verb
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
2019-05-11 18:08:00 +02:00
|
|
|
|
apply_VerboseRenderer (TokenSeq& tokens)
|
2019-04-14 15:38:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
VerboseRenderer receiver;
|
2019-04-21 23:35:57 +02:00
|
|
|
|
for (Token& tok : tokens)
|
2019-04-20 17:27:47 +02:00
|
|
|
|
cout << "dispatching " << tok
|
|
|
|
|
|
<< " -> '"
|
|
|
|
|
|
<< tok.applyTo(receiver)
|
|
|
|
|
|
<< "'\n";
|
2019-04-14 15:38:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-11 18:08:00 +02:00
|
|
|
|
/** @test demonstrate that another receiver
|
|
|
|
|
|
* indeed invokes different implementations
|
|
|
|
|
|
* of the Interface functions embedded within
|
|
|
|
|
|
* the Verb. Here the implementation just prints
|
|
|
|
|
|
* the name of the invoked verb and the arguments.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
apply_different_receivers (TokenSeq& tokens)
|
|
|
|
|
|
{
|
|
|
|
|
|
VerboseRenderer verbose;
|
|
|
|
|
|
DiagnosticRenderer diagnostic;
|
|
|
|
|
|
auto render = [&](Receiver& renderer)
|
|
|
|
|
|
{
|
2023-06-22 20:23:55 +02:00
|
|
|
|
return join (lib::explore(tokens)
|
2019-05-11 18:08:00 +02:00
|
|
|
|
.transform ([&](Token tok)
|
|
|
|
|
|
{
|
|
|
|
|
|
return tok.applyTo (renderer);
|
|
|
|
|
|
})
|
|
|
|
|
|
,"-");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (render(diagnostic) == "woof(false,3)-honk(quaack)-honk(Hoonk)-woof(true,2)-moo(3)-meh()");
|
|
|
|
|
|
CHECK (render(verbose) == "haw-hawhaw-hawhaw-hawhaw-haw-quaack-quaack!-Hoonk-Hoonk!-Woof..Woof..-Moo__Moo__Moo-Meh?");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_copy_and_equality (TokenSeq& tokens)
|
|
|
|
|
|
{
|
|
|
|
|
|
Token bigWoof = tokens[3];
|
|
|
|
|
|
Token oldWoof{&Receiver::woof, "woof", true, 1};
|
|
|
|
|
|
Token oldWolf{&Receiver::woof, "wolf", true, 0};
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (bigWoof == oldWoof);
|
|
|
|
|
|
CHECK (bigWoof != oldWolf);
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (not util::isSameObject (bigWoof, oldWoof));
|
|
|
|
|
|
CHECK (string(bigWoof) == "VerbPack(woof)");
|
|
|
|
|
|
CHECK (string(oldWoof) == "VerbPack(woof)");
|
|
|
|
|
|
CHECK (string(oldWolf) == "VerbPack(wolf)");
|
|
|
|
|
|
|
|
|
|
|
|
VerboseRenderer bark;
|
|
|
|
|
|
CHECK (bigWoof.applyTo(bark) == "Woof..Woof..");
|
|
|
|
|
|
CHECK (oldWoof.applyTo(bark) == "Woof..");
|
|
|
|
|
|
CHECK (oldWolf.applyTo(bark) == "");
|
|
|
|
|
|
}
|
2019-04-14 15:38:57 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (VerbVisitorDispatch_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|