Lumiera  0.pre.03
»edit your freedom«
visitingtool-test.cpp
Go to the documentation of this file.
1 /*
2  VisitingTool(Test) - check the standard visitor use case
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/visitor.hpp"
30 #include "lib/format-string.hpp"
31 
32 #include <iostream>
33 
34 using util::_Fmt;
35 using std::string;
36 using std::cout;
37 
38 
39 namespace lib {
40 namespace visitor {
41 namespace test1 {
42 
43  typedef visitor::Tool<> VisitingTool;
44 
45  class HomoSapiens : public Visitable<>
46  {
47  public:
48  DEFINE_PROCESSABLE_BY (VisitingTool);
49  };
50 
51  class Boss : public HomoSapiens
52  {
53  public:
54  DEFINE_PROCESSABLE_BY (VisitingTool);
55  };
56 
57  class BigBoss : public Boss
58  {
59  public:
60  DEFINE_PROCESSABLE_BY (VisitingTool);
61  };
62 
63  class Visionary : public Boss
64  {
65  DEFINE_PROCESSABLE_BY (VisitingTool);
66  };
67 
68  class Leader : public Visionary
69  {
70  };
71 
72 
73 
75  : public VisitingTool
76  {
77  protected:
78  void talk_to (string guy)
79  {
80  cout << _Fmt{"Hello %s, nice to meet you...\n"} % guy;
81  }
82  };
83 
84  class Babbler
85  : public Applicable< Babbler
86  , Types<Boss,BigBoss,Visionary>::List // dispatch calls to this types
87  , VerboseVisitor
88  >
89  {
90  public:
91  void treat (Boss&) { talk_to("Boss"); }
92  void treat (BigBoss&) { talk_to("Big Boss"); }
93  };
94 
95  // note the following details:
96  // - Babbler "forgot" to declare being applicable to HomoSapiens
97  // - we have new derived class Leader without separate "apply()"-implementation
98 
99 
100 
101 
102  /*********************************************************************/
112  class VisitingTool_test : public Test
113  {
114  virtual void
115  run (Arg)
116  {
117  known_visitor_known_class();
118  visiting_extended_hierarchy();
119  }
120 
121  void
122  known_visitor_known_class()
123  {
124  Boss x1;
125  BigBoss x2;
126 
127  // masquerade as HomoSapiens...
128  HomoSapiens& homo1 (x1);
129  HomoSapiens& homo2 (x2);
130 
131  cout << "=== Babbler meets Boss and BigBoss ===\n";
132  Babbler bab;
133  VisitingTool& vista (bab);
134  homo1.apply (vista);
135  homo2.apply (vista);
136  }
137 
138  void
139  visiting_extended_hierarchy()
140  {
141  HomoSapiens x1;
142  Leader x2;
143 
144  HomoSapiens& homo1 (x1);
145  HomoSapiens& homo2 (x2);
146 
147  cout << "=== Babbler meets HomoSapiens and Leader ===\n";
148  Babbler bab;
149  VisitingTool& vista (bab);
150  homo1.apply (vista); // silent error handler (not Applicable to HomoSapiens)
151  homo2.apply (vista); // Leader handled as Visionary and treated as Boss
152  }
153 
154  };
155 
156 
158  LAUNCHER (VisitingTool_test, "unit common");
159 
160 
161 
162 }}} // namespace lib::visitor::test1
Marker template to declare that some "visiting tool" wants to treat a set of concrete Visitable class...
Definition: visitor.hpp:144
Marker interface or base class for all "Visitables".
Definition: visitor.hpp:197
Front-end for printf-style string template interpolation.
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
virtual ReturnType apply(TOOL &)=0
to be defined by the DEFINE_PROCESSABLE_BY macro in all classes wanting to be treated by some tool ...
Simple test class runner.
#define DEFINE_PROCESSABLE_BY(TOOL)
mark a Visitable subclass as actually treat-able by some "visiting tool" base interface.
Definition: visitor.hpp:231
A library implementation of the Visitor Pattern tailored specifically to Lumiera&#39;s needs within the S...