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