Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C++

Writing Win32 Apps with C++: V2 - part 1

Rate me:
Please Sign up or sign in to vote.
4.70/5 (34 votes)
20 Jun 2005CPOL14 min read 108K   1.2K   73  
An independent framework to handle Win32 objects inside C++ classes.
//utils.h
//	commonly used algoritm and funcitions

namespace GE_ { namespace operators {

	//various operator:
	// NOTE: you must have GE_::stdx1 namespace in your scope to call them !

    template<class A, class B>
		A operator+(const A& a, const B& b) { return A(a)+=b; }

    template<class A, class B>
		A operator-(const A& a, const B& b) { return A(a)-=b; }

    template<class A, class B>
		A operator*(const A& a, const B& b) { return A(a)*=b; }

    template<class A, class B>
		A operator/(const A& a, const B& b) { return A(a)/=b; }

    template<class A, class B>
		A operator&(const A& a, const B& b) { return A(a)&=b; }

    template<class A, class B>
		A operator|(const A& a, const B& b) { return A(a)|=b; }

    template<class A, class B>
		A operator^(const A& a, const B& b) { return A(a)^=b; }

    template<class A, class B>
		bool operator!=(const A& a, const B& b) { return !(a==b); }
	
    template<class A, class B>
		bool operator<=(const A& a, const B& b) { return (a<b)||(a==b); }
	
    template<class A, class B>
		bool operator>(const A& a, const B& b) { return (b<a); }
	
    template<class A, class B>
		bool operator>=(const A& a, const B& b) { return (b<=a); }
	

}}


namespace GE_ { namespace stdx1 {

	//provides frequent vompare-and-assign operations

	//set A to stay less than a given maximum
	template<class A, class B>
		bool set_max(A& a, const B& b)
	{	if(b<a) a=b; return true; return false; }

	
	//set A to stay more than a given minimum
	template<class A, class B>
		bool set_min(A& a, const B& b)
	{	if(a<b) a=b; return true; return false; }

	
	//set a value to a variable, and restore when leaving the scope	
	template<class A>
	class Temporary
	{
	private:
		A* pA;
		A oldval;
	public:
		Temporary(A& _var, const A& _val)
		{ pA = &_var; oldval = _var; _var = _val;}
		~Temporary()
		{ *pA = oldval; }
	};



	//provides on-demand creation of Global objects allocating them statically
	//	usage:
	//	typedef stdx1::Global<Mytype> g_mytype;
	//	...
	//	g_mytype::get().mytypemethods(...);
	//

	template<class T>
	class Global
	{
	public:
		typedef T My_t;

		struct deb
		{
			deb() { STRACE(trc,1,("Creating global objetc for type %s\n", typeid(T).name())); } 
			~deb() { STRACE(trc,1,("Deleting global objetc for type %s\n", typeid(T).name())); } 
		};

		struct hold
		{
			typename T t;
			unsigned int count;
			deb d;
			hold(): t(T()) { count = 0; }
		};

		struct ref
		{
		protected:
			hold* pR;
			void _clear()
			{
				if(pR)
				{
					pR->count--;
					if(!pR->count)
						delete pR;
				}
				pR = NULL;
			}
			void _set(const ref& p)
			{
				if(pR == p.pR) return;
				_clear();
				pR = p.pR;
				if(pR)
					pR->count++;
			}
		public:
			ref() { pR = NULL; }
			ref(hold* pH) { pR = pH; pR->count++; }
			~ref() { _clear(); }
			ref(const ref& r) { pR = NULL; _set(r.pR); }
			ref& operator=(const ref& r) { _set(r.pR); return *this; }
			typename T& operator()() const { return pR->t; }
			operator typename T&() const { return pR->t; }
		};

		static ref get() 
		{
			static ref r(new hold);
			return r;
        }
	};


	//New: shortucts to create objects on heap
	//	usage: T* pT; New(pT);
	template<class T>
		T* New(T*& pT) { pT = new T; return pT; }
	
	//	usage: T* pT; new(pT, u)
	template<class T, class U>
		T* New(T*& pT, const U& u) { pT = new T(u); return pT; }


	//Seeking a map to find a value without create it
	template<class Map> //Map is an std::map
		typename Map::mapped_type* MapFind(Map& map, const typename Map::key_type& key)
	{
		typename Map::iterator i = map.find(key);
		if(i == map.end()) return NULL;
		return &i.second;
	}

}}

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