FlatDirectory.cpp
Go to the documentation of this file.
1 //File: FlatDirectory.h
2 //Brief: FlatDirectory wraps over any class that implements a contract like
3 // art::TFileDirectory to expose the same contract without creating
4 // any sub-TDirectoires. Instead, FlatDirectory::make<>() appends its
5 // name to objects' names to give some strong assurances of name
6 // uniqueness. FlatDirectory might be useful in adapting code that
7 // normally relies on TFileDirectory-like objects to work with
8 // post-processing software like Monet that can't deal with nested
9 // TDirectories.
10 //Author: Andrew Olivier aolivier@ur.rochester.edu
11 
12 //c++ includes
13 #include <memory> //For std::shared_ptr
14 #include <string> //For std::string
15 
16 #ifndef CRT_FLATDIRECTORY_CPP
17 #define CRT_FLATDIRECTORY_CPP
18 namespace CRT //Using namespace CRT just to avoid name pollution for now.
19  //Feel free to move to a more appropriate/descriptive namespace
20  //if in wider use. FlatDirectory really has nothing to do
21  //with the ProtoDUNE-SP CRT.
22 {
23  //A DIRPTR has pointer syntax that accesses an object that looks like:
24  //
25  //template <class HIST, class ARGS...> HIST* make(ARGS... args)
26  //
27  //Looks like art::ServiceHandle<art::TFileService>.
28  template <class DIRPTR>
29  class FlatDirectory //A FlatDirectory IS NOT A DIRECTORY because that would
30  //require that DIRECTORY is copiable and introduce
31  //coupling to DIRECTORY's source code in other ways.
32  {
33  public:
34  //Create a top-level FlatDirectory to wrap over some DIRECTORY
35  //object. A FlatDirectory created this way will not append
36  //anything to the names of its children, but its sub-directories
37  //will.
38  FlatDirectory(DIRPTR dir);
39  virtual ~FlatDirectory() = default;
40 
41  //Implementation of TFileDirectory-like contract.
42  template <class TOBJECT, class ...ARGS>
43  TOBJECT* make(ARGS... args)
44  {
45  //TODO: Figure out which parameter(s) will set obj's name
46  // without actually creating an object with that name.
47  // This would avoid some temporary name conflicts that
48  // would occur with a "real" TDirectory.
49  auto obj = fBaseDir->template make<TOBJECT>(args...);
50  const std::string oldName = obj->GetName();
51  obj->SetName((fName+oldName).c_str());
52  return obj;
53  }
54 
55  template <class TOBJECT, class ...ARGS>
56  TOBJECT* makeAndRegister(const std::string& name, const std::string& title, ARGS... args)
57  {
58  return fBaseDir->template makeAndRegister<TOBJECT>(fName+name, title, args...);
59  }
60 
62 
63  private:
64  DIRPTR fBaseDir; //Base directory in which this
65  //FlatDirectory and all of its
66  //children will put objects they
67  //make<>().
68 
69  const std::string fName; //The name of this FlatDirectory. Will be
70  //appended to the names of all child objects
71  //to create unique names.
72 
73  //Create a subdirectory of a given FlatDirectory. This behavior is
74  //exposed to the user through mkdir.
76 
77  static constexpr auto Separator = "_"; //The separator between nested FlatDirectories' names.
78  //I'll probably never change it, but I've written a
79  //parameter here so that any future changes can be
80  //maintained in one place.
81  //
82  //Worrying so much about the storage "policy" for
83  //what is probably just a C-string is serious overkill.
84  };
85 
86  //Define member functions out of class body for cleanliness
87  template <class DIRPTR>
89  {
90  }
91 
92  template <class DIRPTR>
94  {
95  FlatDirectory child(name, *this);
96  return child;
97  }
98 
99  template <class DIRPTR>
101  fName(parent.fName+name+Separator)
102  {
103  }
104 
105  //For pre-c++17, syntatic sugar to create a FlatDirectory from anything that implements the
106  //art::TFileDirectory contract.
107  //TODO: To be pedantic, maybe a deprecated attribute if compiler supports c++17?
108  template <class DIRPTR>
109  #if __cplusplus > 201402L
110  [[deprecated("In c++17, the compiler should infer template parameters for you in the FlatDirectory<> constructor.")]]
111  #endif
113  {
114  return FlatDirectory<DIRPTR>(ptr);
115  }
116 }
117 
118 #endif //CRT_FLATDIRECTORY_CPP
static QCString name
Definition: declinfo.cpp:673
FlatDirectory(DIRPTR dir)
const std::string fName
FlatDirectory mkdir(const std::string &name)
std::string string
Definition: nybbler.cc:12
TOBJECT * makeAndRegister(const std::string &name, const std::string &title, ARGS...args)
virtual ~FlatDirectory()=default
FlatDirectory< DIRPTR > make_FlatDirectory(DIRPTR &ptr)
string dir
static QCString args
Definition: declinfo.cpp:674
static constexpr auto Separator
TOBJECT * make(ARGS...args)
def parent(G, child, parent_type)
Definition: graph.py:67