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 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
75 if (offset >= 0)
76 return (const T *) ((char *) GetObject() + offset);
77
78 return nullptr;
79 }
80
81 /** Clone container. Trivial for shared_ptr and TObject holder, does not work for unique_ptr */
82 auto Copy() const { return std::unique_ptr<RHolder>(DoCopy()); }
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 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
90 if (offset < 0)
91 return nullptr;
92 auto pobj = TakeObject();
93 if (pobj) {
94 std::unique_ptr<T> unique;
95 unique.reset((T *)((char *) pobj + offset));
96 return unique;
97 }
98 return nullptr;
99 }
100
101 /** Returns shared_ptr of contained object */
102 template<class T>
103 std::shared_ptr<T> get_shared()
104 {
105 // ensure that direct inheritance is used
106 if (!CanCastTo<T>())
107 return nullptr;
108 auto pshared = GetShared();
109 if (pshared)
110 return *(static_cast<std::shared_ptr<T> *>(pshared));
111
112 // automatically convert unique pointer to shared
113 return get_unique<T>();
114 }
115
116 /** Returns plains pointer on object without ownership, only can be used for TObjects */
117 template<class T>
119 {
120 auto offset = const_cast<TClass *>(GetClass())->GetBaseClassOffset(TClass::GetClass<T>());
121 if (offset < 0)
122 return nullptr;
123
124 return (T *) ((char *)AccessObject() + offset);
125 }
126};
127
128} // namespace Browsable
129} // namespace Experimental
130} // namespace ROOT
131
132
133#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: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:103
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:118
auto Copy() const
Clone container.
Definition RHolder.hxx:82
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:81
RLogChannel & BrowsableLog()
Log channel for Browsable diagnostics.
Definition RElement.cxx:20
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.