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
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 = 100;
Toshihiro Shimizu 890ddd
TAtomicVarPtr instanceCounts[maxClassCode + 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline TAtomicVar &getInstanceCounter(long 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::ScopedLock 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
const long TSmartObject::m_unknownClassCode = 0;
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
Shinya Kitaoka 120a6e
long TSmartObject::getInstanceCount(ClassCode code) {
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
//-------------------------------------------------------------------