ROOT  6.06/09
Reference Guide
TDirectory.h
Go to the documentation of this file.
1 /// \file TDirectory.h
2 /// \ingroup Base ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-07-31
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/CREDITS. *
13  *************************************************************************/
14 
15 #ifndef ROOT7_TDirectory
16 #define ROOT7_TDirectory
17 
18 #include "ROOT/TCoopPtr.h"
19 #include "ROOT/TLogger.h"
20 #include "ROOT/TKey.h"
21 
22 #include <unordered_map>
23 #include <experimental/string_view>
24 
25 namespace ROOT {
26 
27 /**
28  Key/value store of objects.
29 
30  Given a name, a `TDirectory` can store and retrieve an object. It will manage
31  ownership through a `TCoopPtr`: if you delete the object, the object will be
32  gone from the `TDirectory`. Once the `TDirectory `is destructed, the objects
33  it contains are destructed (unless other `TCoopPtr`s reference the same
34  object).
35 
36  Example:
37  TDirectory dirBackgrounds;
38  TDirectory dirQCD;
39  TDirectory dirHiggs;
40 
41  */
42 class TDirectory {
43  // Don't keep a unique_ptr as value in a map.
44  /// The values referenced by a TDirectory are type erased `TCoopPtr`s: they
45  /// can be of any type; the actual type is determined through the virtual
46  /// interface of Internal::TCoopPtrTypeErasedBase.
47  using Value_t = std::shared_ptr<Internal::TCoopPtrTypeErasedBase>;
48 
49  /// The directory content is a hashed map of `TKey` => `Value_t`.
50  /// TODO: really? Or just std::string => Value_t - that should be enough!
51  /// Rather add some info (time stamp etc) to the Value_t.
52  using ContentMap_t = std::unordered_map<TKey, Value_t>;
53 
54  /// The `TDirectory`'s content.
56 
57 public:
58 
59  /// Create an object of type `T` (passing some arguments to its constructor).
60  /// The `TDirectory` will register the object.
61  ///
62  /// \param name - Key of the object.
63  /// \param args - arguments to be passed to the constructor of `T`
64  template <class T, class... ARGS>
65  TCoopPtr<T> Create(const std::string& name, ARGS... args) {
66  TCoopPtr<T> ptr(new T(args...));
67  Add(name, ptr);
68  return ptr;
69  }
70 
71  // TODO: The key should probably be a simple std::string. Do we need the TKey
72  // at all? Even for TFile, the key should stay a std::string, and the value
73  // should have an additional meta payload (timestamp). Think of object store.
74  const TKey* FindKey(const std::string& name) const {
75  auto idx = fContent.find(name);
76  if (idx == fContent.end())
77  return nullptr;
78  return &idx->first;
79  }
80 
81  /// Add an existing object (rather a `TCoopPtr` to it) to the TDirectory. The
82  /// TDirectory will not delete the object but it will need to be notified once
83  /// the object is deleted.
84  template <class T>
85  const TKey& Add(const std::string& name, TCoopPtr<T> ptr) {
86  ContentMap_t::iterator idx = fContent.find(name);
87  if (idx != fContent.end()) {
89  << "Replacing object with name " << name;
90  idx->second = std::make_unique<Internal::TCoopPtrTypeErased<T>>(ptr);
91  // The cast is fine: we do not change the key's value (hash)
92  const_cast<TKey&>(idx->first).SetChanged();
93  return idx->first;
94  }
95  return fContent.insert({name,
96  std::make_shared<Internal::TCoopPtrTypeErased<T>>(ptr)})
97  .first->first;
98  }
99 
100  /// Dedicated, process-wide TDirectory.
101  ///
102  /// \note This is *not* thread-safe. You will need to syncronize yourself. In
103  /// general it's a bad idea to use a global collection in a multi-threaded
104  /// environment; ROOT itself does not make use of it. It is merely offered for
105  /// historical, process-wide object registration by name. Instead, pass a
106  /// pointer to the object where you need to access it - this is also much
107  /// faster than a lookup by name.
108  static TDirectory& Heap();
109 };
110 
111 }
112 
113 #endif
#define ARGS(alist)
Definition: gifencode.c:10
TCoopPtr< T > Create(const std::string &name, ARGS...args)
Create an object of type T (passing some arguments to its constructor).
Definition: TDirectory.h:65
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double T(double x)
Definition: ChebyshevPol.h:34
const TKey & Add(const std::string &name, TCoopPtr< T > ptr)
Add an existing object (rather a TCoopPtr to it) to the TDirectory.
Definition: TDirectory.h:85
const TKey * FindKey(const std::string &name) const
Definition: TDirectory.h:74
#define R__LOG_HERE(LEVEL, GROUP)
Definition: TLogger.h:120
Key/value store of objects.
Definition: TDirectory.h:42
static TDirectory & Heap()
Dedicated, process-wide TDirectory.
Definition: TFile.cxx:20
Describe directory structure in memory.
Definition: TDirectory.h:41
std::shared_ptr< Internal::TCoopPtrTypeErasedBase > Value_t
The values referenced by a TDirectory are type erased TCoopPtrs: they can be of any type; the actual ...
Definition: TDirectory.h:47
#define name(a, b)
Definition: linkTestLib0.cpp:5
ContentMap_t fContent
The TDirectory's content.
Definition: TDirectory.h:55
std::unordered_map< TKey, Value_t > ContentMap_t
The directory content is a hashed map of TKey => Value_t.
Definition: TDirectory.h:52
Warnings about likely unexpected behavior.
Several pointers point to the same object, any of them can delete the object, setting all of them to ...
Definition: TCoopPtr.h:46