#include #include "Visitor.h" #include "Visitor_Shared_Pointer.h" using std::cout; using std::endl; /* Set some classes up for conversion */ class Base {public: virtual ~Base() {} }; class Derived1 : virtual public Base { public: virtual ~Derived1() {} }; class Derived2 : virtual public Base { public: virtual ~Derived2() {} }; class Derived3 : public Derived1, public Derived2 { public: virtual ~Derived3(){} }; /* a function class that overloads the appropriate () functions for our test */ class Function { public: void operator()( const Base* ) { cout << "BASE!" << endl; } void operator()( const Derived1* ) { cout << "DERIVED!" << endl; } void operator()( const Derived2* ) { cout << "DERIVED 2!" << endl; } void operator()( const Derived3* ) { cout << "DERIVED 3!" << endl; } void operator()( const boost::shared_ptr& ) { cout << "BASE!" << endl; } void operator()( const boost::shared_ptr& ) { cout << "DERIVED!" << endl; } void operator()( const boost::shared_ptr& ) { cout << "DERIVED 2!" << endl; } void operator()( const boost::shared_ptr& ) { cout << "DERIVED 3!" << endl; } void operator()( const Base& ) { cout << "BREF!" << endl; } void operator()( const Derived1& ) { cout << "DREF!" << endl; } void operator()( const Derived2& ) { cout << "DREF 2!" << endl; } void operator()( const Derived3& ) { cout << "DREF 3!" << endl; } }; int main() { // this visitor will work with pointers Visitor< mpl::list > v; // this visitor will work with references Visitor< mpl::list, Base, boost::add_reference > v2; // this visitor will work with shared ptrs because of our specializations Visitor< mpl::list, Base, SharedPointerPolicy > v3; Base* b = new Base; Base* d1 = new Derived1; Base* d2 = new Derived2; Base* d3 = new Derived3; boost::shared_ptr sb(new Base); boost::shared_ptr sd1(new Derived1); boost::shared_ptr sd2(new Derived2); boost::shared_ptr sd3(new Derived3); Base& br = *b; Base& d1r = *d1; Base& d2r = *d2; Base& d3r = *d3; v.visit( b, Function() ); v.visit( d3, Function() ); v.visit( d1, Function() ); v.visit( d2, Function() ); v2.visit( br, Function() ); v2.visit( d2r, Function() ); v2.visit( d1r, Function() ); v2.visit( d3r, Function() ); v3.visit( sb, Function() ); v3.visit( sd1, Function() ); v3.visit( sd2, Function() ); v3.visit( sd3, Function() ); delete b; delete d1; delete d2; delete d3; }