Click here to Skip to main content
15,889,834 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 107.8K   1.2K   73  
An independent framework to handle Win32 objects inside C++ classes.
//////////////////////////////////////////
//	strings.hpp
//	generic strings
//

#pragma once
#include "smart.hpp"
#pragma message ("including " __FILE__)


namespace GE_{ namespace stdx{

	namespace strings
	{

		//string traits: available for eventual specializations

		struct traits
		{
			template<class Type>
			static size_t len(const Type* p)
			{
				if(!p) return 0;
				size_t n(0);
				while(!!*p++) n++;
				return n;
			}

			template<class Type>
			static void copy(Type* dest, const Type* src, size_t n=-1)
			{
				while(n-- && !!*src)
					*dest++ = *src++;
			}

			struct base{};
		};

	};


	//class stdx::string: mamage a shared data through a smart pointer

	template<class Type, class traits=strings::traits >
	class string:
		public traits::base
	{
	private:
		typedef typename std::vector<Type> data_t;
		typedef typename stdx::statptr<data_t>::strong data_p;
		data_p pData;
		//lets have a shared null string (will simplify a lot the redirections)
		static data_p nullstr() 
		{ 
			static data_p p;
			if(!p)
				p.New()->resize(1);
			return p;
		}
		friend class string;
	public:
		typedef Type element_t;
		string() { pData = nullstr(); }
		Type* get_buffer(size_t len=-1)
		{
			if(len == -1) len = pData->size();
			if(pData.get_holdcount() >1)
				pData.New(*pData); //clone the vector
			if(pData->capacity() < len+1) //less than 10% margin
				pData->reserve(len*3/2+1); //preallocate future grows
			pData->resize(len+1);
			return &(*pData)[0];
		}
		
		const Type* fit_buffer()
		{
			return get_buffer(traits::len((const Type*)*this));
		}

		operator const Type*() const { return &(*pData)[0]; }
		bool operator!() const { return !(*pData)[0]; }
		void clear() { pData = nullstr(); }
		size_t len() const { return pData->size()-1; }
		
		string(const string& s) { pData = s.pData; }
		string(const Type* p, size_t n=-1)
		{
			pData = nullstr();
			if(n==-1)
				n = traits::len(p);
            Type* buff = get_buffer(n);
			traits::copy(buff, p, n+1);
		}
		string(const Type& c, size_t n=1)
		{
			pData.New(n+1, c);
			(*pData)[n] = (*nullstr())[0];
		}

		string& operator=(const string& s) { pData = s.pData; return *this; }
		string& operator+=(const string& s)
		{
			size_t n = len();
			size_t m = s.len();
			Type* buff = get_buffer(m+n);
			traits::copy(buff+n, (const Type*)s, m+1);
			return *this;
		}
		string operator+(const string& s)
		{	return string(*this)+=s; }

		bool operator==(const string& s) const
		{	return pData == s.pData || *pData == *s.pData;  }
		bool operator!=(const string& s) const
		{	return pData != s.pData && *pData != *s.pData;  }
		bool operator<(const string& s) const
		{	return *pData < *s.pData;  }
		bool operator>(const string& s) const
		{	return *pData > *s.pData;  }
		bool operator<=(const string& s) const
		{	return *pData <= *s.pData;  }
		bool operator>=(const string& s) const
		{	return *pData >= *s.pData;  }

		bool operator==(const Type* s) const
		{	return *this == string(s);  }
		bool operator!=(const Type* s) const
		{	return *this != string(s);  }
		bool operator<(const Type* s) const
		{	return *this < string(s);  }
		bool operator>(const Type* s) const
		{	return *this > string(s);  }
		bool operator<=(const Type* s) const
		{	return *this <= string(s);  }
		bool operator>=(const Type* s) const
		{	return *this >= string(s);  }

		const Type& operator[](size_t n) const 
		{	return (*pData)[n]; }
		string operator()(size_t from, size_t count=-1)
		{
			if(count==-1) count = len()-from;
			return string(&(*pData)[from], count);
		}
		
	};


}}

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