Click here to Skip to main content
15,881,173 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.7K   457   48  
An article on a templatized implementation of composite and visitor interaction
#pragma once

#include <Visitor.h>

namespace Types {

template <typename Visitor, typename BaseVisitable>
class CVisitorTopToBottom
{
public:
	virtual ~CVisitorTopToBottom() {}
	void operator()( BaseVisitable * ); 
};

template <typename Visitor, typename BaseVisitable>
class CVisitorBottomToTop
{
public:
	virtual ~CVisitorBottomToTop() {}
	void operator()( BaseVisitable * ); 
};

template <typename Visitor, typename BaseVisitable>
class CVisitorLeftToRight
{
public:
	virtual ~CVisitorLeftToRight() {}
	void operator()( BaseVisitable * );
};

template <typename Visitor, typename BaseVisitable>
void
CVisitorTopToBottom<Visitor, BaseVisitable>::operator()( BaseVisitable *visitable )
{
	// Visit this
	visitable->Accept( *dynamic_cast<Visitor*>( this ) );
	// Now visit all the children
	if ( visitable->empty() )
		return;
	BaseVisitable::shallow_iterator iter = visitable->begin_shallow();
	for ( ; iter != visitable->end_shallow(); ++iter )
		(*this)(*iter);
}

template <typename Visitor, typename BaseVisitable>
void
CVisitorBottomToTop<Visitor, BaseVisitable>::operator()( BaseVisitable *visitable )
{
	// Visit all the children
	if ( !visitable->empty() )
	{
		BaseVisitable::shallow_iterator iter = visitable->begin_shallow();
		for ( ; iter != visitable->end_shallow(); ++iter )
			(*this)(*iter);
	}
	// Now visit this
	visitable->Accept( *dynamic_cast<Visitor*>( this ) );
}

template <typename Visitor, typename BaseVisitable>
void
CVisitorLeftToRight<Visitor, BaseVisitable>::operator()( BaseVisitable *visitable )
{
	// Visit the left hand child
	if ( !visitable->empty() )
		(*this)(*visitable->begin_shallow());
	// Visit this
	visitable->Accept( *dynamic_cast<Visitor*>( this ) );
	// Visit the right hand child(ren)
	if ( visitable->size() > 1 )
	{
		BaseVisitable::shallow_iterator iter = ++visitable->begin_shallow();
		for ( ; iter != visitable->end_shallow(); ++iter )
			(*this)(*iter);
	}
}

}

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