ROOT logo
// $Id: SRefCounted.h 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/.

#ifndef GledCore_SRefCounted_H
#define GledCore_SRefCounted_H

#include <Rtypes.h>
#include "Gled/GMutex.h"

class SRefCounted
{
protected:
  GMutex  mRCMutex;  //!
  Int_t   mRefCount; //!

public:
  SRefCounted();
  virtual ~SRefCounted();

  virtual void  SetRefCount(Int_t rc);

  virtual Int_t IncRefCount(Int_t rc=1);
  virtual Int_t DecRefCount(Int_t rc=1);

  virtual void  OnZeroRefCount();

  ClassDef(SRefCounted, 0);
}; // endclass SRefCounted


class SRefCountedNV
{
protected:
  GMutex  mRCMutex;  //!
  Int_t   mRefCount; //!

public:
  SRefCountedNV() : mRefCount(0) {}

  void  SetRefCount(Int_t rc)   { GMutexHolder _lck(mRCMutex); mRefCount = rc; }
  Int_t IncRefCount(Int_t rc=1) { GMutexHolder _lck(mRCMutex); mRefCount += rc; return mRefCount; }

  // DecRefCount *must* be implemented in derived class. See define below.
  // Int_t DecRefCount(Int_t rc=1);

  ClassDefNV(SRefCountedNV, 0);
}; // endclass SRefCountedNV

#define SRefCountedNV_DecRefCount_macro \
  Int_t DecRefCount(Int_t rc=1) \
  {				\
    mRCMutex.Lock();		\
    mRefCount -= rc;		\
    Int_t ret = mRefCount;	\
    if (mRefCount <= 0)		\
      delete this;		\
    else			\
      mRCMutex.Unlock();	\
    return ret;			\
  }

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