1 // @(#)root/base:$Id$ 2 // Author: Rene Brun 28/11/94 3 4 /************************************************************************* 5 * Copyright (C) 1995-2000, 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 #ifndef ROOT_TDirectory 13 #define ROOT_TDirectory 14 15 16 ////////////////////////////////////////////////////////////////////////// 17 // // 18 // TDirectory // 19 // // 20 // Describe directory structure in memory. // 21 // // 22 ////////////////////////////////////////////////////////////////////////// 23 24 #ifndef ROOT_TNamed 25 #include "TNamed.h" 26 #endif 27 #ifndef ROOT_TList 28 #include "TList.h" 29 #endif 30 #ifndef ROOT_TDatime 31 #include "TDatime.h" 32 #endif 33 #ifndef ROOT_TUUID 34 #include "TUUID.h" 35 #endif 36 37 class TBrowser; 38 class TKey; 39 class TFile; 40 41 class TDirectory : public TNamed { 42 public: 43 /** @class Context 44 * 45 * Small helper to keep current directory context. 46 * Automatically reverts to "old" directory 47 */ 48 class TContext { 49 private: 50 TDirectory *fDirectory; //! Pointer to the previous current directory. 51 TContext *fPrevious; //! Pointer to the next TContext in the implied list of context pointing to fPrevious. 52 TContext *fNext; //! Pointer to the next TContext in the implied list of context pointing to fPrevious. 53 TContext(TContext&); 54 TContext& operator=(TContext&); 55 void CdNull(); 56 friend class TDirectory; 57 public: 58 TContext(TDirectory* previous, TDirectory* newCurrent) 59 : fDirectory(previous),fPrevious(0),fNext(0) 60 { 61 // Store the current directory so we can restore it 62 // later and cd to the new directory. 63 if ( fDirectory ) fDirectory->RegisterContext(this); 64 if ( newCurrent ) newCurrent->cd(); 65 } 66 TContext(TDirectory* newCurrent) : fDirectory(TDirectory::CurrentDirectory()),fPrevious(0),fNext(0) 67 { 68 // Store the current directory so we can restore it 69 // later and cd to the new directory. 70 if ( fDirectory ) fDirectory->RegisterContext(this); 71 if ( newCurrent ) newCurrent->cd(); 72 } 73 ~TContext() 74 { 75 // Destructor. Reset the current directory to its 76 // previous state. 77 if ( fDirectory ) { 78 fDirectory->UnregisterContext(this); 79 fDirectory->cd(); 80 } 81 else CdNull(); 82 } 83 }; 84 85 protected: 86 87 TObject *fMother; //pointer to mother of the directory 88 TList *fList; //List of objects in memory 89 TUUID fUUID; //Unique identifier 90 TString fPathBuffer; //!Buffer for GetPath() function 91 TContext *fContext; //!Pointer to a list of TContext object pointing to this TDirectory 92 static Bool_t fgAddDirectory; //!flag to add histograms, graphs,etc to the directory 93 94 Bool_t cd1(const char *path); 95 static Bool_t Cd1(const char *path); 96 97 virtual void CleanTargets(); 98 void FillFullPath(TString& buf) const; 99 void RegisterContext(TContext *ctxt); 100 void UnregisterContext(TContext *ctxt); 101 friend class TContext; 102 103 protected: 104 TDirectory(const TDirectory &directory); //Directories cannot be copied 105 void operator=(const TDirectory &); //Directorise cannot be copied 106 107 public: 108 109 TDirectory(); 110 TDirectory(const char *name, const char *title, Option_t *option="", TDirectory* motherDir = 0); 111 virtual ~TDirectory(); 112 static void AddDirectory(Bool_t add=kTRUE); 113 static Bool_t AddDirectoryStatus(); 114 virtual void Append(TObject *obj, Bool_t replace = kFALSE); 115 virtual void Add(TObject *obj, Bool_t replace = kFALSE) { Append(obj,replace); } 116 virtual Int_t AppendKey(TKey *) {return 0;} 117 virtual void Browse(TBrowser *b); 118 virtual void Build(TFile* motherFile = 0, TDirectory* motherDir = 0); 119 virtual void Clear(Option_t *option=""); 120 virtual TObject *CloneObject(const TObject *obj, Bool_t autoadd = kTRUE); 121 virtual void Close(Option_t *option=""); 122 static TDirectory *&CurrentDirectory(); // Return the current directory for this thread. 123 virtual void Copy(TObject &) const { MayNotUse("Copy(TObject &)"); } 124 virtual Bool_t cd(const char *path = 0); 125 virtual void DeleteAll(Option_t *option=""); 126 virtual void Delete(const char *namecycle=""); 127 virtual void Draw(Option_t *option=""); 128 virtual TKey *FindKey(const char * /*keyname*/) const {return 0;} 129 virtual TKey *FindKeyAny(const char * /*keyname*/) const {return 0;} 130 virtual TObject *FindObject(const char *name) const; 131 virtual TObject *FindObject(const TObject *obj) const; 132 virtual TObject *FindObjectAny(const char *name) const; 133 virtual TObject *FindObjectAnyFile(const char * /*name*/) const {return 0;} 134 virtual TObject *Get(const char *namecycle); 135 virtual TDirectory *GetDirectory(const char *namecycle, Bool_t printError = false, const char *funcname = "GetDirectory"); 136 template <class T> inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for information 137 { 138 ptr = (T*)GetObjectChecked(namecycle,TBuffer::GetClass(typeid(T))); 139 } 140 virtual void *GetObjectChecked(const char *namecycle, const char* classname); 141 virtual void *GetObjectChecked(const char *namecycle, const TClass* cl); 142 virtual void *GetObjectUnchecked(const char *namecycle); 143 virtual Int_t GetBufferSize() const {return 0;} 144 virtual TFile *GetFile() const { return 0; } 145 virtual TKey *GetKey(const char * /*name */, Short_t /* cycle */=9999) const {return 0;} 146 virtual TList *GetList() const { return fList; } 147 virtual TList *GetListOfKeys() const { return 0; } 148 virtual TObject *GetMother() const { return fMother; } 149 virtual TDirectory *GetMotherDir() const { return fMother==0 ? 0 : dynamic_cast<TDirectory*>(fMother); } 150 virtual Int_t GetNbytesKeys() const { return 0; } 151 virtual Int_t GetNkeys() const { return 0; } 152 virtual Long64_t GetSeekDir() const { return 0; } 153 virtual Long64_t GetSeekParent() const { return 0; } 154 virtual Long64_t GetSeekKeys() const { return 0; } 155 virtual const char *GetPathStatic() const; 156 virtual const char *GetPath() const; 157 TUUID GetUUID() const {return fUUID;} 158 virtual Bool_t IsFolder() const { return kTRUE; } 159 virtual Bool_t IsModified() const { return kFALSE; } 160 virtual Bool_t IsWritable() const { return kFALSE; } 161 virtual void ls(Option_t *option="") const; 162 virtual TDirectory *mkdir(const char *name, const char *title=""); 163 virtual TFile *OpenFile(const char * /*name*/, Option_t * /*option*/ = "", 164 const char * /*ftitle*/ = "", Int_t /*compress*/ = 1, 165 Int_t /*netopt*/ = 0) {return 0;} 166 virtual void Paint(Option_t *option=""); 167 virtual void Print(Option_t *option="") const; 168 virtual void Purge(Short_t /*nkeep*/=1) {} 169 virtual void pwd() const; 170 virtual void ReadAll(Option_t * /*option*/="") {} 171 virtual Int_t ReadKeys(Bool_t /*forceRead*/=kTRUE) {return 0;} 172 virtual Int_t ReadTObject(TObject * /*obj*/, const char * /*keyname*/) {return 0;} 173 virtual TObject *Remove(TObject*); 174 virtual void RecursiveRemove(TObject *obj); 175 virtual void rmdir(const char *name); 176 virtual void Save() {} 177 virtual Int_t SaveObjectAs(const TObject * /*obj*/, const char * /*filename*/="", Option_t * /*option*/="") const; 178 virtual void SaveSelf(Bool_t /*force*/ = kFALSE) {} 179 virtual void SetBufferSize(Int_t /* bufsize */) {} 180 virtual void SetModified() {} 181 virtual void SetMother(TObject *mother) {fMother = (TObject*)mother;} 182 virtual void SetName(const char* newname); 183 virtual void SetTRefAction(TObject * /*ref*/, TObject * /*parent*/) {} 184 virtual void SetSeekDir(Long64_t) {} 185 virtual void SetWritable(Bool_t) {} 186 virtual Int_t Sizeof() const {return 0;} 187 virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0){return 0;} 188 virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0) const {return 0;} 189 virtual Int_t WriteTObject(const TObject *obj, const char *name =0, Option_t * /*option*/="", Int_t /*bufsize*/ =0); 190 template <class T> inline Int_t WriteObject(const T* obj, const char* name, Option_t *option="", Int_t bufsize=0) // see TDirectory::WriteTObject or TDirectoryWriteObjectAny for explanation 191 { 192 return WriteObjectAny(obj,TBuffer::GetClass(typeid(T)),name,option,bufsize); 193 } 194 virtual Int_t WriteObjectAny(const void *, const char * /*classname*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;} 195 virtual Int_t WriteObjectAny(const void *, const TClass * /*cl*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;} 196 virtual void WriteDirHeader() {} 197 virtual void WriteKeys() {} 198 199 static Bool_t Cd(const char *path); 200 static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize = 0); 201 static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle); 202 203 ClassDef(TDirectory,5) //Describe directory structure in memory 204 }; 205 206 #ifndef __CINT__ 207 #define gDirectory (TDirectory::CurrentDirectory()) 208 209 #elif defined(__MAKECINT__) 210 // To properly handle the use of gDirectory in header files (in static declarations) 211 R__EXTERN TDirectory *gDirectory; 212 #endif 213 214 #endif 215