Click here to Skip to main content
12,072,809 members (73,942 online)
Click here to Skip to main content

Tagged as

Stats

11.5K views
224 downloads
17 bookmarked
Posted

Reference counted smart pointer

, 4 Dec 2012 CPOL
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

#if defined(TESTCLASSIMPL_EXPORTS)
#define REFCNTDECL __declspec(dllexport)
#else
#define REFCNTDECL __declspec(dllimport)
#endif

namespace CoreImpl
{
	/**
		The reference counter interface. Abstract class in C++ 
	*/
	class REFCNTDECL IRefCount
	{
		friend class CRefCount;
		
	public:
		/**
			Call to increment the reference count
		 */
		virtual void AddRef(bool isSmartPtr = false) = 0;

		/**
			Call to decrement the reference count until it's 0 and call delete 
			on the object pointer itself
		 */
		virtual void Release(bool isSmartPtr = false) = 0;

	protected:
		virtual bool CanReleaseRef() = 0;
	};

	/**
		Implements the IRefCount interface. A class that want's to have reference counting
		capability must inherit from that class. As C++ supports multiple inheritance this 
		is just a matter of inheriting from that class.
	 */
	class REFCNTDECL CRefCount : public IRefCount
	{
	private:
		int refCount;		// The reference counter
		int refPtrCount;	// Number of _refcnt_ptr monitoring that class instance

	protected:
		/**
			Default contructor, initializes the CRefCount object
		 */
		CRefCount();

	public:
		/**
			Overload of the delete operator for all the objects based on that 
			class
		 */
		void operator delete(void *ptr);

		/**
			Call to increment the reference count
		 */
		virtual void AddRef(bool isSmartPtr = false);

	/**
		Implements the IRefCount interface. A class that want's to have reference counting
		capability must inherit from that class. As C++ supports multiple inheritance this 
		is just a matter of inheriting from that class.
	 */
		virtual void Release(bool isSmartPtr = false);

	protected:
		virtual bool CanReleaseRef();
	};
}

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 currently working on, using a .NET smart card (yes they can run .NET CLR!)

View my profile on LinkedIn

I have recently added to my expertise the DESFire family card and NFC payment systems. This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provided professional consulting for it.

You can contact me for professional consulting by using the forum or via my LinkedIn profile.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 4 Dec 2012
Article Copyright 2012 by orouit
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid