Loki::CyclicVisitor类似设计模式中访问者模式
#include <loki/Visitor.h>
#include <loki/TypelistMacros.h>
#include <loki/Functor.h>
#include <loki/AssocVector.h>
#include <iostream>
using namespace std;
class Dog;
class Cat;
class Wolf;
typedef Loki::CyclicVisitor<void, LOKI_TYPELIST_3(Dog, Cat, Wolf)> MyVisitor;
class Animal
{
public:
virtual void Accept(MyVisitor& v) = 0;
public:
virtual void say(){ cout << "I am Animal class" << endl; }
};
class Dog :public Animal
{
public:
LOKI_DEFINE_CYCLIC_VISITABLE(MyVisitor)
public:
void say(){ cout << "I am Dog" << endl; }
};
class Cat :public Animal
{
public:
LOKI_DEFINE_CYCLIC_VISITABLE(MyVisitor)
public:
void say(){ cout << "I am Cat" << endl; }
};
class Wolf :public Animal
{
public:
LOKI_DEFINE_CYCLIC_VISITABLE(MyVisitor)
public:
void say(){cout<<"I am Wolf"<<endl;}
};
class ConcretVisitor :public MyVisitor
{
public:
void Visit(Dog& dog)
{
dog.say();
}
void Visit(Cat& cat)
{
cat.say();
}
void Visit(Wolf& wolf)
{
wolf.say();
}
};
void testFunc(std::shared_ptr<Animal> pAnimal)
{
ConcretVisitor cv;
pAnimal->Accept(cv);
}
int main()
{
std::shared_ptr<Animal> pDog = std::make_shared<Dog>();
std::shared_ptr<Animal> pCat = std::make_shared<Cat>();
std::shared_ptr<Animal> pWolf = std::make_shared<Wolf>();
testFunc(pDog);
testFunc(pCat);
testFunc(pWolf);
getchar();
return 0;
}