Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RDirectoryEntry.hxx
Go to the documentation of this file.
1/// \file ROOT/RDirectoryEntry.hxx
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
24namespace ROOT {
25namespace Experimental {
26
27namespace Internal {
28
30public:
31 using clock_t = std::chrono::system_clock;
32 using time_point_t = std::chrono::time_point<clock_t>;
33
34private:
35 time_point_t fDate = clock_t::now(); ///< Time of last change
37 std::shared_ptr<void> fObj;
38
39public:
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
77template <class U>
78std::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
85inline 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
94inline 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
const time_point_t & GetDate() const
Get the last change date of the entry.
RDirectoryEntry(const std::shared_ptr< T > &ptr)
std::chrono::time_point< clock_t > time_point_t
const std::shared_ptr< void > & GetPointer() const
TClass * GetType() const
Get the object's type.
void SetChanged()
Inform the entry that it has been modified, and needs to update its last-changed time stamp.
void swap(RDirectoryEntry &other) noexcept
const std::type_info & GetTypeInfo() const
Type of the object represented by this entry.
std::shared_ptr< void > & GetPointer()
Retrieve the shared_ptr of the referenced object.
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
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:4915
const std::type_info * GetTypeInfo() const
Definition TClass.h:496
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
TClass * GetClass(T *)
Definition TClass.h:663