Logo ROOT   6.08/07
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 \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  gROOT->GetListOfCleanups()->Add(this);
39  if (fObj && !fObj->TestBit(kMustCleanup)) {
40  if (fixMustCleanupBit) {
41  fResetMustCleanupBit = kTRUE;
42  fObj->SetBit(kMustCleanup, kTRUE);
43  } else {
44  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
45  }
46  }
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Cleanup.
51 
53 {
56  gROOT->GetListOfCleanups()->Remove(this);
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Sets the object pointer to zero if the object is deleted in the
61 /// RecursiveRemove() operation.
62 
64 {
65  if (obj == fObj) {
66  fObj = 0;
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Set obj as the spy target.
73 
74 void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
75 {
79 
80  fObj = obj;
81 
82  if (fObj && !fObj->TestBit(kMustCleanup)) {
83  if (fixMustCleanupBit) {
86  } else {
87  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
88  }
89  }
90 }
91 
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Register the object that must be spied. The object must have the
95 /// kMustCleanup bit set. If the object has been deleted during a
96 /// RecusiveRemove() operation, GetObject() will return 0.
97 
98 TObjectRefSpy::TObjectRefSpy(TObject *&obj, Bool_t fixMustCleanupBit) :
100 {
101  gROOT->GetListOfCleanups()->Add(this);
102  if (fObj && !fObj->TestBit(kMustCleanup)) {
103  if (fixMustCleanupBit) {
106  } else {
107  Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
108  }
109  }
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Cleanup.
114 
116 {
117  if (fObj && fResetMustCleanupBit)
119  gROOT->GetListOfCleanups()->Remove(this);
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Sets the object pointer to zero if the object is deleted in the
124 /// RecursiveRemove() operation.
125 
127 {
128  if (obj == fObj) {
129  fObj = 0;
131  }
132 }
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:126
virtual void RecursiveRemove(TObject *obj)
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation...
Definition: TObjectSpy.cxx:63
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:157
#define gROOT
Definition: TROOT.h:364
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:739
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
Definition: TObjectSpy.cxx:74
void Error(const char *location, const char *msgfmt,...)
TObject *& fObj
Definition: TObjectSpy.h:61
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:925
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:40
if object destructor must call RecursiveRemove()
Definition: TObject.h:57
virtual ~TObjectRefSpy()
Cleanup.
Definition: TObjectSpy.cxx:115
#define ClassImp(name)
Definition: Rtypes.h:279
Mother of all ROOT objects.
Definition: TObject.h:37
Bool_t fResetMustCleanupBit
Definition: TObjectSpy.h:62
TObject * fObj
Definition: TObjectSpy.h:39
virtual ~TObjectSpy()
Cleanup.
Definition: TObjectSpy.cxx:52
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