Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
21Monitors objects for deletion and reflects the deletion by reverting
22the internal pointer to zero. When this pointer is zero we know the
23object has been deleted. This avoids the unsafe TestBit(kNotDeleted)
24hack. The spied object must have the kMustCleanup bit set otherwise
25you will get an error.
26*/
27
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
34TObjectSpy::TObjectSpy(TObject *obj, Bool_t fixMustCleanupBit) :
36{
37 {
39 gROOT->GetListOfCleanups()->Add(this);
40 }
41 if (fObj && !fObj->TestBit(kMustCleanup)) {
42 if (fixMustCleanupBit) {
43 fResetMustCleanupBit = kTRUE;
44 fObj->SetBit(kMustCleanup, kTRUE);
45 } else {
46 Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
47 }
48 }
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Cleanup.
53
55{
57 fObj->SetBit(kMustCleanup, kFALSE);
59 gROOT->GetListOfCleanups()->Remove(this);
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Sets the object pointer to zero if the object is deleted in the
64/// RecursiveRemove() operation.
65
67{
68 if (obj == fObj) {
69 fObj = nullptr;
71 }
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Set obj as the spy target.
76
77void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
78{
80 fObj->SetBit(kMustCleanup, kFALSE);
82
83 fObj = obj;
84
85 if (fObj && !fObj->TestBit(kMustCleanup)) {
86 if (fixMustCleanupBit) {
88 fObj->SetBit(kMustCleanup, kTRUE);
89 } else {
90 Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
91 }
92 }
93}
94
95
96////////////////////////////////////////////////////////////////////////////////
97/// Register the object that must be spied. The object must have the
98/// kMustCleanup bit set. If the object has been deleted during a
99/// RecusiveRemove() operation, GetObject() will return 0.
100
101TObjectRefSpy::TObjectRefSpy(TObject *&obj, Bool_t fixMustCleanupBit) :
103{
104 {
106 gROOT->GetListOfCleanups()->Add(this);
107 }
108 if (fObj && !fObj->TestBit(kMustCleanup)) {
109 if (fixMustCleanupBit) {
110 fResetMustCleanupBit = kTRUE;
111 fObj->SetBit(kMustCleanup, kTRUE);
112 } else {
113 Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
114 }
115 }
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Cleanup.
120
122{
124 fObj->SetBit(kMustCleanup, kFALSE);
126 gROOT->GetListOfCleanups()->Remove(this);
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Sets the object pointer to zero if the object is deleted in the
131/// RecursiveRemove() operation.
132
134{
135 if (obj == fObj) {
136 fObj = nullptr;
138 }
139}
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
#define gROOT
Definition TROOT.h:417
externTVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define R__LOCKGUARD(mutex)
TObject *& fObj
Definition TObjectSpy.h:59
TObjectRefSpy(const TObjectRefSpy &s)=delete
void RecursiveRemove(TObject *obj) override
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation.
virtual ~TObjectRefSpy()
Cleanup.
Bool_t fResetMustCleanupBit
Definition TObjectSpy.h:60
Bool_t fResetMustCleanupBit
Definition TObjectSpy.h:38
void RecursiveRemove(TObject *obj) override
Sets the object pointer to zero if the object is deleted in the RecursiveRemove() operation.
TObject * fObj
Definition TObjectSpy.h:37
virtual ~TObjectSpy()
Cleanup.
TObjectSpy(const TObjectSpy &s)=delete
void SetObject(TObject *obj, Bool_t fixMustCleanupBit=kTRUE)
Set obj as the spy target.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
TObject()
TObject constructor.
Definition TObject.h:259
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:73