Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RHolder.hxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#ifndef ROOT7_Browsable_RHolder
10#define ROOT7_Browsable_RHolder
11
12#include "TClass.h"
13
14#include <memory>
15
16namespace ROOT {
17namespace Experimental {
18
19class RLogChannel;
20/// Log channel for Browsable diagnostics.
21RLogChannel &BrowsableLog(); // implemented in RElement.cxx
22
23namespace Browsable {
24
25/** \class RHolder
26\ingroup rbrowser
27\brief Basic class for object holder of any kind. Could be used to transfer shared_ptr or unique_ptr or plain pointer
28\author Sergey Linev <S.Linev@gsi.de>
29\date 2019-10-19
30\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
31*/
32
33class RHolder {
34protected:
35
36 /** Returns pointer on existing shared_ptr<T> */
37 virtual void *GetShared() const { return nullptr; }
38
39 /** Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone() */
40 virtual void *TakeObject() { return nullptr; }
41
42 /** Returns plain object pointer without care about ownership, should not be used often */
43 virtual void *AccessObject() { return nullptr; }
44
45 /** Create copy of container, works only when pointer can be shared */
46 virtual RHolder *DoCopy() const { return nullptr; }
47
48public:
49 virtual ~RHolder() = default;
50
51 /** Returns class of contained object */
52 virtual const TClass *GetClass() const = 0;
53
54 /** Returns direct (temporary) object pointer */
55 virtual const void *GetObject() const = 0;
56
57 template <class T>
58 bool InheritsFrom() const
59 {
60 return TClass::GetClass<T>()->InheritsFrom(GetClass());
61 }
62
63 template <class T>
64 bool CanCastTo() const
65 {
66 return const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>()) == 0;
67 }
68
69 /** Returns direct object pointer cast to provided class */
70
71 template<class T>
72 const T *Get() const
73 {
74 if (CanCastTo<T>())
75 return (const T *) GetObject();
76
77 return nullptr;
78 }
79
80 /** Clone container. Trivial for shared_ptr and TObject holder, does not work for unique_ptr */
81 auto Copy() const { return std::unique_ptr<RHolder>(DoCopy()); }
82
83
84 /** Returns unique_ptr of contained object */
85 template<class T>
86 std::unique_ptr<T> get_unique()
87 {
88 // ensure that direct inheritance is used
89 if (!CanCastTo<T>())
90 return nullptr;
91 auto pobj = TakeObject();
92 if (pobj) {
93 std::unique_ptr<T> unique;
94 unique.reset(static_cast<T *>(pobj));
95 return unique;
96 }
97 return nullptr;
98 }
99
100 /** Returns shared_ptr of contained object */
101 template<class T>
102 std::shared_ptr<T> get_shared()
103 {
104 // ensure that direct inheritance is used
105 if (!CanCastTo<T>())
106 return nullptr;
107 auto pshared = GetShared();
108 if (pshared)
109 return *(static_cast<std::shared_ptr<T> *>(pshared));
110
111 // automatically convert unique pointer to shared
112 return get_unique<T>();
113 }
114
115 /** Returns plains pointer on object without ownership, only can be used for TObjects */
116 template<class T>
118 {
119 if (!CanCastTo<T>())
120 return nullptr;
121
122 return (T *) AccessObject();
123 }
124};
125
126} // namespace Browsable
127} // namespace Experimental
128} // namespace ROOT
129
130
131#endif
Basic class for object holder of any kind.
Definition RHolder.hxx:33
virtual const TClass * GetClass() const =0
Returns class of contained object.
virtual RHolder * DoCopy() const
Create copy of container, works only when pointer can be shared.
Definition RHolder.hxx:46
std::shared_ptr< T > get_shared()
Returns shared_ptr of contained object.
Definition RHolder.hxx:102
virtual void * AccessObject()
Returns plain object pointer without care about ownership, should not be used often.
Definition RHolder.hxx:43
const T * Get() const
Returns direct object pointer cast to provided class.
Definition RHolder.hxx:72
virtual const void * GetObject() const =0
Returns direct (temporary) object pointer.
virtual void * TakeObject()
Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone()
Definition RHolder.hxx:40
T * get_object()
Returns plains pointer on object without ownership, only can be used for TObjects.
Definition RHolder.hxx:117
auto Copy() const
Clone container.
Definition RHolder.hxx:81
virtual void * GetShared() const
Returns pointer on existing shared_ptr<T>
Definition RHolder.hxx:37
std::unique_ptr< T > get_unique()
Returns unique_ptr of contained object.
Definition RHolder.hxx:86
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:80
RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition RElement.cxx:18
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...