Logo ROOT   6.14/05
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 "TROOT.h"
14 #include "TVirtualMutex.h"
15 
16 /** \class TObjectRefSpy
17  \class TObjectSpy
18 \ingroup Base
19 
20 Monitors objects for deletion and reflects the deletion by reverting
21 the internal pointer to zero. When this pointer is zero we know the
22 object has been deleted. This avoids the unsafe TestBit(kNotDeleted)
23 hack. The spied object must have the kMustCleanup bit set otherwise
24 you will get an error.
25 */
26 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Register the object that must be spied. The object must have the
32 /// kMustCleanup bit set. If the object has been deleted during a
33 /// RecusiveRemove() operation, GetObject() will return 0.
34 
35 TObjectSpy::TObjectSpy(TObject *obj, Bool_t fixMustCleanupBit) :
36  TObject(), fObj(obj), fResetMustCleanupBit(kFALSE)
37 {
38  {
40  gROOT->GetListOfCleanups()->Add(this);
41  }
42  if (fObj && !fObj->TestBit(kMustCleanup)) {
43  if (fixMustCleanupBit) {
46  } else {
47  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
48  }
49  }
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Cleanup.
54 
56 {
60  gROOT->GetListOfCleanups()->Remove(this);
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Sets the object pointer to zero if the object is deleted in the
65 /// RecursiveRemove() operation.
66 
68 {
69  if (obj == fObj) {
70  fObj = 0;
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////////////////
76 /// Set obj as the spy target.
77 
78 void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
79 {
83 
84  fObj = obj;
85 
86  if (fObj && !fObj->TestBit(kMustCleanup)) {
87  if (fixMustCleanupBit) {
90  } else {
91  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
92  }
93  }
94 }
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Register the object that must be spied. The object must have the
99 /// kMustCleanup bit set. If the object has been deleted during a
100 /// RecusiveRemove() operation, GetObject() will return 0.
101 
102 TObjectRefSpy::TObjectRefSpy(TObject *&obj, Bool_t fixMustCleanupBit) :
104 {
105  {
107  gROOT->GetListOfCleanups()->Add(this);
108  }
109  if (fObj && !fObj->TestBit(kMustCleanup)) {
110  if (fixMustCleanupBit) {
113  } else {
114  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
115  }
116  }
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// Cleanup.
121 
123 {
124  if (fObj && fResetMustCleanupBit)
127  gROOT->GetListOfCleanups()->Remove(this);
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Sets the object pointer to zero if the object is deleted in the
132 /// RecursiveRemove() operation.
133 
135 {
136  if (obj == fObj) {
137  fObj = 0;
139  }
140 }
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:134
TObjectSpy(const TObjectSpy &s)
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:67
#define gROOT
Definition: TROOT.h:410
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
TObjectRefSpy(const TObjectRefSpy &s)
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
Definition: TObjectSpy.cxx:78
TObject *& fObj
Definition: TObjectSpy.h:59
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:38
if object destructor must call RecursiveRemove()
Definition: TObject.h:60
virtual ~TObjectRefSpy()
Cleanup.
Definition: TObjectSpy.cxx:122
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
#define R__LOCKGUARD(mutex)
Mother of all ROOT objects.
Definition: TObject.h:37
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:60
TObject * fObj
Definition: TObjectSpy.h:37
virtual ~TObjectSpy()
Cleanup.
Definition: TObjectSpy.cxx:55
Monitors objects for deletion and reflects the deletion by reverting the internal pointer to zero...
Definition: TObjectSpy.h:30
const Bool_t kTRUE
Definition: RtypesCore.h:87