Logo ROOT   6.16/01
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
25namespace ROOT {
26namespace Experimental {
27
28namespace Internal {
29
31public:
32 using clock_t = std::chrono::system_clock;
33 using time_point_t = std::chrono::time_point<clock_t>;
34
35private:
36 time_point_t fDate = clock_t::now(); ///< Time of last change
38 std::shared_ptr<void> fObj;
39
40public:
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
78template <class U>
79std::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
86inline 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
95inline 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
PyObject * fType
const time_point_t & GetDate() const
Get the last change date of the entry.
const std::shared_ptr< void > & GetPointer() const
void SetChanged()
Inform the entry that it has been modified, and needs to update its last-changed time stamp.
TDirectoryEntry(const std::shared_ptr< T > &ptr)
time_point_t fDate
Time of last change.
std::shared_ptr< void > & GetPointer()
Retrieve the shared_ptr of the referenced object.
std::chrono::time_point< clock_t > time_point_t
TClass * GetType() const
Get the object's type.
void swap(TDirectoryEntry &other) noexcept
const std::type_info & GetTypeInfo() const
Type of the object represented by this entry.
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:75
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:4761
const std::type_info * GetTypeInfo() const
Definition: TClass.h:466
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
void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
double T(double x)
Definition: ChebyshevPol.h:34
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TClass * GetClass(T *)
Definition: TClass.h:582
STL namespace.
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value and is_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:12929