Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C++

Writing Win32 Apps with C++ only classes

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
1 Mar 2004CPOL21 min read 134.6K   3.5K   109  
C++ classes and wrappers to write W32 apps without MFC, ATL or other (part 1?)
//Evt.h
//	definition of "event" model
#pragma once

#include "Wrp.h"

namespace GE_{ namespace NWrp{

	class Event_base //the bas for all type of events
	{};

	struct XSink_base
	{
		virtual ~XSink_base(){}
		virtual Event_base* Get()=0;
	};

	//generic sync object
	template<
		class A //parameters class
	>
	struct XSink:  //will be created by the receiver when registering
		public XSink_base,
		public WrpBase<Event_base*, XSink, traits_wrp_hnd<Event_base*>, XRefChain<XSink> >,
		public EChainedAction<XSink, XSink::iterator, A>
	{
		friend TRefCount;
		friend TChainedAction;
	private:
		virtual bool DoAction(const A& a)=0; //abstract: different type of sink will chain together ...
		bool OnAction(const A& a)
		{	return DoAction(a);	}
		virtual Event_base* Get() { return TWrpBase::Value(); }
	public:
		virtual ~XSink() {}
		virtual void Unregister()=0;
	};

	
	//sync object delegating to a receiver
	template<
		class A, //parameters class
		class R	 //receiver class
	>
	struct XTypedSink:
		public XSink<A>
	{
	private:
		R* _pR; //creator and owner
		typedef bool(R::*t_pfn)(const A&); //callback function
		t_pfn _pfn;
		virtual bool DoAction(const A& a)
		{
			if(_pR && _pfn && (_pR->*_pfn)(a) )
				return true;
			return false;
		}
	public:
		XTypedSink(R* pR, t_pfn pRMemFunc) { _pR = pR; _pfn = pRMemFunc; SetOwnership(false); }
		virtual void Unregister() { if(_pR) _pR->UnregisterEvent(*static_cast<Event<A>*>(Value())); }
	};

	template<
		class A		//parameters
	>
	class Event:
		public Event_base  //use as member variabkle in event sources
	{
	public:
		bool operator()(const A& a)
		{
			XSink<A>::iterator first, last;
			XSink<A>::get_range(first, last, this); //find out eventual existing synks
			return XSink<A>::Act(a, first, last);	//act on them
		}
		~Event()
		{
			XSink<A>::iterator i, last;
			XSink<A>::get_range(i, last, this); //find out eventual existing synks
			while(i!=last)
			{
				XSink<A>* pS = *i;
				i++;
				pS->Unregister();
			}
		}
	};


	template<class D> //derived class
	class EventRcv
	{
	private:
		typedef NWrp::Ptr<XSink_base>::Static t_ptrsink; //oly owner
		typedef std::list<t_ptrsink> t_lstptrsink;
		t_lstptrsink _lstptrsink;

		struct IsEvent
		{
			Event_base* _pEv;
			IsEvent(Event_base* pEv) { _pEv = pEv; }
			bool operator()(const t_ptrsink& p)
			{	return p->Get() == _pEv; }
		};

	public:
		template<class A>
			void RegisterEvent(Event<A>& event, bool(D::*memfunc)(const A&))
		{
			XTypedSink<A,D>* pS = new XTypedSink<A,D>(static_cast<D*>(this), memfunc);
			_lstptrsink.push_front(pS);
			pS->Attach(&event);
		}

		template<class A>
			void UnregisterEvent(Event<A>& event)
		{
			_lstptrsink.remove_if(IsEvent(&event));
		}

		void UnregisterAllEvents()
		{
			_lstptrsink.clear();
		}

		template<class A>
			bool Default()
		{	return XSink<A>::Default();	}
	};
	


}}

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