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)
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 
25 using util::_Fmt;
26 using std::string;
27 using std::cout;
28 
29 
30 namespace lib {
31 namespace visitor {
32 namespace test1 {
33 
34  typedef visitor::Tool<> VisitingTool;
35 
36  class HomoSapiens : public Visitable<>
37  {
38  public:
39  DEFINE_PROCESSABLE_BY (VisitingTool);
40  };
41 
42  class Boss : public HomoSapiens
43  {
44  public:
45  DEFINE_PROCESSABLE_BY (VisitingTool);
46  };
47 
48  class BigBoss : public Boss
49  {
50  public:
51  DEFINE_PROCESSABLE_BY (VisitingTool);
52  };
53 
54  class Visionary : public Boss
55  {
56  DEFINE_PROCESSABLE_BY (VisitingTool);
57  };
58 
59  class Leader : public Visionary
60  {
61  };
62 
63 
64 
66  : public VisitingTool
67  {
68  protected:
69  void talk_to (string guy)
70  {
71  cout << _Fmt{"Hello %s, nice to meet you...\n"} % guy;
72  }
73  };
74 
75  class Babbler
76  : public Applicable< Babbler
77  , Types<Boss,BigBoss,Visionary>::List // dispatch calls to this types
78  , VerboseVisitor
79  >
80  {
81  public:
82  void treat (Boss&) { talk_to("Boss"); }
83  void treat (BigBoss&) { talk_to("Big Boss"); }
84  };
85 
86  // note the following details:
87  // - Babbler "forgot" to declare being applicable to HomoSapiens
88  // - we have new derived class Leader without separate "apply()"-implementation
89 
90 
91 
92 
93  /*********************************************************************/
103  class VisitingTool_test : public Test
104  {
105  virtual void
106  run (Arg)
107  {
108  known_visitor_known_class();
109  visiting_extended_hierarchy();
110  }
111 
112  void
113  known_visitor_known_class()
114  {
115  Boss x1;
116  BigBoss x2;
117 
118  // masquerade as HomoSapiens...
119  HomoSapiens& homo1 (x1);
120  HomoSapiens& homo2 (x2);
121 
122  cout << "=== Babbler meets Boss and BigBoss ===\n";
123  Babbler bab;
124  VisitingTool& vista (bab);
125  homo1.apply (vista);
126  homo2.apply (vista);
127  }
128 
129  void
130  visiting_extended_hierarchy()
131  {
132  HomoSapiens x1;
133  Leader x2;
134 
135  HomoSapiens& homo1 (x1);
136  HomoSapiens& homo2 (x2);
137 
138  cout << "=== Babbler meets HomoSapiens and Leader ===\n";
139  Babbler bab;
140  VisitingTool& vista (bab);
141  homo1.apply (vista); // silent error handler (not Applicable to HomoSapiens)
142  homo2.apply (vista); // Leader handled as Visionary and treated as Boss
143  }
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 or base class for all "Visitables".
Definition: visitor.hpp:188
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 ...
Simplistic 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:222
A library implementation of the Visitor Pattern tailored specifically to Lumiera&#39;s needs within the S...