Logo ROOT   6.14/05
Reference Guide
TDirectory.h
Go to the documentation of this file.
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 #include "TBuffer.h"
25 #include "TNamed.h"
26 #include "TList.h"
27 #include "TDatime.h"
28 #include "TUUID.h"
29 
30 class TBrowser;
31 class TKey;
32 class TFile;
33 
34 class TDirectory : public TNamed {
35 public:
36  /** @class Context
37  *
38  * Small helper to keep current directory context.
39  * Automatically reverts to "old" directory
40  */
41  class TContext {
42  private:
43  std::atomic<TDirectory*> fDirectory; //! Pointer to the previous current directory.
44  std::atomic<bool> fActiveDestructor; //! Set to true during the destructor execution
45  std::atomic<bool> fDirectoryWait; //! Set to true if a TDirectory might still access this object.
46 
47  TContext *fPrevious; //! Pointer to the next TContext in the implied list of context pointing to fPrevious.
48  TContext *fNext; //! Pointer to the next TContext in the implied list of context pointing to fPrevious.
51  void CdNull();
52  friend class TDirectory;
53  public:
54  TContext(TDirectory *previous, TDirectory *newCurrent)
55  : fDirectory(previous), fActiveDestructor(false), fDirectoryWait(false), fPrevious(0), fNext(0)
56  {
57  // Store the current directory so we can restore it
58  // later and cd to the new directory.
59  if ( fDirectory ) (*fDirectory).RegisterContext(this);
60  if ( newCurrent ) newCurrent->cd();
61  else CdNull();
62  }
64  : fDirectory(TDirectory::CurrentDirectory()), fActiveDestructor(false), fDirectoryWait(false), fPrevious(0),
65  fNext(0)
66  {
67  // Store the current directory so we can restore it
68  // later and cd to the new directory.
69  if ( fDirectory ) (*fDirectory).RegisterContext(this);
70  }
71  TContext(TDirectory *newCurrent)
72  : fDirectory(TDirectory::CurrentDirectory()), fActiveDestructor(false), fDirectoryWait(false), fPrevious(0),
73  fNext(0)
74  {
75  // Store the current directory so we can restore it
76  // later and cd to the new directory.
77  if ( fDirectory ) (*fDirectory).RegisterContext(this);
78  if ( newCurrent ) newCurrent->cd();
79  else CdNull();
80  }
81  ~TContext();
82  };
83 
84 protected:
85 
86  TObject *fMother; //pointer to mother of the directory
87  TList *fList; //List of objects in memory
88  TUUID fUUID; //Unique identifier
89  TString fPathBuffer; //!Buffer for GetPath() function
90  TContext *fContext; //!Pointer to a list of TContext object pointing to this TDirectory
91 
92  std::atomic<size_t> fContextPeg; //!Counter delaying the TDirectory destructor from finishing.
93  mutable std::atomic_flag fSpinLock; //! MSVC doesn't support = ATOMIC_FLAG_INIT;
94 
95  static Bool_t fgAddDirectory; //!flag to add histograms, graphs,etc to the directory
96 
97  Bool_t cd1(const char *path);
98  static Bool_t Cd1(const char *path);
99 
100  virtual void CleanTargets();
101  void FillFullPath(TString& buf) const;
102  void RegisterContext(TContext *ctxt);
103  void UnregisterContext(TContext *ctxt);
104  friend class TContext;
105 
106 protected:
107  TDirectory(const TDirectory &directory); //Directories cannot be copied
108  void operator=(const TDirectory &); //Directorise cannot be copied
109 
110 public:
111 
112  TDirectory();
113  TDirectory(const char *name, const char *title, Option_t *option="", TDirectory* motherDir = 0);
114  virtual ~TDirectory();
115  static void AddDirectory(Bool_t add=kTRUE);
116  static Bool_t AddDirectoryStatus();
117  virtual void Append(TObject *obj, Bool_t replace = kFALSE);
118  virtual void Add(TObject *obj, Bool_t replace = kFALSE) { Append(obj,replace); }
119  virtual Int_t AppendKey(TKey *) {return 0;}
120  virtual void Browse(TBrowser *b);
121  virtual void Build(TFile* motherFile = 0, TDirectory* motherDir = 0);
122  virtual void Clear(Option_t *option="");
123  virtual TObject *CloneObject(const TObject *obj, Bool_t autoadd = kTRUE);
124  virtual void Close(Option_t *option="");
125  static TDirectory *&CurrentDirectory(); // Return the current directory for this thread.
126  virtual void Copy(TObject &) const { MayNotUse("Copy(TObject &)"); }
127  virtual Bool_t cd(const char *path = 0);
128  virtual void DeleteAll(Option_t *option="");
129  virtual void Delete(const char *namecycle="");
130  virtual void Draw(Option_t *option="");
131  virtual TKey *FindKey(const char * /*keyname*/) const {return 0;}
132  virtual TKey *FindKeyAny(const char * /*keyname*/) const {return 0;}
133  virtual TObject *FindObject(const char *name) const;
134  virtual TObject *FindObject(const TObject *obj) const;
135  virtual TObject *FindObjectAny(const char *name) const;
136  virtual TObject *FindObjectAnyFile(const char * /*name*/) const {return 0;}
137  virtual TObject *Get(const char *namecycle);
138  virtual TDirectory *GetDirectory(const char *namecycle, Bool_t printError = false, const char *funcname = "GetDirectory");
139  template <class T> inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for information
140  {
141  ptr = (T*)GetObjectChecked(namecycle,TBuffer::GetClass(typeid(T)));
142  }
143  virtual void *GetObjectChecked(const char *namecycle, const char* classname);
144  virtual void *GetObjectChecked(const char *namecycle, const TClass* cl);
145  virtual void *GetObjectUnchecked(const char *namecycle);
146  virtual Int_t GetBufferSize() const {return 0;}
147  virtual TFile *GetFile() const { return 0; }
148  virtual TKey *GetKey(const char * /*name */, Short_t /* cycle */=9999) const {return 0;}
149  virtual TList *GetList() const { return fList; }
150  virtual TList *GetListOfKeys() const { return 0; }
151  virtual TObject *GetMother() const { return fMother; }
152  virtual TDirectory *GetMotherDir() const { return fMother==0 ? 0 : dynamic_cast<TDirectory*>(fMother); }
153  virtual Int_t GetNbytesKeys() const { return 0; }
154  virtual Int_t GetNkeys() const { return 0; }
155  virtual Long64_t GetSeekDir() const { return 0; }
156  virtual Long64_t GetSeekParent() const { return 0; }
157  virtual Long64_t GetSeekKeys() const { return 0; }
158  virtual const char *GetPathStatic() const;
159  virtual const char *GetPath() const;
160  TUUID GetUUID() const {return fUUID;}
161  virtual Bool_t IsFolder() const { return kTRUE; }
162  virtual Bool_t IsModified() const { return kFALSE; }
163  virtual Bool_t IsWritable() const { return kFALSE; }
164  virtual void ls(Option_t *option="") const;
165  virtual TDirectory *mkdir(const char *name, const char *title="");
166  virtual TFile *OpenFile(const char * /*name*/, Option_t * /*option*/ = "",
167  const char * /*ftitle*/ = "", Int_t /*compress*/ = 1,
168  Int_t /*netopt*/ = 0) {return 0;}
169  virtual void Paint(Option_t *option="");
170  virtual void Print(Option_t *option="") const;
171  virtual void Purge(Short_t /*nkeep*/=1) {}
172  virtual void pwd() const;
173  virtual void ReadAll(Option_t * /*option*/="") {}
174  virtual Int_t ReadKeys(Bool_t /*forceRead*/=kTRUE) {return 0;}
175  virtual Int_t ReadTObject(TObject * /*obj*/, const char * /*keyname*/) {return 0;}
176  virtual TObject *Remove(TObject*);
177  virtual void RecursiveRemove(TObject *obj);
178  virtual void rmdir(const char *name);
179  virtual void Save() {}
180  virtual Int_t SaveObjectAs(const TObject * /*obj*/, const char * /*filename*/="", Option_t * /*option*/="") const;
181  virtual void SaveSelf(Bool_t /*force*/ = kFALSE) {}
182  virtual void SetBufferSize(Int_t /* bufsize */) {}
183  virtual void SetModified() {}
184  virtual void SetMother(TObject *mother) {fMother = (TObject*)mother;}
185  virtual void SetName(const char* newname);
186  virtual void SetTRefAction(TObject * /*ref*/, TObject * /*parent*/) {}
187  virtual void SetSeekDir(Long64_t) {}
188  virtual void SetWritable(Bool_t) {}
189  virtual Int_t Sizeof() const {return 0;}
190  virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0){return 0;}
191  virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0) const {return 0;}
192  virtual Int_t WriteTObject(const TObject *obj, const char *name =0, Option_t * /*option*/="", Int_t /*bufsize*/ =0);
193 private:
194  Int_t WriteObject(void *obj, const char* name, Option_t *option="", Int_t bufsize=0); // Intentionaly not implemented.
195 public:
196  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
197  {
198  return WriteObjectAny(obj,TBuffer::GetClass(typeid(T)),name,option,bufsize);
199  }
200  virtual Int_t WriteObjectAny(const void *, const char * /*classname*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
201  virtual Int_t WriteObjectAny(const void *, const TClass * /*cl*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
202  virtual void WriteDirHeader() {}
203  virtual void WriteKeys() {}
204 
205  static Bool_t Cd(const char *path);
206  static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize = 0);
207  static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle);
208 
209  ClassDef(TDirectory,5) //Describe directory structure in memory
210 };
211 
212 #ifndef __CINT__
213 #define gDirectory (TDirectory::CurrentDirectory())
214 
215 #elif defined(__MAKECINT__)
216 // To properly handle the use of gDirectory in header files (in static declarations)
218 #endif
219 
220 #endif
virtual void Draw(Option_t *option="")
Fill Graphics Structure and Paint.
Definition: TDirectory.cxx:707
virtual void Copy(TObject &) const
Copy this to obj.
Definition: TDirectory.h:126
virtual ~TDirectory()
Destructor.
Definition: TDirectory.cxx:101
std::atomic_flag fSpinLock
Counter delaying the TDirectory destructor from finishing.
Definition: TDirectory.h:93
virtual void ReadAll(Option_t *="")
Definition: TDirectory.h:173
virtual Int_t WriteObjectAny(const void *, const TClass *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:201
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
virtual void SetSeekDir(Long64_t)
Definition: TDirectory.h:187
long long Long64_t
Definition: RtypesCore.h:69
virtual void Build(TFile *motherFile=0, TDirectory *motherDir=0)
Initialise directory to defaults.
Definition: TDirectory.cxx:237
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:805
const char Option_t
Definition: RtypesCore.h:62
virtual TDirectory * GetMotherDir() const
Definition: TDirectory.h:152
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:139
virtual void Print(Option_t *option="") const
Print all objects in the directory.
double T(double x)
Definition: ChebyshevPol.h:34
TList * fList
Definition: TDirectory.h:87
virtual TObject * CloneObject(const TObject *obj, Bool_t autoadd=kTRUE)
Clone an object.
Definition: TDirectory.cxx:328
virtual const char * GetPathStatic() const
Returns the full path of the directory.
Definition: TDirectory.cxx:949
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:47
virtual TObject * FindObject(const char *name) const
Find object by name in the list of memory objects.
Definition: TDirectory.cxx:723
void FillFullPath(TString &buf) const
Recursive method to fill full path for directory.
virtual Int_t WriteTObject(const TObject *obj, const char *name=0, Option_t *="", Int_t=0)
See TDirectoryFile::WriteTObject for details.
virtual void SetBufferSize(Int_t)
Definition: TDirectory.h:182
virtual Int_t ReadKeys(Bool_t=kTRUE)
Definition: TDirectory.h:174
Basic string class.
Definition: TString.h:131
std::atomic< bool > fDirectoryWait
Set to true during the destructor execution.
Definition: TDirectory.h:45
virtual Int_t GetNbytesKeys() const
Definition: TDirectory.h:153
int Int_t
Definition: RtypesCore.h:41
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
bool Bool_t
Definition: RtypesCore.h:59
virtual void Purge(Short_t=1)
Definition: TDirectory.h:171
TContext * fPrevious
Set to true if a TDirectory might still access this object.
Definition: TDirectory.h:47
virtual TFile * OpenFile(const char *, Option_t *="", const char *="", Int_t=1, Int_t=0)
Definition: TDirectory.h:166
virtual Long64_t GetSeekParent() const
Definition: TDirectory.h:156
virtual Int_t SaveObjectAs(const TObject *, const char *="", Option_t *="") const
Save object in filename, if filename is 0 or "", a file with "objectname.root" is created...
friend class TDirectory
Definition: TDirectory.h:52
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
See documentation of TDirectory::GetObjectCheck(const char *namecycle, const TClass *cl) ...
Definition: TDirectory.cxx:868
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition: TUUID.h:42
TString fPathBuffer
Definition: TDirectory.h:89
std::atomic< TDirectory * > fDirectory
Definition: TDirectory.h:43
#define ClassDef(name, id)
Definition: Rtypes.h:320
virtual Bool_t IsWritable() const
Definition: TDirectory.h:163
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual Int_t GetBufferSize() const
Definition: TDirectory.h:146
virtual Int_t Write(const char *=0, Int_t=0, Int_t=0)
Write this object to the current directory.
Definition: TDirectory.h:190
virtual void Save()
Definition: TDirectory.h:179
virtual void Paint(Option_t *option="")
Paint all objects in the directory.
Bool_t cd1(const char *path)
flag to add histograms, graphs,etc to the directory
Definition: TDirectory.cxx:515
std::atomic< bool > fActiveDestructor
Pointer to the previous current directory.
Definition: TDirectory.h:44
static TClass * GetClass(const std::type_info &typeinfo)
Forward to TROOT::GetClass().
Definition: TBuffer.cxx:307
virtual void Close(Option_t *option="")
Delete all objects from memory and directory structure itself.
Definition: TDirectory.cxx:584
virtual void rmdir(const char *name)
Removes subdirectory from the directory When directory is deleted, all keys in all subdirectories wil...
virtual Int_t WriteObjectAny(const void *, const char *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:200
virtual void SetWritable(Bool_t)
Definition: TDirectory.h:188
void MayNotUse(const char *method) const
Use this method to signal that a method (defined in a base class) may not be called in a derived clas...
Definition: TObject.cxx:933
virtual TList * GetList() const
Definition: TDirectory.h:149
virtual TKey * FindKey(const char *) const
Definition: TDirectory.h:131
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
virtual Int_t Sizeof() const
Return size of the TNamed part of the TObject.
Definition: TDirectory.h:189
virtual TFile * GetFile() const
Definition: TDirectory.h:147
virtual void pwd() const
Print the path of the directory.
virtual Long64_t GetSeekKeys() const
Definition: TDirectory.h:157
virtual void Delete(const char *namecycle="")
Delete Objects or/and keys in a directory.
Definition: TDirectory.cxx:651
virtual TObject * FindObjectAnyFile(const char *) const
Definition: TDirectory.h:136
A doubly linked list.
Definition: TList.h:44
virtual void WriteDirHeader()
Definition: TDirectory.h:202
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add objects like histograms, TGraph2D, etc in memory...
Definition: TDirectory.cxx:171
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TContext * fNext
Pointer to the next TContext in the implied list of context pointing to fPrevious.
Definition: TDirectory.h:48
virtual void Browse(TBrowser *b)
Browse the content of the directory.
Definition: TDirectory.cxx:215
Int_t WriteObject(const T *obj, const char *name, Option_t *option="", Int_t bufsize=0)
Definition: TDirectory.h:196
TContext * fContext
Buffer for GetPath() function.
Definition: TDirectory.h:90
virtual Int_t GetNkeys() const
Definition: TDirectory.h:154
virtual Int_t ReadTObject(TObject *, const char *)
Definition: TDirectory.h:175
TUUID fUUID
Definition: TDirectory.h:88
virtual void * GetObjectUnchecked(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:860
static Bool_t Cd1(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:554
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:190
virtual Long64_t GetSeekDir() const
Definition: TDirectory.h:155
TContext & operator=(TContext &)
short Short_t
Definition: RtypesCore.h:35
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
virtual const char * GetPath() const
Returns the full path of the directory.
Definition: TDirectory.cxx:987
virtual void WriteKeys()
Definition: TDirectory.h:203
virtual void Clear(Option_t *option="")
Delete all objects from a Directory list.
Definition: TDirectory.cxx:572
TObject * fMother
Definition: TDirectory.h:86
virtual TKey * FindKeyAny(const char *) const
Definition: TDirectory.h:132
const Bool_t kFALSE
Definition: RtypesCore.h:88
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:181
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:119
virtual TObject * GetMother() const
Definition: TDirectory.h:151
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects)...
Definition: TDirectory.h:161
virtual void SetName(const char *newname)
Set the name for directory If the directory name is changed after the directory was written once...
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
static Bool_t Cd(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:541
std::atomic< size_t > fContextPeg
Pointer to a list of TContext object pointing to this TDirectory.
Definition: TDirectory.h:92
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:148
Int_t WriteObject(void *obj, const char *name, Option_t *option="", Int_t bufsize=0)
static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle)
Encode the name and cycle into buffer like: "aap;2".
Describe directory structure in memory.
Definition: TDirectory.h:34
static TDirectory *& CurrentDirectory()
Return the current directory for the current thread.
Definition: TDirectory.cxx:380
static Bool_t AddDirectoryStatus()
Static function: see TDirectory::AddDirectory for more comments.
Definition: TDirectory.cxx:179
static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize=0)
Decode a namecycle "aap;2" into name "aap" and cycle "2".
Mother of all ROOT objects.
Definition: TObject.h:37
#define R__EXTERN
Definition: DllImport.h:27
TContext(TDirectory *previous, TDirectory *newCurrent)
Definition: TDirectory.h:54
virtual Int_t Write(const char *=0, Int_t=0, Int_t=0) const
Write this object to the current directory.
Definition: TDirectory.h:191
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:497
TContext(TDirectory *newCurrent)
Definition: TDirectory.h:71
virtual void DeleteAll(Option_t *option="")
Delete all objects from memory.
Definition: TDirectory.cxx:624
virtual void ls(Option_t *option="") const
List Directory contents.
TUUID GetUUID() const
Definition: TDirectory.h:160
virtual Bool_t IsModified() const
Definition: TDirectory.h:162
virtual TDirectory * GetDirectory(const char *namecycle, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory using apath.
Definition: TDirectory.cxx:400
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define gDirectory
Definition: TDirectory.h:213
~TContext()
Destructor.
Definition: TDirectory.cxx:134
void RegisterContext(TContext *ctxt)
Register a TContext pointing to this TDirectory object.
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:184
virtual void RecursiveRemove(TObject *obj)
Recursively remove object from a Directory.
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void Add(TObject *obj, Bool_t replace=kFALSE)
Definition: TDirectory.h:118
char name[80]
Definition: TGX11.cxx:109
virtual void CleanTargets()
Clean the pointers to this object (gDirectory, TContext, etc.).
Definition: TDirectory.cxx:252
virtual void SetModified()
Definition: TDirectory.h:183
void UnregisterContext(TContext *ctxt)
UnRegister a TContext pointing to this TDirectory object.
static Bool_t fgAddDirectory
MSVC doesn&#39;t support = ATOMIC_FLAG_INIT;.
Definition: TDirectory.h:95
void CdNull()
Set the current directory to null.
virtual void SetTRefAction(TObject *, TObject *)
Definition: TDirectory.h:186
virtual TObject * FindObjectAny(const char *name) const
Find object by name in the list of memory objects of the current directory or its sub-directories...
Definition: TDirectory.cxx:735