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