ROOT logo
// $Id: SRefCounted.cxx 2663 2012-02-07 23:01:04Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#include "SRefCounted.h"

//__________________________________________________________________________
//
// A reference-counted stone.
// Note that the ref-count is not serialized -- so rebuild it in
// post-streaming function when necessary.
// When zero ref count is reached in DecRefCount(),
//   virtual void OnZeroRefCount();
// is called. Default action is to destroy the object. Note, mutex is NOT
// unlocked in this case ... so if you keep the object alive in OnZeroRefCnt()
// also unlock the mutex.

ClassImp(SRefCounted);

SRefCounted::SRefCounted() : mRefCount(0)
{}

SRefCounted::~SRefCounted()
{}

void SRefCounted::SetRefCount(Int_t rc)
{
  GMutexHolder _lck(mRCMutex);
  mRefCount = rc;
}

Int_t SRefCounted::IncRefCount(Int_t rc)
{
  GMutexHolder _lck(mRCMutex);
  mRefCount += rc;
  return mRefCount;
}

Int_t SRefCounted::DecRefCount(Int_t rc)
{
  mRCMutex.Lock();
  mRefCount -= rc;
  Int_t ret = mRefCount;
  if (mRefCount <= 0)
    OnZeroRefCount();
  else
    mRCMutex.Unlock();
  return ret;
}

void SRefCounted::OnZeroRefCount()
{
  delete this;
}


//__________________________________________________________________________
//
// A reference-counted stone without virtual functions, all functions inline.
// Note that the ref-count is not serialized -- so rebuild it in
// post-streaming function when necessary.
// As there are no virtual functions
//   Int_t DecRefCount(Int_t rc=1);
// *must* be implmented in the sub-class. Macro
//   SRefCountedNV_DecRefCount_macro
// is defined for this purpose in the header file. This implementation
// destructs the object when zero ref count is reached.

ClassImp(SRefCountedNV);
 SRefCounted.cxx:1
 SRefCounted.cxx:2
 SRefCounted.cxx:3
 SRefCounted.cxx:4
 SRefCounted.cxx:5
 SRefCounted.cxx:6
 SRefCounted.cxx:7
 SRefCounted.cxx:8
 SRefCounted.cxx:9
 SRefCounted.cxx:10
 SRefCounted.cxx:11
 SRefCounted.cxx:12
 SRefCounted.cxx:13
 SRefCounted.cxx:14
 SRefCounted.cxx:15
 SRefCounted.cxx:16
 SRefCounted.cxx:17
 SRefCounted.cxx:18
 SRefCounted.cxx:19
 SRefCounted.cxx:20
 SRefCounted.cxx:21
 SRefCounted.cxx:22
 SRefCounted.cxx:23
 SRefCounted.cxx:24
 SRefCounted.cxx:25
 SRefCounted.cxx:26
 SRefCounted.cxx:27
 SRefCounted.cxx:28
 SRefCounted.cxx:29
 SRefCounted.cxx:30
 SRefCounted.cxx:31
 SRefCounted.cxx:32
 SRefCounted.cxx:33
 SRefCounted.cxx:34
 SRefCounted.cxx:35
 SRefCounted.cxx:36
 SRefCounted.cxx:37
 SRefCounted.cxx:38
 SRefCounted.cxx:39
 SRefCounted.cxx:40
 SRefCounted.cxx:41
 SRefCounted.cxx:42
 SRefCounted.cxx:43
 SRefCounted.cxx:44
 SRefCounted.cxx:45
 SRefCounted.cxx:46
 SRefCounted.cxx:47
 SRefCounted.cxx:48
 SRefCounted.cxx:49
 SRefCounted.cxx:50
 SRefCounted.cxx:51
 SRefCounted.cxx:52
 SRefCounted.cxx:53
 SRefCounted.cxx:54
 SRefCounted.cxx:55
 SRefCounted.cxx:56
 SRefCounted.cxx:57
 SRefCounted.cxx:58
 SRefCounted.cxx:59
 SRefCounted.cxx:60
 SRefCounted.cxx:61
 SRefCounted.cxx:62
 SRefCounted.cxx:63
 SRefCounted.cxx:64
 SRefCounted.cxx:65
 SRefCounted.cxx:66
 SRefCounted.cxx:67
 SRefCounted.cxx:68
 SRefCounted.cxx:69
 SRefCounted.cxx:70
 SRefCounted.cxx:71