Click here to Skip to main content
11,480,626 members (59,773 online)
Click here to Skip to main content
Add your own
alternative version

A policy based reference counting implementation for compound objects

, 26 May 2005 CPOL 25.7K 253 29
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
	{
	private:
		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;
			if(!p)
				p.New()->resize(1);
			return p;
		}
		friend class string;
	public:
		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
			pData->resize(len+1);
			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();
			if(n==-1)
				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.

License

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

Share

About the Author

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

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