lumiera_/tests/basics/visitingtool-extended-test.cpp

220 lines
6.4 KiB
C++
Raw Normal View History

/*
VisitingToolExtended(Test) - check obscure corner cases of visitor lib implementation
2010-12-17 23:28:49 +01: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)
2008, Hermann Vosseler <Ichthyostega@web.de>
2010-12-17 23:28:49 +01: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.
2010-12-17 23:28:49 +01: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
* *****************************************************************/
/** @file visitingtool-extended-test.cpp
** unit test \ref VisitingToolExtended_test
*/
#include "lib/test/run.hpp"
#include "lib/visitor.hpp"
#include "lib/format-string.hpp"
#include <iostream>
using util::_Fmt;
using std::string;
using std::cout;
2011-12-03 02:56:50 +01:00
namespace lib {
namespace visitor{
namespace test2 {
typedef visitor::Tool<> Tool;
class HomoSapiens : public Visitable<>
{
2011-12-03 02:56:50 +01:00
public:
DEFINE_PROCESSABLE_BY (Tool);
};
class Boss : public HomoSapiens
{
public:
DEFINE_PROCESSABLE_BY (Tool);
};
class BigBoss : public Boss
{
public:
DEFINE_PROCESSABLE_BY (Tool);
};
template<class BASE>
class VerboseVisitor
: public BASE
{
protected:
void talk_to (string guy)
{
cout << _Fmt{"Hello %s, nice to meet you...\n"} % guy;
2011-12-03 02:56:50 +01:00
}
};
class Babbler
: public Applicable< Babbler,
Types<Boss,BigBoss>::List, // treat this types
2011-12-03 02:56:50 +01:00
VerboseVisitor<Tool> // intermediary base class
>
{
public:
void treat (Boss&) { talk_to("Boss"); }
void treat (BigBoss&) { talk_to("Big Boss"); }
};
// the classes above comprise the standard use case,
// what follows covers rather exotic corner cases
2011-12-03 02:56:50 +01:00
/** defines an catch-all-function instead of the silent default error handler */
template<class RET>
struct Catched
{
RET onUnknown (HomoSapiens&) { cout << "we-do-everything-for-YOU!\n"; return RET(); }
};
/** defines another different visiting tool base */
2011-12-03 02:56:50 +01:00
typedef visitor::Tool<void, Catched> Hastalavista;
typedef Visitable<Hastalavista> Chief; ///< another special kind of visitable
2011-12-03 02:56:50 +01:00
#define DEFINE_HASTALAVISTA_PROCESSABLE \
virtual void apply (Hastalavista& tool) \
{ return Chief::dispatchOp (*this, tool); }
/** now mixing the two hierarchies... */
class Leader : public Chief,
public Boss ///< can act as HomoSapiens or as Chief
{
public:
using HomoSapiens::apply;
DEFINE_HASTALAVISTA_PROCESSABLE;
};
class Visionary : public Leader
{
DEFINE_HASTALAVISTA_PROCESSABLE;
};
/** Hastalavista-Visiting-Tool
* tailored for the Chief hierarchy
*/
class Blatherer
: public Applicable< Blatherer,
Types<Visionary>::List, // get calls to Visionary dispatched
VerboseVisitor<Hastalavista> // note: different tool base class
2011-12-03 02:56:50 +01:00
>
{
public:
void treat (Leader&) { talk_to("Mr.Future"); }
};
2011-12-03 02:56:50 +01:00
/*********************************************************************//**
2011-12-03 02:56:50 +01:00
* @test more esoteric corner cases of our visitor lib implementation.
* Defines a hierarchy of test classes, which mix two different
* kinds of "visitable" by two disjoint tool base classes. One
* of these base classes uses an explicit error handling
* catch-all-function.
*/
class VisitingToolExtended_test : public Test
{
virtual void
run(Arg)
{
2011-12-03 02:56:50 +01:00
known_visitor_known_class();
visitor_not_visiting_some_class();
visiting_mixed_hierarchy();
}
2011-12-03 02:56:50 +01:00
void known_visitor_known_class()
{
2011-12-03 02:56:50 +01:00
Boss x1;
BigBoss x2;
// masquerade as HomoSapiens...
HomoSapiens& homo1 (x1);
HomoSapiens& homo2 (x2);
cout << "=== Babbler meets Boss and BigBoss ===\n";
Babbler bab;
homo1.apply (bab);
homo2.apply (bab);
}
2011-12-03 02:56:50 +01:00
void visitor_not_visiting_some_class()
{
2011-12-03 02:56:50 +01:00
HomoSapiens x1;
Leader x2;
HomoSapiens& homo1 (x1);
HomoSapiens& homo2 (x2);
cout << "=== Babbler meets HomoSapiens and Leader ===\n";
Babbler bab;
homo1.apply (bab); // doesn't visit HomoSapiens
homo2.apply (bab); // treats Leader as Boss
}
void visiting_mixed_hierarchy()
{
2011-12-03 02:56:50 +01:00
Leader x1;
Visionary x2;
2011-12-03 02:56:50 +01:00
HomoSapiens& homo1 (x1);
HomoSapiens& homo2 (x2);
Chief& chief1 (x1);
Chief& chief2 (x2);
Leader& lead1 (x1);
Leader& lead2 (x2);
2011-12-03 02:56:50 +01:00
Blatherer bla;
cout << "=== Blatherer meets Leader and Visionary masqueraded as Chief ===\n";
chief1.apply (bla); // catch-all, because Blatherer doesn't declare to be applicalbe to Leader
chief2.apply (bla); // treat(Visionary&) resolved to treat(Leader&) as expected
Babbler bab;
Tool& tool1 (bab);
cout << "=== Babbler masqueraded as Tool meets Leader and Visionary masqueraded as HomoSapiens ===\n";
homo1.apply (tool1); // because just going through the VTable, the dispatch works as expected
2011-12-03 02:56:50 +01:00
homo2.apply (tool1); // same here (in both cases, the call is resolved to treat(Boss&) as expected)
cout << "=== Babbler masqueraded as Tool meets Leader and Visionary masqueraded as Leader ===\n";
lead1.apply (tool1); // nothing happens, because Leader here is treated by his HomoSapiens base
lead2.apply (tool1); // surprisingly the VTable mechanism is choosen here, resulting in an correct dispatch
// note: the following doesn't compile (an this is a feature, not a bug):
// "Chief chief" : is abstract, because the Visitable-Template enforces implementing
// the "apply(TOOL&)" function, either directly or via the
2011-12-03 02:56:50 +01:00
// DEFINE_PROCESSABLE_BY macro
}
};
/** Register this test class... */
LAUNCHER (VisitingToolExtended_test, "unit common");
}}} // namespace lib::visitor::test2