Click here to Skip to main content
15,894,740 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.
// Trace.h
//	debug tracer

#pragma once

namespace GE_{ namespace NUtil{

	using namespace NTypes;
	
	struct STrace //do trace into debug
	{
	private:
		static int& _Deep() {	static int deep=0; return deep; }
		UINT _filter; // the filter value for this trace object
	public:
		//_Filter is the warning level: the lower the level, the greater are thre received messages
		static UINT& _Filter() { static UINT filter = 0; return filter; }
		STrace(UINT filter) {	_filter = filter; _Deep()++;	}
		~STrace() {	_Deep()--; }
		void operator() (LPCTSTR format, ...); //printf like syntax
		void createmsg(const type_info& ti, LPVOID address);
		void deletemsg(const type_info& ti, LPVOID address);
	};


#ifdef _DEBUG
	inline void STrace::operator() (LPCTSTR format, ...)
	{
		if(_filter < _Filter()) return;
		TCHAR s[1024]; memset(s,0,sizeof(s));
		int m = _stprintf(&s[0], "(%4u)", _filter);
		memset(&s[m],'.',sizeof(TCHAR)*_Deep());
		va_list params;
		int n = _vstprintf(&s[_Deep()+m], format, va_start(params, format));
		_RPT0(_CRT_WARN, &s[0]);
	}

	inline void STrace::createmsg(const type_info& ti, LPVOID address)
	{
		operator()("Create %s @ %p\n", ti.name(), address);
	}

	inline void STrace::deletemsg(const type_info& ti, LPVOID address)
	{
		operator()("Delete %s @ %p\n", ti.name(), address);
	}

#else
	inline void STrace::operator() (LPCTSTR format, ...) {}
	inline void STrace::createmsg(const type_info& ti, LPVOID address) {}
	inline void STrace::deletemsg(const type_info& ti, LPVOID address) {}
#endif


}}

#ifdef _DEBUG
#define STRACE(variable,lev,command) GE_::NUtil::STrace variable(lev); variable command;
#else
#define STRACE(variable,lev,command) //defined as "empty"
#endif

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