TObjectSpy.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TObjectSpy.cxx 22638 2008-03-13 15:00:12Z brun $
00002 // Author: Matevz Tadel   16/08/2006
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TObjectSpy.h"
00013 #include "TROOT.h"
00014 
00015 
00016 //////////////////////////////////////////////////////////////////////////
00017 //                                                                      //
00018 // TObjectSpy, TObjectRefSpy                                            //
00019 //                                                                      //
00020 // Monitors objects for deletion and reflects the deletion by reverting //
00021 // the internal pointer to zero. When this pointer is zero we know the  //
00022 // object has been deleted. This avoids the unsafe TestBit(kNotDeleted) //
00023 // hack. The spied object must have the kMustCleanup bit set otherwise  //
00024 // you will get an error.                                               //
00025 //                                                                      //
00026 //////////////////////////////////////////////////////////////////////////
00027 
00028 ClassImp(TObjectSpy)
00029 ClassImp(TObjectRefSpy)
00030 
00031 //______________________________________________________________________________
00032 TObjectSpy::TObjectSpy(TObject *obj, Bool_t fixMustCleanupBit) :
00033    TObject(), fObj(obj), fResetMustCleanupBit(kFALSE)
00034 {
00035    // Register the object that must be spied. The object must have the
00036    // kMustCleanup bit set. If the object has been deleted during a
00037    // RecusiveRemove() operation, GetObject() will return 0.
00038 
00039    gROOT->GetListOfCleanups()->Add(this);
00040    if (fObj && !fObj->TestBit(kMustCleanup)) {
00041       if (fixMustCleanupBit) {
00042          fResetMustCleanupBit = kTRUE;
00043          fObj->SetBit(kMustCleanup, kTRUE);
00044       } else {
00045          Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
00046       }
00047    }
00048 }
00049 
00050 //______________________________________________________________________________
00051 TObjectSpy::~TObjectSpy()
00052 {
00053    // Cleanup.
00054 
00055    if (fObj && fResetMustCleanupBit)
00056       fObj->SetBit(kMustCleanup, kFALSE);
00057    gROOT->GetListOfCleanups()->Remove(this);
00058 }
00059 
00060 //______________________________________________________________________________
00061 void TObjectSpy::RecursiveRemove(TObject *obj)
00062 {
00063    // Sets the object pointer to zero if the object is deleted in the
00064    // RecursiveRemove() operation.
00065 
00066    if (obj == fObj) {
00067       fObj = 0;
00068       fResetMustCleanupBit = kFALSE;
00069    }
00070 }
00071 
00072 //______________________________________________________________________________
00073 void TObjectSpy::SetObject(TObject *obj, Bool_t fixMustCleanupBit)
00074 {
00075    // Set obj as the spy target.
00076 
00077    if (fObj && fResetMustCleanupBit)
00078       fObj->SetBit(kMustCleanup, kFALSE);
00079    fResetMustCleanupBit = kFALSE;
00080 
00081    fObj = obj;
00082 
00083    if (fObj && !fObj->TestBit(kMustCleanup)) {
00084       if (fixMustCleanupBit) {
00085          fResetMustCleanupBit = kTRUE;
00086          fObj->SetBit(kMustCleanup, kTRUE);
00087       } else {
00088          Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
00089       }
00090    }
00091 }
00092 
00093 
00094 //______________________________________________________________________________
00095 TObjectRefSpy::TObjectRefSpy(TObject *&obj, Bool_t fixMustCleanupBit) :
00096    fObj(obj), fResetMustCleanupBit(kFALSE)
00097 {
00098    // Register the object that must be spied. The object must have the
00099    // kMustCleanup bit set. If the object has been deleted during a
00100    // RecusiveRemove() operation, GetObject() will return 0.
00101 
00102    gROOT->GetListOfCleanups()->Add(this);
00103    if (fObj && !fObj->TestBit(kMustCleanup)) {
00104       if (fixMustCleanupBit) {
00105          fResetMustCleanupBit = kTRUE;
00106          fObj->SetBit(kMustCleanup, kTRUE);
00107       } else {
00108          Error("TObjectSpy", "spied object must have the kMustCleanup bit set");
00109       }
00110    }
00111 }
00112 
00113 //______________________________________________________________________________
00114 TObjectRefSpy::~TObjectRefSpy()
00115 {
00116    // Cleanup.
00117 
00118    if (fObj && fResetMustCleanupBit)
00119       fObj->SetBit(kMustCleanup, kFALSE);
00120    gROOT->GetListOfCleanups()->Remove(this);
00121 }
00122 
00123 //______________________________________________________________________________
00124 void TObjectRefSpy::RecursiveRemove(TObject *obj)
00125 {
00126    // Sets the object pointer to zero if the object is deleted in the
00127    // RecursiveRemove() operation.
00128 
00129    if (obj == fObj) {
00130       fObj = 0;
00131       fResetMustCleanupBit = kFALSE;
00132    }
00133 }

Generated on Tue Jul 5 14:11:21 2011 for ROOT_528-00b_version by  doxygen 1.5.1