All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
util::ROOT::TDirectoryChanger Class Reference

A class restoring the previous TDirectory on destruction. More...

#include <ROOTutils.h>

Public Member Functions

 TDirectoryChanger ()
 
 TDirectoryChanger (TDirectory *dir)
 
 TDirectoryChanger (std::string const &dir, std::string const &title="")
 
 ~TDirectoryChanger ()
 
void save ()
 Stores the current directory as the one to be saved. More...
 
void restore () const
 
void forget ()
 Do not restore the old directory on destruction. More...
 
void cd () const
 Make the stored new directory as current again. More...
 
void cd (TDirectory *dir)
 Make the specified directory as current. More...
 
void cd (std::string const &name, std::string const &title="")
 Make the specified directory as current, possibly creating it. More...
 
TDirectory * saved () const
 Returns a pointer to the directory that will be restored on destruction. More...
 
bool hasSaved () const
 Returns whether there is a directory to be restored on destruction. More...
 

Static Public Member Functions

static TDirectory * currentDir ()
 

Private Attributes

TDirectory * pSaved = nullptr
 
TDirectory * pNew = nullptr
 

Detailed Description

A class restoring the previous TDirectory on destruction.

When an instance of this object is created, the existing current directory is saved, and it is then restored on destruction.

Additional methods allow finer control on the restoration feature.

Example:

{
TDirectoryChanger DirGuard(gDirectory->GetDirectory("subdir"));
// everything here happens in the (existing) `subdir` directory.
}
// whatever follows happens under the previous directory

Definition at line 54 of file ROOTutils.h.

Constructor & Destructor Documentation

util::ROOT::TDirectoryChanger::TDirectoryChanger ( )
inline

Definition at line 61 of file ROOTutils.h.

61 { save(); }
void save()
Stores the current directory as the one to be saved.
Definition: ROOTutils.h:69
util::ROOT::TDirectoryChanger::TDirectoryChanger ( TDirectory *  dir)
inline

Definition at line 62 of file ROOTutils.h.

62 { save(); cd(dir); }
void cd() const
Make the stored new directory as current again.
Definition: ROOTutils.h:79
tuple dir
Definition: dropbox.py:28
void save()
Stores the current directory as the one to be saved.
Definition: ROOTutils.h:69
util::ROOT::TDirectoryChanger::TDirectoryChanger ( std::string const &  dir,
std::string const &  title = "" 
)
inline

Definition at line 63 of file ROOTutils.h.

64  { save(); cd(dir, title); }
void cd() const
Make the stored new directory as current again.
Definition: ROOTutils.h:79
tuple dir
Definition: dropbox.py:28
void save()
Stores the current directory as the one to be saved.
Definition: ROOTutils.h:69
util::ROOT::TDirectoryChanger::~TDirectoryChanger ( )
inline

Definition at line 66 of file ROOTutils.h.

66 { restore(); }

Member Function Documentation

void util::ROOT::TDirectoryChanger::cd ( ) const
inline

Make the stored new directory as current again.

Definition at line 79 of file ROOTutils.h.

79 { if (pNew) pNew->cd(); }
void util::ROOT::TDirectoryChanger::cd ( TDirectory *  dir)
inline

Make the specified directory as current.

Definition at line 82 of file ROOTutils.h.

82 { pNew = dir; cd(); }
void cd() const
Make the stored new directory as current again.
Definition: ROOTutils.h:79
tuple dir
Definition: dropbox.py:28
void util::ROOT::TDirectoryChanger::cd ( std::string const &  name,
std::string const &  title = "" 
)
inline

Make the specified directory as current, possibly creating it.

Definition at line 85 of file ROOTutils.h.

86  {
87  assert(currentDir());
88  pNew = currentDir()->GetDirectory(name.c_str());
89  if (!pNew) pNew = currentDir()->mkdir(name.c_str(), title.c_str());
90  cd();
91  }
void cd() const
Make the stored new directory as current again.
Definition: ROOTutils.h:79
static TDirectory * currentDir()
Definition: ROOTutils.h:99
then echo fcl name
static TDirectory* util::ROOT::TDirectoryChanger::currentDir ( )
inlinestatic

Definition at line 99 of file ROOTutils.h.

99 { return gDirectory; }
void util::ROOT::TDirectoryChanger::forget ( )
inline

Do not restore the old directory on destruction.

Definition at line 76 of file ROOTutils.h.

76 { pSaved = nullptr; }
bool util::ROOT::TDirectoryChanger::hasSaved ( ) const
inline

Returns whether there is a directory to be restored on destruction.

Definition at line 97 of file ROOTutils.h.

97 { return saved() != nullptr; }
TDirectory * saved() const
Returns a pointer to the directory that will be restored on destruction.
Definition: ROOTutils.h:94
void util::ROOT::TDirectoryChanger::restore ( ) const
inline

Immediately restores the old directory. It will still restored on destruction too.

Definition at line 73 of file ROOTutils.h.

73 { if (pSaved) pSaved->cd(); }
void util::ROOT::TDirectoryChanger::save ( )
inline

Stores the current directory as the one to be saved.

Definition at line 69 of file ROOTutils.h.

69 { pSaved = currentDir(); }
static TDirectory * currentDir()
Definition: ROOTutils.h:99
TDirectory* util::ROOT::TDirectoryChanger::saved ( ) const
inline

Returns a pointer to the directory that will be restored on destruction.

Definition at line 94 of file ROOTutils.h.

94 { return pSaved; }

Member Data Documentation

TDirectory* util::ROOT::TDirectoryChanger::pNew = nullptr
private

Definition at line 57 of file ROOTutils.h.

TDirectory* util::ROOT::TDirectoryChanger::pSaved = nullptr
private

Definition at line 56 of file ROOTutils.h.


The documentation for this class was generated from the following file: