Logo ROOT   6.18/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
30class TBrowser;
31class TKey;
32class TFile;
33
34class TDirectory : public TNamed {
35public:
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 }
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)
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
84protected:
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
106protected:
107 TDirectory(const TDirectory &directory); //Directories cannot be copied
108 void operator=(const TDirectory &); //Directorise cannot be copied
109
110public:
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 /// See documentation of TDirectoryFile::Get(const char *namecycle)
139 template <class T> inline T* Get(const char* namecycle)
140 {
141 return static_cast<T*>(GetObjectChecked(namecycle, TClass::GetClass<T>()));
142 }
143 virtual TDirectory *GetDirectory(const char *namecycle, Bool_t printError = false, const char *funcname = "GetDirectory");
144 template <class T> inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for information
145 {
146 ptr = (T *)GetObjectChecked(namecycle, TClass::GetClass<T>());
147 }
148 virtual void *GetObjectChecked(const char *namecycle, const char* classname);
149 virtual void *GetObjectChecked(const char *namecycle, const TClass* cl);
150 virtual void *GetObjectUnchecked(const char *namecycle);
151 virtual Int_t GetBufferSize() const {return 0;}
152 virtual TFile *GetFile() const { return 0; }
153 virtual TKey *GetKey(const char * /*name */, Short_t /* cycle */=9999) const {return 0;}
154 virtual TList *GetList() const { return fList; }
155 virtual TList *GetListOfKeys() const { return 0; }
156 virtual TObject *GetMother() const { return fMother; }
157 virtual TDirectory *GetMotherDir() const { return fMother==0 ? 0 : dynamic_cast<TDirectory*>(fMother); }
158 virtual Int_t GetNbytesKeys() const { return 0; }
159 virtual Int_t GetNkeys() const { return 0; }
160 virtual Long64_t GetSeekDir() const { return 0; }
161 virtual Long64_t GetSeekParent() const { return 0; }
162 virtual Long64_t GetSeekKeys() const { return 0; }
163 virtual const char *GetPathStatic() const;
164 virtual const char *GetPath() const;
165 TUUID GetUUID() const {return fUUID;}
166 virtual Bool_t IsFolder() const { return kTRUE; }
167 virtual Bool_t IsModified() const { return kFALSE; }
168 virtual Bool_t IsWritable() const { return kFALSE; }
169 virtual void ls(Option_t *option="") const;
170 virtual TDirectory *mkdir(const char *name, const char *title="");
171 virtual TFile *OpenFile(const char * /*name*/, Option_t * /*option*/ = "",
172 const char * /*ftitle*/ = "", Int_t /*compress*/ = 1,
173 Int_t /*netopt*/ = 0) {return 0;}
174 virtual void Paint(Option_t *option="");
175 virtual void Print(Option_t *option="") const;
176 virtual void Purge(Short_t /*nkeep*/=1) {}
177 virtual void pwd() const;
178 virtual void ReadAll(Option_t * /*option*/="") {}
179 virtual Int_t ReadKeys(Bool_t /*forceRead*/=kTRUE) {return 0;}
180 virtual Int_t ReadTObject(TObject * /*obj*/, const char * /*keyname*/) {return 0;}
181 virtual TObject *Remove(TObject*);
182 virtual void RecursiveRemove(TObject *obj);
183 virtual void rmdir(const char *name);
184 virtual void Save() {}
185 virtual Int_t SaveObjectAs(const TObject * /*obj*/, const char * /*filename*/="", Option_t * /*option*/="") const;
186 virtual void SaveSelf(Bool_t /*force*/ = kFALSE) {}
187 virtual void SetBufferSize(Int_t /* bufsize */) {}
188 virtual void SetModified() {}
189 virtual void SetMother(TObject *mother) {fMother = (TObject*)mother;}
190 virtual void SetName(const char* newname);
191 virtual void SetTRefAction(TObject * /*ref*/, TObject * /*parent*/) {}
192 virtual void SetSeekDir(Long64_t) {}
193 virtual void SetWritable(Bool_t) {}
194 virtual Int_t Sizeof() const {return 0;}
195 virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0){return 0;}
196 virtual Int_t Write(const char * /*name*/=0, Int_t /*opt*/=0, Int_t /*bufsize*/=0) const {return 0;}
197 virtual Int_t WriteTObject(const TObject *obj, const char *name =0, Option_t * /*option*/="", Int_t /*bufsize*/ =0);
198private:
199 Int_t WriteObject(void *obj, const char* name, Option_t *option="", Int_t bufsize=0); // Intentionaly not implemented.
200public:
201 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
202 {
203 return WriteObjectAny(obj, TClass::GetClass<T>(), name, option, bufsize);
204 }
205 virtual Int_t WriteObjectAny(const void *, const char * /*classname*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
206 virtual Int_t WriteObjectAny(const void *, const TClass * /*cl*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
207 virtual void WriteDirHeader() {}
208 virtual void WriteKeys() {}
209
210 static Bool_t Cd(const char *path);
211 static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize = 0);
212 static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle);
213
214 ClassDef(TDirectory,5) //Describe directory structure in memory
215};
216
217#ifndef __CINT__
218#define gDirectory (TDirectory::CurrentDirectory())
219
220#elif defined(__MAKECINT__)
221// To properly handle the use of gDirectory in header files (in static declarations)
223#endif
224
225#endif
#define R__EXTERN
Definition: DllImport.h:27
#define b(i)
Definition: RSha256.hxx:100
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
short Short_t
Definition: RtypesCore.h:35
long long Long64_t
Definition: RtypesCore.h:69
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:326
#define gDirectory
Definition: TDirectory.h:218
char name[80]
Definition: TGX11.cxx:109
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:75
TContext(TDirectory *previous, TDirectory *newCurrent)
Definition: TDirectory.h:54
std::atomic< bool > fDirectoryWait
Set to true during the destructor execution.
Definition: TDirectory.h:45
TContext & operator=(TContext &)
void CdNull()
Set the current directory to null.
TContext(TDirectory *newCurrent)
Definition: TDirectory.h:71
~TContext()
Destructor.
Definition: TDirectory.cxx:134
TContext * fPrevious
Set to true if a TDirectory might still access this object.
Definition: TDirectory.h:47
std::atomic< bool > fActiveDestructor
Pointer to the previous current directory.
Definition: TDirectory.h:44
TContext(TContext &)
Pointer to the next TContext in the implied list of context pointing to fPrevious.
TContext * fNext
Pointer to the next TContext in the implied list of context pointing to fPrevious.
Definition: TDirectory.h:48
std::atomic< TDirectory * > fDirectory
Definition: TDirectory.h:43
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual Long64_t GetSeekDir() const
Definition: TDirectory.h:160
Bool_t cd1(const char *path)
flag to add histograms, graphs,etc to the directory
Definition: TDirectory.cxx:515
virtual void Close(Option_t *option="")
Delete all objects from memory and directory structure itself.
Definition: TDirectory.cxx:584
virtual void Save()
Definition: TDirectory.h:184
virtual TList * GetList() const
Definition: TDirectory.h:154
virtual void SetSeekDir(Long64_t)
Definition: TDirectory.h:192
virtual void ls(Option_t *option="") const
List Directory contents.
virtual void RecursiveRemove(TObject *obj)
Recursively remove object from a Directory.
virtual Int_t Write(const char *=0, Int_t=0, Int_t=0) const
Write this object to the current directory.
Definition: TDirectory.h:196
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:119
virtual TDirectory * GetDirectory(const char *namecycle, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory using apath.
Definition: TDirectory.cxx:400
virtual Int_t Sizeof() const
Return size of the TNamed part of the TObject.
Definition: TDirectory.h:194
virtual TObject * GetMother() const
Definition: TDirectory.h:156
virtual Int_t GetNkeys() const
Definition: TDirectory.h:159
virtual void WriteKeys()
Definition: TDirectory.h:208
virtual Int_t GetNbytesKeys() const
Definition: TDirectory.h:158
virtual const char * GetPath() const
Returns the full path of the directory.
Definition: TDirectory.cxx:987
std::atomic_flag fSpinLock
Counter delaying the TDirectory destructor from finishing.
Definition: TDirectory.h:93
virtual void ReadAll(Option_t *="")
Definition: TDirectory.h:178
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:805
virtual void * GetObjectUnchecked(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:860
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 void DeleteAll(Option_t *option="")
Delete all objects from memory.
Definition: TDirectory.cxx:624
std::atomic< size_t > fContextPeg
Pointer to a list of TContext object pointing to this TDirectory.
Definition: TDirectory.h:92
virtual void SetModified()
Definition: TDirectory.h:188
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:497
static TDirectory *& CurrentDirectory()
Return the current directory for the current thread.
Definition: TDirectory.cxx:380
virtual void rmdir(const char *name)
Removes subdirectory from the directory When directory is deleted, all keys in all subdirectories wil...
static Bool_t AddDirectoryStatus()
Static function: see TDirectory::AddDirectory for more comments.
Definition: TDirectory.cxx:179
void FillFullPath(TString &buf) const
Recursive method to fill full path for directory.
virtual TObject * FindObject(const char *name) const
Find object by name in the list of memory objects.
Definition: TDirectory.cxx:723
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
virtual void CleanTargets()
Clean the pointers to this object (gDirectory, TContext, etc.).
Definition: TDirectory.cxx:252
virtual void Paint(Option_t *option="")
Paint all objects in the directory.
virtual Int_t WriteObjectAny(const void *, const TClass *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:206
TContext * fContext
Buffer for GetPath() function.
Definition: TDirectory.h:90
virtual TFile * OpenFile(const char *, Option_t *="", const char *="", Int_t=1, Int_t=0)
Definition: TDirectory.h:171
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:190
Int_t WriteObject(const T *obj, const char *name, Option_t *option="", Int_t bufsize=0)
Definition: TDirectory.h:201
virtual void WriteDirHeader()
Definition: TDirectory.h:207
TDirectory()
Directory default constructor.
Definition: TDirectory.cxx:46
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".
static Bool_t Cd(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:541
virtual void Add(TObject *obj, Bool_t replace=kFALSE)
Definition: TDirectory.h:118
virtual Int_t WriteObjectAny(const void *, const char *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:205
virtual TFile * GetFile() const
Definition: TDirectory.h:152
virtual void Print(Option_t *option="") const
Print all objects in the directory.
Int_t WriteObject(void *obj, const char *name, Option_t *option="", Int_t bufsize=0)
virtual ~TDirectory()
Destructor.
Definition: TDirectory.cxx:101
virtual Int_t ReadKeys(Bool_t=kTRUE)
Definition: TDirectory.h:179
virtual Int_t WriteTObject(const TObject *obj, const char *name=0, Option_t *="", Int_t=0)
See TDirectoryFile::WriteTObject for details.
static Bool_t Cd1(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:554
virtual Bool_t IsWritable() const
Definition: TDirectory.h:168
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:153
virtual void SetName(const char *newname)
Set the name for directory If the directory name is changed after the directory was written once,...
virtual void Purge(Short_t=1)
Definition: TDirectory.h:176
void UnregisterContext(TContext *ctxt)
UnRegister a TContext pointing to this TDirectory object.
virtual Int_t ReadTObject(TObject *, const char *)
Definition: TDirectory.h:180
virtual void Browse(TBrowser *b)
Browse the content of the directory.
Definition: TDirectory.cxx:215
virtual void SetWritable(Bool_t)
Definition: TDirectory.h:193
virtual Bool_t IsModified() const
Definition: TDirectory.h:167
virtual void SetTRefAction(TObject *, TObject *)
Definition: TDirectory.h:191
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:186
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.
TString fPathBuffer
Definition: TDirectory.h:89
virtual TKey * FindKey(const char *) const
Definition: TDirectory.h:131
virtual Int_t GetBufferSize() const
Definition: TDirectory.h:151
virtual Long64_t GetSeekParent() const
Definition: TDirectory.h:161
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:189
virtual Long64_t GetSeekKeys() const
Definition: TDirectory.h:162
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:155
TUUID fUUID
Definition: TDirectory.h:88
virtual void Clear(Option_t *option="")
Delete all objects from a Directory list.
Definition: TDirectory.cxx:572
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition: TDirectory.h:166
TUUID GetUUID() const
Definition: TDirectory.h:165
virtual TObject * FindObjectAnyFile(const char *) const
Definition: TDirectory.h:136
static Bool_t fgAddDirectory
MSVC doesn't support = ATOMIC_FLAG_INIT;.
Definition: TDirectory.h:95
virtual void Build(TFile *motherFile=0, TDirectory *motherDir=0)
Initialise directory to defaults.
Definition: TDirectory.cxx:237
static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle)
Encode the name and cycle into buffer like: "aap;2".
virtual const char * GetPathStatic() const
Returns the full path of the directory.
Definition: TDirectory.cxx:949
virtual TDirectory * GetMotherDir() const
Definition: TDirectory.h:157
TObject * fMother
Definition: TDirectory.h:86
virtual Int_t Write(const char *=0, Int_t=0, Int_t=0)
Write this object to the current directory.
Definition: TDirectory.h:195
void operator=(const TDirectory &)
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:144
virtual void pwd() const
Print the path of the directory.
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
See documentation of TDirectory::GetObjectCheck(const char *namecycle, const TClass *cl)
Definition: TDirectory.cxx:868
virtual TObject * CloneObject(const TObject *obj, Bool_t autoadd=kTRUE)
Clone an object.
Definition: TDirectory.cxx:328
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
virtual TDirectory * mkdir(const char *name, const char *title="")
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
void RegisterContext(TContext *ctxt)
Register a TContext pointing to this TDirectory object.
TList * fList
Definition: TDirectory.h:87
virtual TKey * FindKeyAny(const char *) const
Definition: TDirectory.h:132
virtual void Delete(const char *namecycle="")
Delete Objects or/and keys in a directory.
Definition: TDirectory.cxx:651
T * Get(const char *namecycle)
See documentation of TDirectoryFile::Get(const char *namecycle)
Definition: TDirectory.h:139
virtual void SetBufferSize(Int_t)
Definition: TDirectory.h:187
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
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
A doubly linked list.
Definition: TList.h:44
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Mother of all ROOT objects.
Definition: TObject.h:37
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
Basic string class.
Definition: TString.h:131
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition: TUUID.h:42
double T(double x)
Definition: ChebyshevPol.h:34