Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
visitingtool-test.cpp
Go to the documentation of this file.
1/*
2 VisitingTool(Test) - check the standard visitor use case
3
4 Copyright (C)
5 2008, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
20#include "lib/visitor.hpp"
21#include "lib/format-string.hpp"
22
23#include <iostream>
24
25using util::_Fmt;
26using std::string;
27using std::cout;
28
29
30namespace lib {
31namespace visitor {
32namespace test1 {
33
35
36 class HomoSapiens : public Visitable<>
37 {
38 public:
40 };
41
42 class Boss : public HomoSapiens
43 {
44 public:
46 };
47
48 class BigBoss : public Boss
49 {
50 public:
52 };
53
58
59 class Leader : public Visionary
60 {
61 /* can not be visited */
62 };
63
64
65
67 : public VisitingTool
68 {
69 protected:
70 void talk_to (string guy)
71 {
72 cout << _Fmt{"Hello %s, nice to meet you...\n"} % guy;
73 }
74 };
75
76 class Babbler
77 : public Applicable< Babbler
78 , Types<Boss,BigBoss,Visionary>::List // dispatch calls to this types
79 , VerboseVisitor // (base class / interface)
80 >
81 {
82 public:
83 void treat (Boss&) { talk_to("Boss"); }
84 void treat (BigBoss&) { talk_to("Big Boss"); }
85 };
86
87 // note the following fine points:
88 // - Babbler "forgot" to declare being applicable to HomoSapiens
89 // - the class Leader hidden deep in the hierarchy is lacking an "apply()"-implementation
90
91
92
93
94 /*********************************************************************/
104 class VisitingTool_test : public Test
105 {
106 virtual void
112
113 void
115 {
116 Boss x1;
117 BigBoss x2;
118
119 // masquerade as HomoSapiens...
120 HomoSapiens& homo1{x1};
121 HomoSapiens& homo2{x2};
122
123 cout << "=== Babbler meets Boss and BigBoss ===\n";
124 Babbler bab;
125 VisitingTool& vista{bab};
126 homo1.apply (vista);
127 homo2.apply (vista);
128 }
129
130 void
132 {
133 HomoSapiens x1;
134 Leader x2;
135
136 HomoSapiens& homo1 (x1);
137 HomoSapiens& homo2 (x2);
138
139 cout << "=== Babbler meets HomoSapiens and Leader ===\n";
140 Babbler bab;
141 VisitingTool& vista (bab);
142 homo1.apply (vista); // silent error handler (not Applicable to HomoSapiens)
143 homo2.apply (vista); // Leader handled as Visionary and treated as Boss
144 }
145 };
146
147
149 LAUNCHER (VisitingTool_test, "unit common");
150
151
152
153}}} // namespace lib::visitor::test1
Marker template to declare that some "visiting tool" wants to treat a set of concrete Visitable class...
Definition visitor.hpp:135
Marker interface / base class for all "visiting tools".
Definition visitor.hpp:99
Marker interface or base class for all "Visitables".
Definition visitor.hpp:189
virtual ReturnType apply(TOOL &)=0
to be defined by the DEFINE_PROCESSABLE_BY macro in all classes wanting to be treated by some tool
DEFINE_PROCESSABLE_BY(VisitingTool)
DEFINE_PROCESSABLE_BY(VisitingTool)
A front-end for using printf-style formatting.
Front-end for printf-style string template interpolation.
Implementation namespace for support and library code.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A library implementation of the Visitor Pattern tailored specifically to Lumiera's needs within the S...