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*/
31
32class RHolder {
33protected:
34
35 /** Returns pointer on existing shared_ptr<T> */
36 virtual void *GetShared() const { return nullptr; }
37
38 /** Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone() */
39 virtual void *TakeObject() { return nullptr; }
40
41 /** Returns plain object pointer without care about ownership, should not be used often */
42 virtual void *AccessObject() { return nullptr; }
43
44 /** Create copy of container, works only when pointer can be shared */
45 virtual RHolder *DoCopy() const { return nullptr; }
46
47public:
48 virtual ~RHolder() = default;
49
50 /** Returns class of contained object */
51 virtual const TClass *GetClass() const = 0;
52
53 /** Returns direct (temporary) object pointer */
54 virtual const void *GetObject() const = 0;
55
56 /** Clear all pointers without performing cleanup */
57 virtual void Forget() {}
58
59 template <class T>
60 bool InheritsFrom() const
61 {
62 return TClass::GetClass<T>()->InheritsFrom(GetClass());
63 }
64
65 template <class T>
66 bool CanCastTo() const
67 {
68 return const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>()) >= 0;
69 }
70
71 /** Returns direct object pointer cast to provided class */
72
73 template<class T>
74 const T *Get() const
75 {
76 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
77 if (offset >= 0)
78 return (const T *) ((char *) GetObject() + offset);
79
80 return nullptr;
81 }
82
83 /** Clone container. Trivial for shared_ptr and TObject holder, does not work for unique_ptr */
84 auto Copy() const { return std::unique_ptr<RHolder>(DoCopy()); }
85
86 /** Returns unique_ptr of contained object */
87 template<class T>
88 std::unique_ptr<T> get_unique()
89 {
90 // ensure that direct inheritance is used
91 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
92 if (offset < 0)
93 return nullptr;
94 auto pobj = TakeObject();
95 if (pobj) {
96 std::unique_ptr<T> unique;
97 unique.reset((T *)((char *) pobj + offset));
98 return unique;
99 }
100 return nullptr;
101 }
102
103 /** Returns shared_ptr of contained object */
104 template<class T>
105 std::shared_ptr<T> get_shared()
106 {
107 // ensure that direct inheritance is used
108 if (!CanCastTo<T>())
109 return nullptr;
110 auto pshared = GetShared();
111 if (pshared)
112 return *(static_cast<std::shared_ptr<T> *>(pshared));
113
114 // automatically convert unique pointer to shared
115 return get_unique<T>();
116 }
117
118 /** Returns plains pointer on object without ownership, only can be used for TObjects */
119 template<class T>
121 {
122 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
123 if (offset < 0)
124 return nullptr;
125
126 return (T *) ((char *)AccessObject() + offset);
127 }
128};
129
130} // namespace Browsable
131} // namespace ROOT
132
133
134#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:32
bool CanCastTo() const
Definition RHolder.hxx:66
auto Copy() const
Clone container.
Definition RHolder.hxx:84
virtual void Forget()
Clear all pointers without performing cleanup.
Definition RHolder.hxx:57
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:36
virtual void * AccessObject()
Returns plain object pointer without care about ownership, should not be used often.
Definition RHolder.hxx:42
std::shared_ptr< T > get_shared()
Returns shared_ptr of contained object.
Definition RHolder.hxx:105
std::unique_ptr< T > get_unique()
Returns unique_ptr of contained object.
Definition RHolder.hxx:88
virtual void * TakeObject()
Returns pointer with ownership, normally via unique_ptr<T>::release() or tobj->Clone()
Definition RHolder.hxx:39
virtual RHolder * DoCopy() const
Create copy of container, works only when pointer can be shared.
Definition RHolder.hxx:45
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:120
const T * Get() const
Returns direct object pointer cast to provided class.
Definition RHolder.hxx:74
bool InheritsFrom() const
Definition RHolder.hxx:60
A log configuration for a channel, e.g.
Definition RLogger.hxx:97
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
ROOT::RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition RElement.cxx:22