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

#include "GenericTree.h"
#include "Visitor.h"

#include <string>

class CNodeBase :
	public Loki::BaseVisitable<>,
	public Types::CGenericTree<CNodeBase>
{
public:
	DEFINE_VISITABLE()
	CNodeBase(void) : m_id( m_nextId++ ) {}
	CNodeBase( const CNodeBase &src ) : Types::CGenericTree<CNodeBase>( src ), m_id( m_nextId++ ) {}
	virtual ~CNodeBase(void) {}
	CNodeBase &operator=( const CNodeBase &rhs ) { if ( this != &rhs ) Types::CGenericTree<CNodeBase>::operator=( rhs ); return *this; }
	virtual CNodeBase *Clone() { return new CNodeBase( *this ); }

	unsigned long GetId() { return m_id; }
	virtual std::string GetType() { return std::string( "CNodeBase" ); }

private:
	unsigned long m_id;
	static unsigned long m_nextId;
};

class CNodeDerived1 :
	public CNodeBase
{
public:
	DEFINE_VISITABLE()
	CNodeDerived1(void) {}
	CNodeDerived1( const CNodeDerived1 &src ) : CNodeBase( src ) {}
	virtual ~CNodeDerived1(void) {}
	CNodeDerived1 &operator=( const CNodeDerived1 &rhs ) { if ( this != &rhs ) CNodeBase::operator=( rhs ); return *this; }
	virtual CNodeDerived1 *Clone() { return new CNodeDerived1( *this ); }

	virtual std::string GetType() { return std::string( "CNodeDerived1" ); }

};

class CNodeDerived2 :
	public CNodeBase
{
public:
	DEFINE_VISITABLE()
	CNodeDerived2(void) {}
	CNodeDerived2( const CNodeDerived2 &src ) : CNodeBase( src ) {}
	virtual ~CNodeDerived2(void) {}
	CNodeDerived2 &operator=( const CNodeDerived2 &rhs ) { if ( this != &rhs ) CNodeBase::operator=( rhs ); return *this; }
	virtual CNodeDerived2 *Clone() { return new CNodeDerived2( *this ); }

	virtual std::string GetType() { return std::string( "CNodeDerived2" ); }

};

unsigned long CNodeBase::m_nextId = 0;

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