Logo ROOT   6.14/05
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
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TDirectoryEntry
17 #define ROOT7_TDirectoryEntry
18 
19 #include "TClass.h"
20 
21 #include <chrono>
22 #include <memory>
23 #include <typeinfo>
24 
25 namespace ROOT {
26 namespace Experimental {
27 
28 namespace Internal {
29 
31 public:
32  using clock_t = std::chrono::system_clock;
33  using time_point_t = std::chrono::time_point<clock_t>;
34 
35 private:
36  time_point_t fDate = clock_t::now(); ///< Time of last change
38  std::shared_ptr<void> fObj;
39 
40 public:
42 
43  TDirectoryEntry(std::nullptr_t): TDirectoryEntry(std::make_shared<std::nullptr_t>(nullptr)) {}
44 
45  template <class T>
46  explicit TDirectoryEntry(T *ptr): TDirectoryEntry(std::make_shared<T>(*ptr))
47  {}
48 
49  template <class T>
50  explicit TDirectoryEntry(const std::shared_ptr<T> &ptr): fType(TClass::GetClass(typeid(T))), fObj(ptr)
51  {}
52 
53  /// Get the last change date of the entry.
54  const time_point_t &GetDate() const { return fDate; }
55 
56  /// Inform the entry that it has been modified, and needs to update its
57  /// last-changed time stamp.
58  void SetChanged() { fDate = clock_t::now(); }
59 
60  /// Type of the object represented by this entry.
61  const std::type_info &GetTypeInfo() const { return *fType->GetTypeInfo(); }
62 
63  /// Get the object's type.
64  TClass *GetType() const { return fType; }
65 
66  /// Retrieve the `shared_ptr` of the referenced object.
67  std::shared_ptr<void> &GetPointer() { return fObj; }
68  const std::shared_ptr<void> &GetPointer() const { return fObj; }
69 
70  template <class U>
71  std::shared_ptr<U> CastPointer() const;
72 
73  explicit operator bool() const { return !!fObj; }
74 
75  void swap(TDirectoryEntry &other) noexcept;
76 };
77 
78 template <class U>
79 std::shared_ptr<U> TDirectoryEntry::CastPointer() const
80 {
81  if (auto ptr = fType->DynamicCast(TClass::GetClass(typeid(U)), fObj.get()))
82  return std::shared_ptr<U>(fObj, static_cast<U *>(ptr));
83  return std::shared_ptr<U>();
84 }
85 
86 inline void TDirectoryEntry::swap(TDirectoryEntry &other) noexcept
87 {
88  using std::swap;
89 
90  swap(fDate, other.fDate);
91  swap(fType, other.fType);
92  swap(fObj, other.fObj);
93 }
94 
95 inline void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
96 {
97  e1.swap(e2);
98 }
99 
100 } // namespace Internal
101 
102 } // namespace Experimental
103 } // namespace ROOT
104 #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:461
std::shared_ptr< void > & GetPointer()
Retrieve the shared_ptr of the referenced object.
STL namespace.
TClass * GetClass(T *)
Definition: TClass.h:577
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:75
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:2887
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:4729
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