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 {
17
18namespace Experimental {
19class RLogChannel;
20} // namespace Experimental
21
22/// Log channel for Browsable diagnostics.
23ROOT::Experimental::RLogChannel &BrowsableLog(); // implemented in RElement.cxx
24
25namespace Browsable {
26
27/** \class RHolder
28\ingroup rbrowser
29\brief Basic class for object holder of any kind. Could be used to transfer shared_ptr or unique_ptr or plain pointer
30\author Sergey Linev <S.Linev@gsi.de>
31\date 2019-10-19
32\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
33*/
34
35class RHolder {
36protected:
37
38 /** Returns pointer on existing shared_ptr<T> */
39 virtual void *GetShared() const { return nullptr; }
40
41 /** Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone() */
42 virtual void *TakeObject() { return nullptr; }
43
44 /** Returns plain object pointer without care about ownership, should not be used often */
45 virtual void *AccessObject() { return nullptr; }
46
47 /** Create copy of container, works only when pointer can be shared */
48 virtual RHolder *DoCopy() const { return nullptr; }
49
50public:
51 virtual ~RHolder() = default;
52
53 /** Returns class of contained object */
54 virtual const TClass *GetClass() const = 0;
55
56 /** Returns direct (temporary) object pointer */
57 virtual const void *GetObject() const = 0;
58
59 /** Clear all pointers without performing cleanup */
60 virtual void Forget() {}
61
62 template <class T>
63 bool InheritsFrom() const
64 {
65 return TClass::GetClass<T>()->InheritsFrom(GetClass());
66 }
67
68 template <class T>
69 bool CanCastTo() const
70 {
71 return const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>()) >= 0;
72 }
73
74 /** Returns direct object pointer cast to provided class */
75
76 template<class T>
77 const T *Get() const
78 {
79 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
80 if (offset >= 0)
81 return (const T *) ((char *) GetObject() + offset);
82
83 return nullptr;
84 }
85
86 /** Clone container. Trivial for shared_ptr and TObject holder, does not work for unique_ptr */
87 auto Copy() const { return std::unique_ptr<RHolder>(DoCopy()); }
88
89 /** Returns unique_ptr of contained object */
90 template<class T>
91 std::unique_ptr<T> get_unique()
92 {
93 // ensure that direct inheritance is used
94 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
95 if (offset < 0)
96 return nullptr;
97 auto pobj = TakeObject();
98 if (pobj) {
99 std::unique_ptr<T> unique;
100 unique.reset((T *)((char *) pobj + offset));
101 return unique;
102 }
103 return nullptr;
104 }
105
106 /** Returns shared_ptr of contained object */
107 template<class T>
108 std::shared_ptr<T> get_shared()
109 {
110 // ensure that direct inheritance is used
111 if (!CanCastTo<T>())
112 return nullptr;
113 auto pshared = GetShared();
114 if (pshared)
115 return *(static_cast<std::shared_ptr<T> *>(pshared));
116
117 // automatically convert unique pointer to shared
118 return get_unique<T>();
119 }
120
121 /** Returns plains pointer on object without ownership, only can be used for TObjects */
122 template<class T>
124 {
125 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
126 if (offset < 0)
127 return nullptr;
128
129 return (T *) ((char *)AccessObject() + offset);
130 }
131};
132
133} // namespace Browsable
134} // namespace ROOT
135
136
137#endif
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Basic class for object holder of any kind.
Definition RHolder.hxx:35
bool CanCastTo() const
Definition RHolder.hxx:69
auto Copy() const
Clone container.
Definition RHolder.hxx:87
virtual void Forget()
Clear all pointers without performing cleanup.
Definition RHolder.hxx:60
virtual const TClass * GetClass() const =0
Returns class of contained object.
virtual void * GetShared() const
Returns pointer on existing shared_ptr<T>
Definition RHolder.hxx:39
virtual void * AccessObject()
Returns plain object pointer without care about ownership, should not be used often.
Definition RHolder.hxx:45
std::shared_ptr< T > get_shared()
Returns shared_ptr of contained object.
Definition RHolder.hxx:108
std::unique_ptr< T > get_unique()
Returns unique_ptr of contained object.
Definition RHolder.hxx:91
virtual void * TakeObject()
Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone()
Definition RHolder.hxx:42
virtual RHolder * DoCopy() const
Create copy of container, works only when pointer can be shared.
Definition RHolder.hxx:48
virtual const void * GetObject() const =0
Returns direct (temporary) object pointer.
virtual ~RHolder()=default
T * get_object()
Returns plains pointer on object without ownership, only can be used for TObjects.
Definition RHolder.hxx:123
const T * Get() const
Returns direct object pointer cast to provided class.
Definition RHolder.hxx:77
bool InheritsFrom() const
Definition RHolder.hxx:63
A log configuration for a channel, e.g.
Definition RLogger.hxx:101
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
ROOT::Experimental::RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition RElement.cxx:20