Click here to Skip to main content
13,090,267 members (91,999 online)
Click here to Skip to main content


30 bookmarked
Posted 21 May 2005

A policy based reference counting implementation for compound objects

, 26 May 2005
Reference counting smart pointers and handles of various flavours.
//	strings.hpp
//	generic strings

#pragma once
#include "smart.hpp"
#pragma message ("Compiling " __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
		typedef std::vector<Type> data_t;
		typedef 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;
			return p;
		friend class string;
		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
			return &(*pData)[0];
		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();
				n = traits::len(p);
            Type* buff = get_buffer(n);
			traits::copy(buff, p, n);
		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);
			return *this;
		string operator+(const string& s)
		{	return string(*this)+=s; }

		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 string& s) const
		{	return *pData <= *s.pData;  }
		bool operator>=(const string& s) const
		{	return *pData >= *s.pData;  }

		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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Emilio Garavaglia
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 27 May 2005
Article Copyright 2005 by Emilio Garavaglia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid