-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathacyclic_visitor.cpp
More file actions
114 lines (94 loc) · 3.34 KB
/
Copy pathacyclic_visitor.cpp
File metadata and controls
114 lines (94 loc) · 3.34 KB
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// cyclic visitor: based on function overloading
// works only on a stable hierarchy
// acyclic visitor: based on RTTI
// no hierarchy limitations, but slower
// -----------------------------------------------------------------------------
// Visitor bases
// -----------------------------------------------------------------------------
template <typename Visitable> struct Visitor {
virtual void visit(Visitable &obj) = 0;
};
// VisitorBase is use as a marker interface, for the hierarchy to accept.
// The accept methods are then forced to dynamic_cast to Visitor<T> type.
struct VisitorBase {
virtual ~VisitorBase() = default;
};
// -----------------------------------------------------------------------------
// HIERARCHY
// -----------------------------------------------------------------------------
struct Expression {
virtual ~Expression() = default;
// The VisitorBase object is expected to have type Visitor<T>
// vvv
virtual void accept(VisitorBase &obj) {
using EV = Visitor<Expression>; // <--- Visitor for Expression
if (auto ev = dynamic_cast<EV *>(&obj))
ev->visit(*this);
}
};
struct DoubleExpression : Expression {
double value;
explicit DoubleExpression(double value) : value(value) {}
virtual void accept(VisitorBase &obj) override {
using DEV = Visitor<DoubleExpression>; // <--- Visitor for DoubleExpression
if (auto ev = dynamic_cast<DEV *>(&obj))
ev->visit(*this);
}
};
struct AdditionExpression : Expression {
Expression *left, *right;
AdditionExpression(Expression *left, Expression *right)
: left(left), right(right) {}
~AdditionExpression() {
delete left;
delete right;
}
virtual void accept(VisitorBase &obj) override {
using AEV =
Visitor<AdditionExpression>; // <--- Visitor for AdditionExpression
if (auto ev = dynamic_cast<AEV *>(&obj))
ev->visit(*this);
}
};
// -----------------------------------------------------------------------------
// Actual Visitor
// -----------------------------------------------------------------------------
// This approach provides additional flexibility, as the visitor is not forced
// to inherit from all types of visitables.
// Then, the hierarchy does not need to be rigid: Cases can be added
// incrementally, instead of having to create accept() methods everywhere.
struct ExpressionPrinter
: VisitorBase, // <--- Inherit from marker interface
Visitor<Expression>, // <-- can be used as a fallback.
// Visitor<DoubleExpression>, // <-- can be removed!
Visitor<AdditionExpression> {
void visit(Expression &) override {
// Not a fallback!
oss << "<this will not be displayed>";
}
// Can be removed!
// void visit(DoubleExpression &obj) override { oss << obj.value; }
void visit(AdditionExpression &obj) override {
oss << "(";
obj.left->accept(*this);
oss << "+";
obj.right->accept(*this);
oss << ")";
}
string str() const { return oss.str(); }
private:
ostringstream oss;
};
int main() {
auto e = new AdditionExpression{
new DoubleExpression{1},
new AdditionExpression{new DoubleExpression{2}, new DoubleExpression{3}}};
ExpressionPrinter ep;
ep.visit(*e);
cout << ep.str() << "\n";
return 0;
}