Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RDirectoryEntry.hxx
Go to the documentation of this file.
1 /// \file ROOT/RDirectoryEntry.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_RDirectoryEntry
17 #define ROOT7_RDirectoryEntry
18 
19 #include "TClass.h"
20 
21 #include <chrono>
22 #include <memory>
23 
24 namespace ROOT {
25 namespace Experimental {
26 
27 namespace Internal {
28 
29 class RDirectoryEntry {
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
36  TClass *fType;
37  std::shared_ptr<void> fObj;
38 
39 public:
40  RDirectoryEntry(): RDirectoryEntry(nullptr) {}
41 
42  RDirectoryEntry(std::nullptr_t): RDirectoryEntry(std::make_shared<std::nullptr_t>(nullptr)) {}
43 
44  template <class T>
45  explicit RDirectoryEntry(T *ptr): RDirectoryEntry(std::make_shared<T>(*ptr))
46  {}
47 
48  template <class T>
49  explicit RDirectoryEntry(const std::shared_ptr<T> &ptr): fType(TClass::GetClass<T>()), fObj(ptr)
50  {}
51 
52  /// Get the last change date of the entry.
53  const time_point_t &GetDate() const { return fDate; }
54 
55  /// Inform the entry that it has been modified, and needs to update its
56  /// last-changed time stamp.
57  void SetChanged() { fDate = clock_t::now(); }
58 
59  /// Type of the object represented by this entry.
60  const std::type_info &GetTypeInfo() const { return *fType->GetTypeInfo(); }
61 
62  /// Get the object's type.
63  TClass *GetType() const { return fType; }
64 
65  /// Retrieve the `shared_ptr` of the referenced object.
66  std::shared_ptr<void> &GetPointer() { return fObj; }
67  const std::shared_ptr<void> &GetPointer() const { return fObj; }
68 
69  template <class U>
70  std::shared_ptr<U> CastPointer() const;
71 
72  explicit operator bool() const { return !!fObj; }
73 
74  void swap(RDirectoryEntry &other) noexcept;
75 };
76 
77 template <class U>
78 std::shared_ptr<U> RDirectoryEntry::CastPointer() const
79 {
80  if (auto ptr = fType->DynamicCast(TClass::GetClass<U>(), fObj.get()))
81  return std::shared_ptr<U>(fObj, static_cast<U *>(ptr));
82  return std::shared_ptr<U>();
83 }
84 
85 inline void RDirectoryEntry::swap(RDirectoryEntry &other) noexcept
86 {
87  using std::swap;
88 
89  swap(fDate, other.fDate);
90  swap(fType, other.fType);
91  swap(fObj, other.fObj);
92 }
93 
94 inline void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
95 {
96  e1.swap(e2);
97 }
98 
99 } // namespace Internal
100 
101 } // namespace Experimental
102 } // namespace ROOT
103 #endif