Click here to Skip to main content
15,897,226 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.
//Shapes.cpp
#include "stdafx.h"
#include "shapes.h"

namespace GE_{ namespace NApp{

	void SRect::Rescale(const SRect& rFrom, const SRect& rTo)
	{
		left = rTo.left + (rTo.right-rTo.left)*(left-rFrom.left)/(rFrom.right-rFrom.left);
		right = rTo.left + (rTo.right-rTo.left)*(right-rFrom.left)/(rFrom.right-rFrom.left);
		top = rTo.top + (rTo.bottom-rTo.top)*(top-rFrom.top)/(rFrom.bottom-rFrom.top);
		bottom = rTo.top + (rTo.bottom-rTo.top)*(bottom-rFrom.top)/(rFrom.bottom-rFrom.top);
	}


	EShape::EShape()
	{
		for(int i=0; i<attr__count; i++)
			_attr[i] = -1;
		_rcBound.clear(); 
	}


	NTypes::UINT EShape::GetAttribute(e_shapeattributes a)
	{	return _attr[a];	}

	NTypes::UINT EShape::GetDeepAttribute(e_shapeattributes a)
	{
		static NTypes::UINT defattr[] = { 1, 2, 3 };
		NTypes::UINT r=-1;
		Ptr pSh(this);
		while(!!pSh && r==-1)
		{
			r = pSh->GetAttribute(a);
			pSh = pSh->GetParent(); //note the assignment
		}
		if(r == -1)
			r = defattr[a];
		return r;
	}

	void EShape::SetAttribute(e_shapeattributes attr, NTypes::UINT value)
	{	_attr[attr] = value;	OnChangedAttributes(attr); Invalidate(); }

	void EShape::Invalidate()
	{
		if(GetChildrenCount()) _rcBound.clear(); //delay recalculation on next "Get"
	}
	
	void EShape::GetBound(SRect& r)
	{ 
		r = _rcBound;
		if(!r)
		{
			ItShallow i = GetFirstChild();
			for(;!!i;++i)
			{
				SRect rc; i->GetBound(rc);
				if(r.left==0 && r.right==0)
					r.left = r.right = rc.left;
				else  if(r.left>rc.left) r.left = rc.left;
				if(r.right<rc.right) r.right = rc.right;
				if(r.top==0 && r.bottom == 0)
					r.top = r.bottom = rc.top;
				else if(r.top>rc.top) r.top = rc.top;
				if(r.bottom < rc.bottom) r.bottom = rc.bottom;
			}
			_rcBound = r;
		}
	}

   	void EShape::SetBound(const SRect& r)
	{
		SRect oldRect = _rcBound;
		SRect newRect = r;
		if(oldRect == newRect) return; //nothing to do
		OnResizing(newRect);
		//fisrt: scale the children
		ItShallow i = GetFirstChild();
		for(;!!i;++i)
		{
			SRect rcChild; i->GetBound(rcChild);
			rcChild.Rescale(oldRect,newRect);
			i->SetBound(rcChild); //this can ask us a recalc ...
		}
		//second: ask the parent to recalc
		Ptr pParent = GetParent();
		if(!!pParent) pParent->Invalidate();
		_rcBound = newRect;
		//third: invalidate ourselves
		Invalidate();
	}


	bool EGraphicShape::OnSettingParent(ITreeNode* pParent, ITreeNode* pPrevious)
	{
		CGroup::Ptr pGroup(pParent);
        if(!pGroup) return false;
		pGroup->Invalidate();
		return true;
	}


	void CCircle::Draw(IDrawer* pDraw)
	{
		STRACE(trc,2,("Drawing %s at %d\n",typeid(*this).name(), this));
		SRect rc; GetBound(rc);
		std::cout << "Drawing circle at " << this << "\n\t";
		std::cout << "Line color = " << GetDeepAttribute(attr_linecolor) <<
			" Fill color = " << GetDeepAttribute(attr_fillcolor) << "\n\t";
		std::cout << "placement: ("; 
		std::cout << rc.left << ',' << rc.top << ',' << rc.right << ',' << rc.bottom << ")\n\n";
	}

	void CRectangle::Draw(IDrawer* pDraw)
	{
		STRACE(trc,2,("Drawing %s at %d\n",typeid(*this).name(), this));
		SRect rc; GetBound(rc);
		std::cout << "Drawing rectangle at " << this << "\n\t";
		std::cout << "Line color = " << GetDeepAttribute(attr_linecolor) <<
			" Fill color = " << GetDeepAttribute(attr_fillcolor) <<
			" Text color = " << GetDeepAttribute(attr_textcolor) << "\n\t";
		std::cout << "placement: ("; 
		std::cout << rc.left << ',' << rc.top << ',' << rc.right << ',' << rc.bottom << ")\n\n";
	}

	CRectangle::CRectangle(CGroup* pGroup, const SRect& r,
			NTypes::UINT lineclr, NTypes::UINT fillclr, NTypes::UINT txtclr)
	{
		SetBound(r);
		SetAttribute(attr_linecolor, lineclr); SetAttribute(attr_fillcolor, fillclr); SetAttribute(attr_textcolor, txtclr);
		SetParent(pGroup);
	}


	void CGroup::Draw(IDrawer* pDraw)
	{
		STRACE(trc,2,("Drawing %s at %d\n",typeid(*this).name(), this));
		std::cout << "Drawing group at " << this << " ...\n\n";
		ItShallow i = GetFirstChild();
		for(;!!i;++i)
			i->Draw(pDraw);
		std::cout << "end drawing group at " << this << " ...\n\n";
	};

	CCircle::CCircle(CGroup* pGroup, const SRect& r,
			NTypes::UINT lineclr, NTypes::UINT fillclr)
	{
		SetBound(r);
		SetAttribute(attr_linecolor, lineclr); SetAttribute(attr_fillcolor, fillclr);
		SetParent(pGroup);
	}


}}

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