Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

Reference counted smart pointer

, 4 Dec 2012
This article presents a solution to manage pointers in C++ in a similar way as COM.
RefCountSmartpointer.zip
RefCountSmartpointer
TestClassImpl
TestClassImpl.vcxproj.filters
UnitTestCoreImpl
UnitTestCoreImpl.vcxproj.filters
UnitTestCoreImpl.vcxproj.user
#pragma once

#include "refcount.h"

namespace CoreImpl
{
	class _refcnt
	{
	};

	/**
		This class is inspired from the smart pointer class of COM. The pointer it's managing 
		must implement IRefCount. It gives a class a COM like behavior by insuring that it is destroyed 
		only when it is no longer used.
	 */
	template<class T> class _refcnt_ptr sealed : public _refcnt
	{
	private:
		IRefCount* m_ptr;	// The pointer to manage

	public:
		/**
			Create an empty _refcnt_ptr
		 */
		_refcnt_ptr() : m_ptr(nullptr)		
		{
		}

		/**
			Copy constructor
		 */
		_refcnt_ptr(const _refcnt_ptr<T>& otherPtr) : m_ptr(otherPtr.m_ptr)
		{
			_AddRef();
		}

		/**
			Construct from a pointer
		 */
		_refcnt_ptr(T* ptr)  : m_ptr(ptr)
		{
			_AddRef();
		}

		/**
			Destruct the current instance and try to release the managed pointer
		 */
		~_refcnt_ptr()
		{
			_Release();
		}

		/**
			Overload operator ->
		 */
		T* operator->()
		{
			return static_cast<T*>(m_ptr);
		}

		/**
			Overload operator *
		 */
		T& operator* ()
		{
			return  *static_cast<T*>(m_ptr);
		}

		/**
			Overload cast operator
		 */
		operator T*()
		{
			return static_cast<T*>(m_ptr);
		}

		/**
			Overload operator = 
		 */
		_refcnt_ptr<T>& operator=(const _refcnt_ptr<T>& otherPtr)
		{
			if (this != &otherPtr)
			{
				_Release();
				m_ptr = otherPtr.m_ptr;
				_AddRef();
			}

			return *this;
		}

	private:
		void _AddRef()
		{
			if (m_ptr != nullptr)
			{
				m_ptr->AddRef(true);
				//m_ptr->AddRefPtr();
			}
		}

		void _Release()
		{
			if (m_ptr != nullptr)
			{
				m_ptr->Release(true);
				//m_ptr->ReleaseRefPtr();
			}
		}
	};
}

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

orouit
Architect Consistel - Singapore
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.
 
I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.
 
I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.
 
For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!
 
After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers! I'm currently back in the business to design the licensing system for the enterprise solution I'm currenly working on, using a .NET smart card (yes they can run .NET CLR!)
 
View my profile on LinkedIn
 
You can contact me for professional consulting by using the forum.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 4 Dec 2012
Article Copyright 2012 by orouit
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid