#ifndef REFCOUNT_H
#define REFCOUNT_H

#include <stddef.h>
#include <stdint.h>
#include <assert.h>

class RefCount
{
public:
	// Virtual destructor
	virtual ~RefCount() {}
	
	// Add reference
	uint64_t AddRef();
	
	// Release (remove reference and destroy object at zero reference's)
	uint64_t Release();
	
private:
	// Ref counting
	uint64_t m_RefCount = 0;
};

inline uint64_t RefCount::AddRef()
{
	++m_RefCount;
	return m_RefCount;
}

inline uint64_t RefCount::Release()
{
	assert(m_RefCount > 0);
	--m_RefCount;

	if (m_RefCount == 0)
	{
		delete this; 
		return 0; 
	}

	return m_RefCount;
}

#endif
