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
18class RLogChannel;
19
20/// Log channel for Browsable diagnostics.
21ROOT::RLogChannel &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 /** Clear all pointers without performing cleanup */
58 virtual void Forget() {}
59
60 template <class T>
61 bool InheritsFrom() const
62 {
63 return TClass::GetClass<T>()->InheritsFrom(GetClass());
64 }
65
66 template <class T>
67 bool CanCastTo() const
68 {
69 return const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>()) >= 0;
70 }
71
72 /** Returns direct object pointer cast to provided class */
73
74 template<class T>
75 const T *Get() const
76 {
77 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
78 if (offset >= 0)
79 return (const T *) ((char *) GetObject() + offset);
80
81 return nullptr;
82 }
83
84 /** Clone container. Trivial for shared_ptr and TObject holder, does not work for unique_ptr */
85 auto Copy() const { return std::unique_ptr<RHolder>(DoCopy()); }
86
87 /** Returns unique_ptr of contained object */
88 template<class T>
89 std::unique_ptr<T> get_unique()
90 {
91 // ensure that direct inheritance is used
92 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
93 if (offset < 0)
94 return nullptr;
95 auto pobj = TakeObject();
96 if (pobj) {
97 std::unique_ptr<T> unique;
98 unique.reset((T *)((char *) pobj + offset));
99 return unique;
100 }
101 return nullptr;
102 }
103
104 /** Returns shared_ptr of contained object */
105 template<class T>
106 std::shared_ptr<T> get_shared()
107 {
108 // ensure that direct inheritance is used
109 if (!CanCastTo<T>())
110 return nullptr;
111 auto pshared = GetShared();
112 if (pshared)
113 return *(static_cast<std::shared_ptr<T> *>(pshared));
114
115 // automatically convert unique pointer to shared
116 return get_unique<T>();
117 }
118
119 /** Returns plains pointer on object without ownership, only can be used for TObjects */
120 template<class T>
122 {
123 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
124 if (offset < 0)
125 return nullptr;
126
127 return (T *) ((char *)AccessObject() + offset);
128 }
129};
130
131} // namespace Browsable
132} // namespace ROOT
133
134
135#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:33
bool CanCastTo() const
Definition RHolder.hxx:67
auto Copy() const
Clone container.
Definition RHolder.hxx:85
virtual void Forget()
Clear all pointers without performing cleanup.
Definition RHolder.hxx:58
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:37
virtual void * AccessObject()
Returns plain object pointer without care about ownership, should not be used often.
Definition RHolder.hxx:43
std::shared_ptr< T > get_shared()
Returns shared_ptr of contained object.
Definition RHolder.hxx:106
std::unique_ptr< T > get_unique()
Returns unique_ptr of contained object.
Definition RHolder.hxx:89
virtual void * TakeObject()
Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone()
Definition RHolder.hxx:40
virtual RHolder * DoCopy() const
Create copy of container, works only when pointer can be shared.
Definition RHolder.hxx:46
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:121
const T * Get() const
Returns direct object pointer cast to provided class.
Definition RHolder.hxx:75
bool InheritsFrom() const
Definition RHolder.hxx:61
A log configuration for a channel, e.g.
Definition RLogger.hxx:98
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
ROOT::RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition RElement.cxx:20