ROOT  6.06/09
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 
15 
16 /** \class TObjectRefSpy
17  \class TObjectSpy
18 
19 Monitors objects for deletion and reflects the deletion by reverting
20 the internal pointer to zero. When this pointer is zero we know the
21 object has been deleted. This avoids the unsafe TestBit(kNotDeleted)
22 hack. The spied object must have the kMustCleanup bit set otherwise
23 you will get an error.
24 */
25 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Register the object that must be spied. The object must have the
31 /// kMustCleanup bit set. If the object has been deleted during a
32 /// RecusiveRemove() operation, GetObject() will return 0.
33 
34 TObjectSpy::TObjectSpy(TObject *obj, Bool_t fixMustCleanupBit) :
35  TObject(), fObj(obj), fResetMustCleanupBit(kFALSE)
36 {
37  gROOT->GetListOfCleanups()->Add(this);
38  if (fObj && !fObj->TestBit(kMustCleanup)) {
39  if (fixMustCleanupBit) {
40  fResetMustCleanupBit = kTRUE;
41  fObj->SetBit(kMustCleanup, kTRUE);
42  } else {
43  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
44  }
45  }
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Cleanup.
50 
52 {
55  gROOT->GetListOfCleanups()->Remove(this);
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Sets the object pointer to zero if the object is deleted in the
60 /// RecursiveRemove() operation.
61 
63 {
64  if (obj == fObj) {
65  fObj = 0;
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Set obj as the spy target.
72 
73 void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
74 {
78 
79  fObj = obj;
80 
81  if (fObj && !fObj->TestBit(kMustCleanup)) {
82  if (fixMustCleanupBit) {
85  } else {
86  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
87  }
88  }
89 }
90 
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Register the object that must be spied. The object must have the
94 /// kMustCleanup bit set. If the object has been deleted during a
95 /// RecusiveRemove() operation, GetObject() will return 0.
96 
98  fObj(obj), fResetMustCleanupBit(kFALSE)
99 {
100  gROOT->GetListOfCleanups()->Add(this);
101  if (fObj && !fObj->TestBit(kMustCleanup)) {
102  if (fixMustCleanupBit) {
105  } else {
106  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
107  }
108  }
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Cleanup.
113 
115 {
116  if (fObj && fResetMustCleanupBit)
118  gROOT->GetListOfCleanups()->Remove(this);
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Sets the object pointer to zero if the object is deleted in the
123 /// RecursiveRemove() operation.
124 
126 {
127  if (obj == fObj) {
128  fObj = 0;
130  }
131 }
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:125
void * fObj
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:62
ClassImp(TObjectSpy) ClassImp(TObjectRefSpy) TObjectSpy
Register the object that must be spied.
Definition: TObjectSpy.cxx:26
#define gROOT
Definition: TROOT.h:340
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
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:732
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
Definition: TObjectSpy.cxx:73
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void Error(const char *location, const char *msgfmt,...)
TObject *& fObj
Definition: TObjectSpy.h:61
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:40
virtual ~TObjectRefSpy()
Cleanup.
Definition: TObjectSpy.cxx:114
Mother of all ROOT objects.
Definition: TObject.h:58
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:62
TObject * fObj
Definition: TObjectSpy.h:39
virtual ~TObjectSpy()
Cleanup.
Definition: TObjectSpy.cxx:51
const Bool_t kTRUE
Definition: Rtypes.h:91
Monitors objects for deletion and reflects the deletion by reverting the internal pointer to zero...
Definition: TObjectSpy.h:32
TObject * obj