Accessor - GerdHirsch/Cpp-VisitorFrameworkCyclicAcyclic GitHub Wiki
To give special access to the visitors, a type can define an inner class Accessor{...};
class NonVisitableWithAccessor{
public:
...
class Accessor{
protected:
static void setData(this_type& This, std::string data){
This.data = data;
}
static std::string getData(this_type& This){
return This.data;
}
};
private:
std::string data;
};
Visitors inherit protected from these Accessor
s and therefore are able to use the static protected interface inside of the visit(..)
methods, no matter whether the type inherits from Repository::VisitableImpl<..>
or not. The type has to be named Accessor
by convention. using Accessor = ...
and this->Accessor::getData()
ist needed because of name ambiguity of e.g. setData(...)
in different types to be visited.
class DemoVisitor23 : public Repository::visits<E2, E3, NonVisitableWithAccessor>
{
public:
void visit(E2& v);
void visit(E3& v);
void visit(NonVisitableWithAccessor& v) {
using Accessor = NonVisitableWithAccessor::Accessor;
std::cout << this->Accessor::getData(v) << std::endl;
this->Accessor::setData(v, "DemoVisitor23::Data");
std::cout << this->Accessor::getData(v) << std::endl;
}
std::string toString() const override { return "DemoVisitor23"; }
};