-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisitor.hpp
139 lines (107 loc) · 2.62 KB
/
Visitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
namespace visitor::primary {
// 1. Add an accept(Visitor) method to the "element" hierarchy
class Element {
public:
virtual auto Accept(class Visitor& v)->void = 0;
virtual ~Element() = default;
};
class This
: public Element {
public:
virtual auto Accept(Visitor& v)->void override;
static auto Thiss()->string {
return "This";
}
virtual ~This() override = default;
};
class That
: public Element {
public:
virtual auto Accept(Visitor& v)->void override;
static auto Thatt()->string {
return "That";
}
virtual ~That() override = default;
};
class TheOther
: public Element {
public:
virtual auto Accept(Visitor& v)->void override;
static auto TheOtherr()->string {
return "TheOther";
}
virtual ~TheOther() override = default;
};
// 2. Create a "visitor" base class w/ a visit() method for every "element" type
class Visitor {
public:
virtual auto visit(This *e)->void = 0;
virtual auto visit(That *e)->void = 0;
virtual auto visit(TheOther *e)->void = 0;
virtual ~Visitor() = default;
};
inline auto This::Accept(Visitor& v)->void {
v.visit(this);
}
inline auto That::Accept(Visitor& v)->void {
v.visit(this);
}
inline auto TheOther::Accept(Visitor& v)->void {
v.visit(this);
}
// 3. Create a "visitor" derived class for each "operation" to do on "elements"
class UpVisitor
: public Visitor {
virtual auto visit(This *e)->void override {
cout << "do Up on " + e->Thiss() << endl;
}
virtual auto visit(That *e)->void override {
cout << "do Up on " + e->Thatt() << endl;
}
virtual auto visit(TheOther *e)->void override {
cout << "do Up on " + e->TheOtherr() << endl;
}
public:
virtual ~UpVisitor() override = default;
};
class DownVisitor
: public Visitor {
virtual auto visit(This *e)->void override {
cout << "do Down on " + e->Thiss() << endl;
}
virtual auto visit(That *e)->void override {
cout << "do Down on " + e->Thatt() << endl;
}
virtual auto visit(TheOther *e)->void override {
cout << "do Down on " + e->TheOtherr() << endl;
}
public:
virtual ~DownVisitor() override = default;
};
inline auto Run()->void
{
Element *list[]
{
new This(),
new That(),
new TheOther()
};
UpVisitor up; // 4. Client creates
DownVisitor down; // "visitor" objects
for (auto element : list) {
// and passes each
element->Accept(up);
// to accept() calls
}
for (auto element : list) {
element->Accept(down);
if (element != nullptr) delete element;
}
}
}