Logo ROOT   6.10/09
Reference Guide
TDrawable.hxx
Go to the documentation of this file.
1 /// \file ROOT/TDrawable.h
2 /// \ingroup Base ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-08-07
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/CREDITS. *
13  *************************************************************************/
14 
15 #ifndef ROOT7_TDrawable
16 #define ROOT7_TDrawable
17 
18 #include <memory>
19 
20 namespace ROOT {
21 namespace Experimental {
22 class TCanvas;
23 
24 namespace Internal {
25 
26 /** \class TDrawable
27  Base class for drawable entities: objects that can be painted on a `TPad`.
28  */
29 
30 class TDrawable {
31 public:
32  virtual ~TDrawable();
33 
34  /// Paint the object
35  virtual void Paint(TCanvas& onCanv) = 0;
36 };
37 
38 /// \class TAnyPtr
39 /// Models a shared pointer or a unique pointer.
40 
41 template <class T>
42 class TUniWeakPtr {
43  union {
44  std::unique_ptr<T> fUnique;
45  std::weak_ptr<T> fWeak;
46  };
47  bool fIsWeak; ///< fUnique or fWeak?
48 
49 public:
50  /// \class Accessor
51  /// Gives transparent access to the shared or unique pointer.
52  /// Locks if needed.
53  class Accessor {
54  union {
55  std::shared_ptr<T> fShared;
56  T* fRaw;
57  };
58  bool fIsShared; ///< fRaw or fShared?
59 
60  public:
61  Accessor(const TUniWeakPtr& uniweak):
62  fIsShared(uniweak.fIsWeak) {
63  if (fIsShared)
64  fShared = uniweak.fWeak.lock();
65  else
66  fRaw = uniweak.fUnique.get();
67  }
68 
69  Accessor(Accessor&& rhs): fIsShared(rhs.fIsShared) {
70  if (fIsShared)
71  fShared.swap(rhs.fShared);
72  else
73  fRaw = rhs.fRaw;
74  }
75 
76  T* operator->() const { return fIsShared ? fRaw : fShared.get(); }
77  T& operator*() const { return *operator->(); }
78  operator bool() const { return fIsShared ? (bool)fRaw : (bool)fShared; }
79 
81  if (fIsShared)
82  fShared.~shared_ptr();
83  }
84  };
85 
86  TUniWeakPtr(const std::shared_ptr<T>& ptr): fWeak(ptr), fIsWeak(true) {}
87  TUniWeakPtr(std::unique_ptr<T>&& ptr): fUnique(std::move(ptr)), fIsWeak(false) {}
88  TUniWeakPtr(TUniWeakPtr&& rhs): fIsWeak(rhs.fIsWeak) {
89  if (fIsWeak)
90  fWeak.swap(rhs.fWeak);
91  else
92  fUnique.swap(rhs.fUnique);
93  }
94 
96  if (fIsWeak)
97  fWeak.~weak_ptr();
98  else
99  fUnique.~unique_ptr();
100  }
101 
102  Accessor Get() const { return Accessor(*this); }
103  void Reset() {
104  if (fIsWeak)
105  fWeak.reset();
106  else
107  fUnique.reset();
108  }
109 };
110 
111 } // namespace Internal
112 } // namespace Experimental
113 } // namespace ROOT
114 
115 #endif
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
Graphic container for TDrawable-s.
Definition: TCanvas.hxx:38
virtual void Paint(TCanvas &onCanv)=0
Paint the object.
STL namespace.
Gives transparent access to the shared or unique pointer.
Definition: TDrawable.hxx:53
TUniWeakPtr(std::unique_ptr< T > &&ptr)
Definition: TDrawable.hxx:87
The Canvas class.
Definition: TCanvas.h:31
TUniWeakPtr(const std::shared_ptr< T > &ptr)
Definition: TDrawable.hxx:86
Base class for drawable entities: objects that can be painted on a TPad.
Definition: TDrawable.hxx:30