/** @file refcounted.h
*	Really lightweight base class for refcounted types
*	@author Chris Burns (cburns@ea.com)
*
*	EABaseTypes
*	(c) 2012 Electronic Arts Inc.
*/

#ifndef EABASETYPESPLUS_REFCOUNTED_H_
#define EABASETYPESPLUS_REFCOUNTED_H_

namespace EA
{
namespace Types
{
	//lint -esym(1539, EA::Types::RefCounted::mRefCount)
	/*! @class	RefCounted refcounted.h "EABaseTypes/refcounted.h"
	*	@brief	Non-virtual base class for simple refcounted objects
	*/
	class RefCounted
	{
	public:

		inline RefCounted();

		/*!
		*	virtual destructor
		*/
		virtual ~RefCounted() {}

		/*!
		*	Add a reference to this object
		*	@return	int	The new reference count
		*/
		inline int AddRef();

		/*!
		*	Release a reference to this object
		*	@return	int	The new reference count
		*/
		inline int Release();

		/*!
		*	Get the current reference count
		*	@return	int	The current reference count
		*/
		inline int GetRefCount() const;

	protected:

		/*!
		*	Delete this object
		*/
		virtual void DeleteThis() = 0;

		int mRefCount;
	};

	/* inline */ RefCounted::RefCounted():
		mRefCount(1)
	{
	}

	/* inline */ int RefCounted::AddRef()
	{
		return ++mRefCount;
	}

	/* inline */ int RefCounted::Release()
	{
		if(--mRefCount <= 0)
		{
			this->DeleteThis();
			return 0;
		}
		else
		{
			return mRefCount;
		}
	}

	/* inline */ int RefCounted::GetRefCount() const
	{
		return mRefCount;
	}
}
}

#endif /* EABASETYPESPLUS_REFCOUNTED_H_ */
