Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C++

Composites-Visitors pattern: the OOP way

Rate me:
Please Sign up or sign in to vote.
4.37/5 (9 votes)
15 Aug 2004CPOL13 min read 50.4K   387   28  
An implementation of the composite-visitors pattern avoiding the use of rescursive generic code.
//Tree.h
#pragma once
#include "RefCnt.h"

namespace GE_{ namespace NTypes{

	//ITreeNode: interface for composite objects

	interface ITreeNode:
		public virtual IReferable
	{
		virtual ITreeNode* GetFirstChild()=0;
		virtual ITreeNode* GetLastChild()=0;
		virtual int GetChildrenCount()=0;
		virtual ITreeNode* GetParent()=0;
		virtual ITreeNode* GetPrev()=0;
		virtual ITreeNode* GetNext()=0;
		virtual bool SetParent(ITreeNode* pParent, ITreeNode* pPrevious=NULL)=0;
		virtual ~ITreeNode() {}
	
        typedef NTypes::Ptr<ITreeNode, IReferable::XPtr> Ptr;
	protected:
		//function for use by implementors
		virtual void SetFirstChild(ITreeNode*)=0;
		virtual void SetLastChild(ITreeNode*)=0;
		virtual void SetPrev(ITreeNode*)=0;
		virtual void SetNext(ITreeNode*)=0;
		virtual void ChangeChildrenCount(int delta)=0;
		struct XProxy
		{
			ITreeNode* _pN;
			XProxy(ITreeNode* pN) { _pN = pN; }
			void SetFirstChild(ITreeNode* pN) {_pN->SetFirstChild(pN); }
			void SetLastChild(ITreeNode* pN) {_pN->SetLastChild(pN); }
			void SetPrev(ITreeNode* pN) {_pN->SetPrev(pN); }
			void SetNext(ITreeNode* pN) {_pN->SetNext(pN); }
			void ChangeChildrenCount(int delta) {_pN->ChangeChildrenCount(delta); }
		};
		friend XProxy;

	protected:
		struct XShallowIterate
		{
		protected:
			typedef ITreeNode _TRefType;
			void _assign(ITreeNode* p);
			ITreeNode* _access() const;
			void _inc();
			void _dec();
			ITreeNode* _subscript(int i);

		protected:
			ITreeNode::Ptr _pCurrent;
		};
	public:
		typedef NTypes::Ptr<ITreeNode, XShallowIterate> ItShallow;

	protected:
		struct XDeepIterate: public XShallowIterate
		{
			void _inc();
			void _dec();
		};
	public:
		typedef NTypes::Ptr<ITreeNode, XDeepIterate> ItDeep;

	};

	class ETreeNode: 
		public virtual ITreeNode,
		public virtual EReferable
	{
	public:
		virtual ITreeNode* GetFirstChild();
		virtual ITreeNode* GetLastChild();
		virtual ITreeNode* GetParent();
		virtual ITreeNode* GetPrev();
		virtual ITreeNode* GetNext();
		virtual int GetChildrenCount();
		virtual bool SetParent(ITreeNode* pParent, ITreeNode* pPrevious=NULL);
	protected:
		virtual void SetFirstChild(ITreeNode* pN);
		virtual void SetLastChild(ITreeNode* pN);
		virtual void SetPrev(ITreeNode* pN);
		virtual void SetNext(ITreeNode* pN);
		virtual void ChangeChildrenCount(int delta);
	protected:
		//hooks
		virtual bool OnSettingParent(ITreeNode* pParent, ITreeNode* pPrevious) {return true;}
		virtual void OnSettedParent() {}
	private:
		ITreeNode::Ptr _pChildFirst, _pChildLast, _pNext;
		ITreeNode *_pParent, *_pPrev; //backpointers don't refcount.
		int _nCount;
	protected:
		ETreeNode();
		virtual ~ETreeNode();
	};

}}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Italy Italy
Born and living in Milan (Italy), I'm an engineer in electronics actually working in the ICT department of an important oil/gas & energy company as responsible for planning and engineering of ICT infrastructures.
Interested in programming since the '70s, today I still define architectures for the ICT, deploying dedicated specific client application for engineering purposes, working with C++, MFC, STL, and recently also C# and D.

Comments and Discussions