Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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  }
66  TContext() : fDirectory(TDirectory::CurrentDirectory())
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  }
73  TContext(TDirectory *newCurrent) : fDirectory(TDirectory::CurrentDirectory())
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)
227 R__EXTERN TDirectory *gDirectory;
228 #endif
229 
230 #endif