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
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
36TObjectSpy::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
79void 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
103TObjectRefSpy::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{
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}
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
#define ClassImp(name)
Definition Rtypes.h:382
R__EXTERN TVirtualMutex * gROOTMutex
Definition TROOT.h:63
#define gROOT
Definition TROOT.h:406
#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
Monitors objects for deletion and reflects the deletion by reverting the internal pointer to zero.
Definition TObjectSpy.h:30
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.
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:798
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1005
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:64