/********************************************************************
 * File: RefCountedObject.h
 * Author: Keith Schwarz (htiek@cs.stanford.edu)
 *
 * A base class that makes an object reference-counted and allocable only
 * on the heap.  Entities should derive from this class.
 */
#ifndef RefCountedObject_Included
#define RefCountedObject_Included

#include <stdlib.h>

namespace Copper3D
{
	class RefCountedObject
	{
	public:
		/* Query the reference count. */
		size_t referenceCount() const;

	protected:
		/* Can only instantiate from derived classes. */
		RefCountedObject();

		/* Cannot be destroyed outside of the refcounting system. */
		virtual ~RefCountedObject();

	private:
		
		/* Reference-counted objects can't be copied or assigned. */
		RefCountedObject(const RefCountedObject&);
		RefCountedObject& operator= (const RefCountedObject&);

		/* The actual reference count. */
		mutable size_t refCount;

		/* Modify the reference count.  This can only be invoked by the Ptr
		 * class since no one else should be messing with it.
		 */
		void addRef() const;
		void deleteRef() const;

		/* Only Ptr can manage the reference count. */
		template <typename T> friend class Ptr;
	};
}

#endif