Visitor & Visitable - GerdHirsch/Cpp-VisitorFrameworkCyclicAcyclic GitHub Wiki
Cyclic
Cyclic Visitor base declares an operation virtual void visit(ElementType&)
for each type of element
that all Visitors want to visit and therefore must be accepted by
void Visitable::accept(MyCyclicVisitor&)
.
class MyCyclicVisitor{ void visit(E1& v) = 0; void visit(En& v) = 0; };
This is an application specific family of types and the resulting Visitor type is application specific too.
Because the base of all Visitables must define accept(..)
it's also application specific.
The template Visitable
decouples each other. The template parameter VisitorBase
is the pure abstract concept of
a Visitor with it's help, the application specific type is injected into the framework.
template<class VisitorBase>
class Visitable
{
public:
virtual ~Visitable(){};
virtual void accept(VisitorBase& visitor) = 0;
virtual std::string toString() const = 0;
};
Acyclic
Acyclic Visitor base is the pure abstract empty class Visitor be accepted by all Visitables, no matter to which familiy of Visitables they belong to.
struct Visitor{
virtual ~Visitor(){}
virtual std::string toString() const = 0;
};
class Visitor
is just needed to declare the signature
of accept(Visitor&)
in the base class of all Visitables
.
class Visitable
{
public:
virtual ~Visitable(){};
virtual void accept(Visitor& visitor) = 0;
virtual std::string toString() const = 0;
};