Logo ROOT   6.10/09
Reference Guide
TDirectoryEntry.hxx
Go to the documentation of this file.
1 /// \file ROOT/TDirectoryEntry.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_TDirectoryEntry
16 #define ROOT7_TDirectoryEntry
17 
18 #include "TClass.h"
19 
20 #include <chrono>
21 #include <memory>
22 #include <typeinfo>
23 
24 namespace ROOT {
25 namespace Experimental {
26 
27 namespace Internal {
28 
30 public:
31  using clock_t = std::chrono::system_clock;
32  using time_point_t = std::chrono::time_point<clock_t>;
33 
34 private:
35  time_point_t fDate = clock_t::now(); ///< Time of last change
37  std::shared_ptr<void> fObj;
38 
39 public:
41  TDirectoryEntry(std::nullptr_t):
42  TDirectoryEntry(std::make_shared<std::nullptr_t>(nullptr)) {}
43  template<class T>
44  explicit TDirectoryEntry(T* ptr):
45  TDirectoryEntry(std::make_shared<T>(*ptr)) {}
46  template<class T>
47  explicit TDirectoryEntry(const std::shared_ptr<T>& ptr):
48  fType(TClass::GetClass(typeid(T))),
49  fObj(ptr) {}
50 
51  /// Get the last change date of the entry.
52  const time_point_t& GetDate() const { return fDate; }
53 
54  /// Inform the entry that it has been modified, and needs to update its
55  /// last-changed time stamp.
56  void SetChanged() { fDate = clock_t::now(); }
57 
58  /// Type of the object represented by this entry.
59  const std::type_info& GetTypeInfo() const { return *fType->GetTypeInfo(); }
60 
61  /// Get the object's type.
62  TClass* GetType() const { return fType; }
63 
64  /// Retrieve the `shared_ptr` of the referenced object.
65  std::shared_ptr<void>& GetPointer() { return fObj; }
66  const std::shared_ptr<void>& GetPointer() const { return fObj; }
67 
68  template<class U>
69  std::shared_ptr<U> CastPointer() const;
70 
71  explicit operator bool() const { return !!fObj; }
72 
73  void swap(TDirectoryEntry& other) noexcept;
74 };
75 
76 template<class U>
77 std::shared_ptr<U> TDirectoryEntry::CastPointer() const {
78  if (auto ptr = fType->DynamicCast(TClass::GetClass(typeid(U)), fObj.get()))
79  return std::shared_ptr<U>(fObj, static_cast<U*>(ptr));
80  return std::shared_ptr<U>();
81 }
82 
83 inline void TDirectoryEntry::swap(TDirectoryEntry& other) noexcept {
84  using std::swap;
85 
86  swap(fDate, other.fDate);
87  swap(fType, other.fType);
88  swap(fObj, other.fObj);
89 }
90 
91 inline void swap(TDirectoryEntry& e1, TDirectoryEntry& e2) noexcept {
92  e1.swap(e2);
93 }
94 
95 } // namespace Internal
96 
97 } // namespace Experimental
98 } // namespace ROOT
99 #endif
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
double T(double x)
Definition: ChebyshevPol.h:34
TClass * GetType() const
Get the object&#39;s type.
const std::type_info * GetTypeInfo() const
Definition: TClass.h:441
std::shared_ptr< void > & GetPointer()
Retrieve the shared_ptr of the referenced object.
STL namespace.
TClass * GetClass(T *)
Definition: TClass.h:545
void swap(TDirectoryEntry &other) noexcept
TDirectoryEntry(const std::shared_ptr< T > &ptr)
const std::shared_ptr< void > & GetPointer() const
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
const time_point_t & GetDate() const
Get the last change date of the entry.
time_point_t fDate
Time of last change.
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2885
const std::type_info & GetTypeInfo() const
Type of the object represented by this entry.
void * DynamicCast(const TClass *base, void *obj, Bool_t up=kTRUE)
Cast obj of this class type up to baseclass cl if up is true.
Definition: TClass.cxx:4643
void SetChanged()
Inform the entry that it has been modified, and needs to update its last-changed time stamp...
std::chrono::time_point< clock_t > time_point_t