Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:38

0001 /// \file ROOT/RDirectoryEntry.hxx
0002 /// \ingroup Base ROOT7
0003 /// \author Axel Naumann <axel@cern.ch>
0004 /// \date 2015-07-31
0005 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
0006 /// is welcome!
0007 
0008 /*************************************************************************
0009  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers.               *
0010  * All rights reserved.                                                  *
0011  *                                                                       *
0012  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0013  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0014  *************************************************************************/
0015 
0016 #ifndef ROOT7_RDirectoryEntry
0017 #define ROOT7_RDirectoryEntry
0018 
0019 #include "TClass.h"
0020 
0021 #include <chrono>
0022 #include <memory>
0023 
0024 namespace ROOT {
0025 namespace Experimental {
0026 
0027 namespace Internal {
0028 
0029 class RDirectoryEntry {
0030 public:
0031    using clock_t = std::chrono::system_clock;
0032    using time_point_t = std::chrono::time_point<clock_t>;
0033 
0034 private:
0035    time_point_t fDate = clock_t::now(); ///< Time of last change
0036    TClass *fType;
0037    std::shared_ptr<void> fObj;
0038 
0039 public:
0040    RDirectoryEntry(): RDirectoryEntry(nullptr) {}
0041 
0042    RDirectoryEntry(std::nullptr_t): RDirectoryEntry(std::make_shared<std::nullptr_t>(nullptr)) {}
0043 
0044    template <class T>
0045    explicit RDirectoryEntry(T *ptr): RDirectoryEntry(std::make_shared<T>(*ptr))
0046    {}
0047 
0048    template <class T>
0049    explicit RDirectoryEntry(const std::shared_ptr<T> &ptr): fType(TClass::GetClass<T>()), fObj(ptr)
0050    {}
0051 
0052    /// Get the last change date of the entry.
0053    const time_point_t &GetDate() const { return fDate; }
0054 
0055    /// Inform the entry that it has been modified, and needs to update its
0056    /// last-changed time stamp.
0057    void SetChanged() { fDate = clock_t::now(); }
0058 
0059    /// Type of the object represented by this entry.
0060    const std::type_info &GetTypeInfo() const { return *fType->GetTypeInfo(); }
0061 
0062    /// Get the object's type.
0063    TClass *GetType() const { return fType; }
0064 
0065    /// Retrieve the `shared_ptr` of the referenced object.
0066    std::shared_ptr<void> &GetPointer() { return fObj; }
0067    const std::shared_ptr<void> &GetPointer() const { return fObj; }
0068 
0069    template <class U>
0070    std::shared_ptr<U> CastPointer() const;
0071 
0072    explicit operator bool() const { return !!fObj; }
0073 
0074    void swap(RDirectoryEntry &other) noexcept;
0075 };
0076 
0077 template <class U>
0078 std::shared_ptr<U> RDirectoryEntry::CastPointer() const
0079 {
0080    if (auto ptr = fType->DynamicCast(TClass::GetClass<U>(), fObj.get()))
0081       return std::shared_ptr<U>(fObj, static_cast<U *>(ptr));
0082    return std::shared_ptr<U>();
0083 }
0084 
0085 inline void RDirectoryEntry::swap(RDirectoryEntry &other) noexcept
0086 {
0087    using std::swap;
0088 
0089    swap(fDate, other.fDate);
0090    swap(fType, other.fType);
0091    swap(fObj, other.fObj);
0092 }
0093 
0094 inline void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept
0095 {
0096    e1.swap(e2);
0097 }
0098 
0099 } // namespace Internal
0100 
0101 } // namespace Experimental
0102 } // namespace ROOT
0103 #endif