Logo ROOT  
Reference Guide
TObjectSpy.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Matevz Tadel 16/08/2006
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TObjectSpy.h"
13 #include "TSeqCollection.h"
14 #include "TROOT.h"
15 #include "TVirtualMutex.h"
16 
17 /** \class TObjectRefSpy
18  \class TObjectSpy
19 \ingroup Base
20 
21 Monitors objects for deletion and reflects the deletion by reverting
22 the internal pointer to zero. When this pointer is zero we know the
23 object has been deleted. This avoids the unsafe TestBit(kNotDeleted)
24 hack. The spied object must have the kMustCleanup bit set otherwise
25 you will get an error.
26 */
27 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Register the object that must be spied. The object must have the
33 /// kMustCleanup bit set. If the object has been deleted during a
34 /// RecusiveRemove() operation, GetObject() will return 0.
35 
36 TObjectSpy::TObjectSpy(TObject *obj, Bool_t fixMustCleanupBit) :
37  TObject(), fObj(obj), fResetMustCleanupBit(kFALSE)
38 {
39  {
41  gROOT->GetListOfCleanups()->Add(this);
42  }
43  if (fObj && !fObj->TestBit(kMustCleanup)) {
44  if (fixMustCleanupBit) {
47  } else {
48  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
49  }
50  }
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Cleanup.
55 
57 {
61  gROOT->GetListOfCleanups()->Remove(this);
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Sets the object pointer to zero if the object is deleted in the
66 /// RecursiveRemove() operation.
67 
69 {
70  if (obj == fObj) {
71  fObj = nullptr;
73  }
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Set obj as the spy target.
78 
79 void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
80 {
84 
85  fObj = obj;
86 
87  if (fObj && !fObj->TestBit(kMustCleanup)) {
88  if (fixMustCleanupBit) {
91  } else {
92  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
93  }
94  }
95 }
96 
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Register the object that must be spied. The object must have the
100 /// kMustCleanup bit set. If the object has been deleted during a
101 /// RecusiveRemove() operation, GetObject() will return 0.
102 
103 TObjectRefSpy::TObjectRefSpy(TObject *&obj, Bool_t fixMustCleanupBit) :
104  fObj(obj), fResetMustCleanupBit(kFALSE)
105 {
106  {
108  gROOT->GetListOfCleanups()->Add(this);
109  }
110  if (fObj && !fObj->TestBit(kMustCleanup)) {
111  if (fixMustCleanupBit) {
114  } else {
115  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
116  }
117  }
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Cleanup.
122 
124 {
125  if (fObj && fResetMustCleanupBit)
128  gROOT->GetListOfCleanups()->Remove(this);
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Sets the object pointer to zero if the object is deleted in the
133 /// RecursiveRemove() operation.
134 
136 {
137  if (obj == fObj) {
138  fObj = nullptr;
140  }
141 }
TObject::kMustCleanup
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition: TObject.h:60
TObjectSpy::TObjectSpy
TObjectSpy(const TObjectSpy &s)=delete
TObjectSpy::~TObjectSpy
virtual ~TObjectSpy()
Cleanup.
Definition: TObjectSpy.cxx:56
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
TObjectSpy::RecursiveRemove
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation.
Definition: TObjectSpy.cxx:68
TObjectRefSpy::RecursiveRemove
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation.
Definition: TObjectSpy.cxx:135
TObjectSpy.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TObjectSpy::SetObject
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
Definition: TObjectSpy.cxx:79
bool
TROOT.h
TObjectSpy
Monitors objects for deletion and reflects the deletion by reverting the internal pointer to zero.
Definition: TObjectSpy.h:30
TSeqCollection.h
gROOTMutex
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:61
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TObjectRefSpy::fObj
TObject *& fObj
Definition: TObjectSpy.h:59
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
R__LOCKGUARD
#define R__LOCKGUARD(mutex)
Definition: TVirtualMutex.h:104
TObjectRefSpy::~TObjectRefSpy
virtual ~TObjectRefSpy()
Cleanup.
Definition: TObjectSpy.cxx:123
TVirtualMutex.h
TObjectSpy::fObj
TObject * fObj
Definition: TObjectSpy.h:37
TObjectRefSpy::fResetMustCleanupBit
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:60
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TObjectSpy::fResetMustCleanupBit
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:38
TObjectRefSpy::TObjectRefSpy
TObjectRefSpy(const TObjectRefSpy &s)=delete
TObjectRefSpy
Definition: TObjectSpy.h:52
gROOT
#define gROOT
Definition: TROOT.h:406