2019-04-14 15:38:57 +02:00
|
|
|
/*
|
|
|
|
|
VerbVisitorDispatch(Test) - Setup to dispatch to arbitrary functions on a receiver interface
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2019, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
/** @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-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
2019-04-20 22:23:55 +02:00
|
|
|
///////////////////////////TODO : Debugging
|
|
|
|
|
struct Trackr
|
|
|
|
|
{
|
|
|
|
|
size_t num;
|
|
|
|
|
|
|
|
|
|
Trackr (size_t val)
|
|
|
|
|
: num(val)
|
|
|
|
|
{
|
|
|
|
|
cout <<"Trackr("<<val<<")"<<endl;
|
|
|
|
|
}
|
|
|
|
|
~Trackr()
|
|
|
|
|
{
|
|
|
|
|
cout <<"~Trackr()"<<endl;
|
|
|
|
|
}
|
|
|
|
|
Trackr (Trackr const& lval)
|
|
|
|
|
: num(lval.num)
|
|
|
|
|
{
|
|
|
|
|
cout <<"Trackr()<<-LVal"<<endl;
|
|
|
|
|
}
|
|
|
|
|
Trackr (Trackr && rval)
|
|
|
|
|
: num(rval.num)
|
|
|
|
|
{
|
|
|
|
|
cout <<"Trackr()<<-RVal"<<endl;
|
|
|
|
|
}
|
|
|
|
|
Trackr&
|
|
|
|
|
operator= (Trackr const& orig)
|
|
|
|
|
{
|
|
|
|
|
cout <<"Tracker = orig"<<endl;
|
|
|
|
|
num = orig.num;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
///////////////////////////TODO : Debugging
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Receiver
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Receiver() { } ///< this is an interface
|
|
|
|
|
|
2019-04-17 18:32:21 +02:00
|
|
|
virtual string woof (bool huge, uint cnt) =0;
|
|
|
|
|
virtual string honk (string) =0;
|
2019-04-20 22:23:55 +02:00
|
|
|
virtual string moo (Trackr num) =0;
|
2019-04-17 18:32:21 +02:00
|
|
|
virtual string meh () =0;
|
2019-04-14 15:38:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace {
|
2019-04-17 18:32:21 +02:00
|
|
|
using Token = VerbPack<Receiver, string, sizeof(string)>;
|
|
|
|
|
using TokenSeq = vector<Token>;
|
2019-04-14 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* a receiver of verb-tokens,
|
|
|
|
|
* which renders them verbosely
|
|
|
|
|
*/
|
|
|
|
|
class VerboseRenderer
|
|
|
|
|
: public Receiver
|
|
|
|
|
{
|
|
|
|
|
string
|
2019-04-17 18:32:21 +02:00
|
|
|
woof (bool huge, uint cnt) override
|
2019-04-14 15:38:57 +02:00
|
|
|
{
|
2019-04-17 18:32:21 +02:00
|
|
|
string woof{huge? "Woof..":"haw-haw"};
|
|
|
|
|
while (0 < cnt--)
|
|
|
|
|
woof += woof;
|
|
|
|
|
return woof;
|
|
|
|
|
}
|
|
|
|
|
string
|
|
|
|
|
honk (string theHonk) override
|
|
|
|
|
{
|
|
|
|
|
return theHonk+"-"+theHonk+"!";
|
|
|
|
|
}
|
|
|
|
|
string
|
2019-04-20 22:23:55 +02:00
|
|
|
moo (Trackr num) override
|
2019-04-17 18:32:21 +02:00
|
|
|
{
|
2019-04-20 22:23:55 +02:00
|
|
|
return join (vector<string>{num.num, "Moo"}, "__");
|
2019-04-17 18:32:21 +02:00
|
|
|
}
|
|
|
|
|
string
|
|
|
|
|
meh() override
|
|
|
|
|
{
|
|
|
|
|
return "Meh!";
|
2019-04-14 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-10 02:19:01 +02:00
|
|
|
#define SHOW_TYPE(_TY_) \
|
|
|
|
|
cout << "typeof( " << STRINGIFY(_TY_) << " )= " << lib::meta::typeStr<_TY_>() <<endl;
|
|
|
|
|
#define SHOW_EXPR(_XX_) \
|
|
|
|
|
cout << "Probe " << STRINGIFY(_XX_) << " ? = " << _XX_ <<endl;
|
2019-04-14 15:38:57 +02:00
|
|
|
|
|
|
|
|
|
2019-04-17 18:32:21 +02:00
|
|
|
|
2019-04-14 15:38:57 +02:00
|
|
|
/***********************************************************************//**
|
|
|
|
|
* @test Demonstration/Concept: dispatch a specific function
|
|
|
|
|
* based on the given verbs of an embedded custom language.
|
|
|
|
|
* Actually what we want to achieve here is a specific form
|
|
|
|
|
* of double dispatch; thus the implementation relies on a
|
|
|
|
|
* variation of the visitor pattern.
|
|
|
|
|
*
|
|
|
|
|
* @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-04-14 15:38:57 +02:00
|
|
|
render_verbose (tokens);
|
2019-04-16 18:21:51 +02:00
|
|
|
// profile.append_woof(1, 2);
|
2019-05-10 02:19:01 +02:00
|
|
|
|
|
|
|
|
using IrrelvantType = struct{};
|
|
|
|
|
const size_t VERB_TOK_SIZE = sizeof(lib::VerbToken<IrrelvantType, void(void)>);
|
|
|
|
|
using HoldL = Holder<Receiver, string(string)>;
|
|
|
|
|
const size_t VERB_TOK_SIZ2 = sizeof(HoldL::Verb);
|
|
|
|
|
const size_t HOLDER_SIZE = sizeof(HoldL);
|
|
|
|
|
const size_t ARG_TUP_SIZE = sizeof(std::tuple<string>);
|
|
|
|
|
|
|
|
|
|
SHOW_EXPR (VERB_TOK_SIZE);
|
|
|
|
|
SHOW_EXPR (VERB_TOK_SIZ2);
|
|
|
|
|
SHOW_EXPR (HOLDER_SIZE);
|
|
|
|
|
SHOW_EXPR (ARG_TUP_SIZE);
|
|
|
|
|
SHOW_EXPR (sizeof(string));
|
|
|
|
|
using AdapT = Token::Adapter<HoldL>;
|
|
|
|
|
SHOW_TYPE (AdapT);
|
|
|
|
|
SHOW_EXPR (sizeof(AdapT));
|
2019-04-14 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** prepare a sequence of verbs
|
|
|
|
|
* for the actual tests to work on */
|
2019-04-17 18:32:21 +02:00
|
|
|
/* VerbSeq
|
2019-04-14 15:38:57 +02:00
|
|
|
build_test_feed()
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
VERB_woof,
|
|
|
|
|
VERB_honk,
|
|
|
|
|
VERB_moo,
|
|
|
|
|
VERB_meh
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-04-17 18:32:21 +02:00
|
|
|
*/
|
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 ()
|
|
|
|
|
{
|
|
|
|
|
Token littleWoof(&Receiver::woof, "woof", false, 3u);
|
2019-05-09 23:40:47 +02:00
|
|
|
Token bigWoof(&Receiver::woof, "woof", true, 2u);
|
2019-04-17 18:32:21 +02:00
|
|
|
Token quack(&Receiver::honk, "honk", string{"quaack"});
|
|
|
|
|
Token honk(&Receiver::honk, "honk", string{"Hoonk"});
|
2019-04-20 22:23:55 +02:00
|
|
|
Token moo(&Receiver::moo, "moo", Trackr(3));
|
2019-04-17 18:32:21 +02:00
|
|
|
Token meh(&Receiver::meh, "meh");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return TokenSeq{{littleWoof, quack,honk, bigWoof, moo, meh}};
|
|
|
|
|
}
|
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-04-17 18:32:21 +02:00
|
|
|
render_verbose (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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (VerbVisitorDispatch_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|