Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Objective C

Composites and Visitors - a Templatized Approach

Rate me:
Please Sign up or sign in to vote.
4.22/5 (12 votes)
13 Oct 20049 min read 94.8K   457   48  
An article on a templatized implementation of composite and visitor interaction
#pragma once

#include "GenericVisitor.h"
#include "Visitor.h"

// Normally the Loki visitor allows forward declares to be used for the visitable objects
// but I have put everything in the header of this simple demonstration
#include "NodeBase.h"

#include <iostream>

using namespace std;

class CVisitorBase :
	public Loki::BaseVisitor,
	public Loki::Visitor<CNodeBase>,
	public Loki::Visitor<CNodeDerived1>,
	public Loki::Visitor<CNodeDerived2>
{
public:
	CVisitorBase() {}
	virtual ~CVisitorBase() {}

	virtual void Visit( CNodeBase &node ) {}
	virtual void Visit( CNodeDerived1 &node ) { Visit( (CNodeBase&) node ); }
	virtual void Visit( CNodeDerived2 &node ) { Visit( (CNodeBase&) node ); }
};

class CVisitorDerived1 :
	public CVisitorBase
{
public:
	CVisitorDerived1() {}
	virtual ~CVisitorDerived1() {}

	virtual void Visit( CNodeBase &node ) { cout << "Visiting base " << node.GetId() << endl; }
	virtual void Visit( CNodeDerived1 &node ) { cout << "Visiting derived1 " << node.GetId() << endl; }
	// Note that by not implementing the CNodeDerived2 visit function, the base will be called
};

class CVisitorDerived2 :
	public CVisitorBase
{
public:
	CVisitorDerived2() {}
	virtual ~CVisitorDerived2() {}

	virtual void Visit( CNodeBase &node ) { cout << "Visiting base " << node.GetId() << endl; }
	virtual void Visit( CNodeDerived1 &node ) { cout << "Visiting derived1 " << node.GetId() << endl; }
	virtual void Visit( CNodeDerived2 &node ) { cout << "Visiting derived2 " << node.GetId() << endl; }
};

class CVisitorDerived1TopToBottom :
	public CVisitorDerived1,
	public Types::CVisitorTopToBottom<CVisitorDerived1TopToBottom,CNodeBase>
{
};

class CVisitorDerived2BottomToTop :
	public CVisitorDerived2,
	public Types::CVisitorBottomToTop<CVisitorDerived2BottomToTop,CNodeBase>
{
};

class CVisitorDerived2LeftToRight :
	public CVisitorDerived2,
	public Types::CVisitorLeftToRight<CVisitorDerived2LeftToRight,CNodeBase>
{
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
United Kingdom United Kingdom
I started programming on 8 bit machines as a teenager, writing my first compiled programming language before I was 16. I went on to study Engineering and Computer Science at Oxford University, getting a first and the University Prize for the best results in Computer Science. Since then I have worked in a variety of roles, involving systems management and development management on a wide variety of platforms. Now I manage a software development company producing CAD software for Windows using C++.

My 3 favourite reference books are: Design Patterns, Gamma et al; The C++ Standard Library, Josuttis; and Computer Graphics, Foley et al.

Outside computers, I am also the drummer in a band, The Unbelievers and we have just released our first album. I am a pretty good juggler and close up magician, and in my more insane past, I have cycled from Spain to Eastern Turkey, and cycled across the Namib desert.

Comments and Discussions