Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
#include "tthread.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef TAtomicVar *TAtomicVarPtr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int maxClassCode = 100;
Toshihiro Shimizu 890ddd
TAtomicVarPtr instanceCounts[maxClassCode + 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline TAtomicVar &getInstanceCounter(long classCode)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0 <= classCode && classCode <= maxClassCode);
Toshihiro Shimizu 890ddd
	TAtomicVarPtr &instanceCountPtr = instanceCounts[classCode];
Toshihiro Shimizu 890ddd
	if (instanceCountPtr == 0) {
Toshihiro Shimizu 890ddd
		static TThread::Mutex mutex;
Toshihiro Shimizu 890ddd
		TThread::ScopedLock g(mutex);
Toshihiro Shimizu 890ddd
		if (instanceCountPtr == 0)
Toshihiro Shimizu 890ddd
			instanceCountPtr = new TAtomicVar();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	assert(instanceCountPtr);
Toshihiro Shimizu 890ddd
	return *instanceCountPtr;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const long TSmartObject::m_unknownClassCode = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSmartObject::incrementInstanceCount()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef INSTANCE_COUNT_ENABLED
Toshihiro Shimizu 890ddd
	TAtomicVar &instanceCount = getInstanceCounter(m_classCodeRef);
Toshihiro Shimizu 890ddd
	++instanceCount;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	assert(0);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSmartObject::decrementInstanceCount()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef INSTANCE_COUNT_ENABLED
Toshihiro Shimizu 890ddd
	TAtomicVar &instanceCount = getInstanceCounter(m_classCodeRef);
Toshihiro Shimizu 890ddd
	assert(instanceCount > 0);
Toshihiro Shimizu 890ddd
	--instanceCount;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	assert(0);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
long TSmartObject::getInstanceCount(ClassCode code)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef INSTANCE_COUNT_ENABLED
Toshihiro Shimizu 890ddd
	TAtomicVar &instanceCount = getInstanceCounter(code);
Toshihiro Shimizu 890ddd
	return instanceCount;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	assert(0);
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------