Add RefCounted base class back to the reposotory.

This commit is contained in:
castano 2009-03-05 05:33:53 +00:00
parent 098bc2f905
commit 8f0b129a52
3 changed files with 134 additions and 17 deletions

View File

@ -2,30 +2,24 @@ PROJECT(nvcore)
SET(CORE_SRCS SET(CORE_SRCS
nvcore.h nvcore.h
Algorithms.h
Containers.h
Debug.h Debug.cpp
DefsGnucDarwin.h DefsGnucDarwin.h
DefsGnucLinux.h DefsGnucLinux.h
DefsGnucWin32.h DefsGnucWin32.h
DefsVcWin32.h DefsVcWin32.h
FileSystem.h FileSystem.cpp
Library.h Library.cpp
Memory.h Memory.cpp
Ptr.h Ptr.h
Memory.h RefCounted.h RefCounted.cpp
Memory.cpp StrLib.h StrLib.cpp
Debug.h
Debug.cpp
Containers.h
StrLib.h
StrLib.cpp
Algorithms.h
Timer.h
Library.h
Library.cpp
Stream.h Stream.h
StdStream.h StdStream.h
TextReader.h TextReader.h TextReader.cpp
TextReader.cpp TextWriter.h TextWriter.cpp
TextWriter.h Timer.h)
TextWriter.cpp
FileSystem.h
FileSystem.cpp)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -0,0 +1,9 @@
// This code is in the public domain -- castanyo@yahoo.es
#include "RefCounted.h"
using namespace nv;
int nv::RefCounted::s_total_ref_count = 0;
int nv::RefCounted::s_total_obj_count = 0;

114
src/nvcore/RefCounted.h Normal file
View File

@ -0,0 +1,114 @@
// This code is in the public domain -- castanyo@yahoo.es
#ifndef NV_CORE_REFCOUNTED_H
#define NV_CORE_REFCOUNTED_H
#include <nvcore/nvcore.h>
#include <nvcore/Debug.h>
namespace nv
{
/// Reference counted base class to be used with SmartPtr and WeakPtr.
class RefCounted
{
NV_FORBID_COPY(RefCounted);
public:
/// Ctor.
RefCounted() : m_count(0)/*, m_weak_proxy(NULL)*/
{
s_total_obj_count++;
}
/// Virtual dtor.
virtual ~RefCounted()
{
nvCheck( m_count == 0 );
nvCheck( s_total_obj_count > 0 );
s_total_obj_count--;
}
/// Increase reference count.
uint addRef() const
{
s_total_ref_count++;
m_count++;
return m_count;
}
/// Decrease reference count and remove when 0.
uint release() const
{
nvCheck( m_count > 0 );
s_total_ref_count--;
m_count--;
if( m_count == 0 ) {
// releaseWeakProxy();
delete this;
return 0;
}
return m_count;
}
/*
/// Get weak proxy.
WeakProxy * getWeakProxy() const
{
if (m_weak_proxy == NULL) {
m_weak_proxy = new WeakProxy;
m_weak_proxy->AddRef();
}
return m_weak_proxy;
}
/// Release the weak proxy.
void releaseWeakProxy() const
{
if (m_weak_proxy != NULL) {
m_weak_proxy->NotifyObjectDied();
m_weak_proxy->Release();
m_weak_proxy = NULL;
}
}
*/
/** @name Debug methods: */
//@{
/// Get reference count.
int refCount() const
{
return m_count;
}
/// Get total number of objects.
static int totalObjectCount()
{
return s_total_obj_count;
}
/// Get total number of references.
static int totalReferenceCount()
{
return s_total_ref_count;
}
//@}
private:
NVCORE_API static int s_total_ref_count;
NVCORE_API static int s_total_obj_count;
mutable int m_count;
// mutable WeakProxy * weak_proxy;
};
} // nv namespace
#endif // NV_CORE_REFCOUNTED_H