Smart pointer

Description

Smart pointer is an abstract data type, that extend functionality for standard c++ pointers. The most import features are automatic lifetime and references management of the wrapped objects. Smart pointer automatically deletes non-referenced objects and fulfills the role of garbage collector. This is very useful for reduce memmory leaks and related bugs.

Source code C++

#pragma once
#ifndef SMARTPOINTER_H_
#define SMARTPOINTER_H_
 
#include "assert.h"
 
template <typename T>
class SmartPointer
{
public:
        SmartPointer();
        SmartPointer(T* pT);
        ~SmartPointer();
        SmartPointer(const SmartPointer<T>& rT);
 
        SmartPointer<T>& operator=(const SmartPointer<T>& rT);
 
        T* operator->();
        const T* operator->() const;
 
		bool operator==(const SmartPointer<T>& rT) const;
		bool operator!=(const SmartPointer<T>& rT) const;
 
        bool Null() const {return m_pCounted == 0;}
        void SetNull() { UnBind(); }
 
private:
        void UnBind();
 
		template <typename T>
		class Counted
		{
			friend class SmartPointer<T>;
 
		private:
				Counted(T* pT) : Count(0), my_pT(pT) 
								   { assert(pT != 0); }
				~Counted()         { assert(Count == 0); delete my_pT; }
 
				unsigned GetRef()  { return ++Count; }
				unsigned FreeRef() { assert(Count!=0); return --Count; }
 
				T* const my_pT;
				unsigned Count;
		};
 
        Counted<T>* m_pCounted;
 
};
 
template<typename T>
SmartPointer<T>::SmartPointer() : m_pCounted(0) {}
 
template<typename T>
SmartPointer<T>::SmartPointer(T* pT)
{
        m_pCounted = new Counted<T>(pT);
        m_pCounted->GetRef();
}
 
template<typename T>
SmartPointer<T>::~SmartPointer()
{
        UnBind();
}
 
template<typename T>
void SmartPointer<T>::UnBind()
{
        if (!Null() && m_pCounted->FreeRef() == 0)       
                delete m_pCounted;
        m_pCounted = 0;
}
 
template<typename T>
T* SmartPointer<T>::operator->()
{
        return m_pCounted->my_pT;
}
 
template<typename T>
const T* SmartPointer<T>::operator->() const
{
        return m_pCounted->my_pT;
}
 
template<typename T>
SmartPointer<T>::SmartPointer(const SmartPointer<T>& rT)
{
        m_pCounted = rT.m_pCounted;
        if (!Null())
                m_pCounted->GetRef();
}
 
template<typename T>
SmartPointer<T>& SmartPointer<T>::operator=(const SmartPointer<T>& rT)
{
        if (!rT.Null())
                rT.m_pCounted->GetRef();
        UnBind();
        m_pCounted = rT.m_pCounted;
        return *this;
}
 
template<typename T>
bool SmartPointer<T>::operator==(const SmartPointer<T>& rT) const
{
	return rT.m_pCounted->my_pT == m_pCounted->my_pT;
}
 
template<typename T>
bool SmartPointer<T>::operator!=(const SmartPointer<T>& rT) const
{
	return !(rT == this);
}
 
#endif /* SMARTPOINTER_H_ */

References

programming/c-cpp/smaartpointer.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0