Logo ROOT   6.12/07
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
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TDrawable
17 #define ROOT7_TDrawable
18 
19 #include <memory>
20 #include <string>
21 
22 namespace ROOT {
23 namespace Experimental {
24 
25 class TCanvas;
26 class TMenuItems;
27 
28 namespace Internal {
29 class TVirtualCanvasPainter;
30 }
31 
32 /** \class TDrawable
33  Base class for drawable entities: objects that can be painted on a `TPad`.
34  */
35 
36 class TDrawable {
37 public:
38  virtual ~TDrawable();
39 
40  virtual void Paint(Internal::TVirtualCanvasPainter &onCanv) = 0;
41 
42  /** Method can be used to provide menu items for the drawn object */
43  virtual void PopulateMenu(TMenuItems &){};
44 
45  virtual void Execute(const std::string &);
46 };
47 
48 namespace Internal {
49 
50 /// \class TAnyPtr
51 /// Models a shared pointer or a unique pointer.
52 
53 template <class T>
54 class TUniWeakPtr {
55  // Needs I/O support for union (or variant, actually) {
56  std::unique_ptr<T> fUnique;
57  std::weak_ptr<T> fWeak; //! Cannot save for now :-(
58  T* fWeakForIO = nullptr; // Hack to allow streaming *out* of fWeak (reading is still broken because we don't set fWeak)
59  // };
60  bool fIsWeak = false; ///< fUnique or fWeak?
61 
62 public:
63  /// \class Accessor
64  /// Gives transparent access to the shared or unique pointer.
65  /// Locks if needed.
66  class Accessor {
67  union {
68  T *fRaw; ///< The raw, non-owning pointer accessing a TUniWeak's unique_ptr
69  std::shared_ptr<T> fShared; ///< The shared_ptr accessing a TUniWeak's weak_ptr
70  };
71  bool fIsShared; ///< fRaw or fShared?
72 
73  public:
74  Accessor(const TUniWeakPtr &uniweak): fIsShared(uniweak.fIsWeak)
75  {
76  if (fIsShared)
77  new (&fShared) std::shared_ptr<T>(uniweak.fWeak.lock());
78  else
79  fRaw = uniweak.fUnique.get();
80  }
81 
82  Accessor(Accessor &&rhs): fIsShared(rhs.fIsShared)
83  {
84  if (fIsShared)
85  new (&fShared) std::shared_ptr<T>(std::move(rhs.fShared));
86  else
87  fRaw = rhs.fRaw;
88  }
89 
90  T *operator->() const { return fIsShared ? fRaw : fShared.get(); }
91  T &operator*() const { return *operator->(); }
92  operator bool() const { return fIsShared ? (bool)fRaw : (bool)fShared; }
93 
95  {
96  if (fIsShared)
97  fShared.~shared_ptr();
98  }
99  };
100 
101  TUniWeakPtr() = default;
102  TUniWeakPtr(const std::shared_ptr<T> &ptr): fWeak(ptr), fWeakForIO(ptr.get()), fIsWeak(true) {}
103  TUniWeakPtr(std::unique_ptr<T> &&ptr): fUnique(std::move(ptr)), fIsWeak(false) {}
104  TUniWeakPtr(TUniWeakPtr &&rhs): fIsWeak(rhs.fIsWeak)
105  {
106  if (rhs.fIsWeak) {
107  fWeak = std::move(rhs.fWeak);
108  auto shptr = rhs.fWeak.lock();
109  fWeakForIO = shptr.get();
110  } else {
111  fUnique = std::move(rhs.fUnique);
112  }
113  }
114 
116  {
117  }
118 
119  Accessor Get() const { return Accessor(*this); }
120  void Reset()
121  {
122  if (fIsWeak)
123  fWeak.reset();
124  else
125  fUnique.reset();
126  }
127 };
128 
129 } // namespace Internal
130 } // namespace Experimental
131 } // namespace ROOT
132 
133 #endif
Base class for drawable entities: objects that can be painted on a TPad.
Definition: TDrawable.hxx:36
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double T(double x)
Definition: ChebyshevPol.h:34
STL namespace.
Gives transparent access to the shared or unique pointer.
Definition: TDrawable.hxx:66
std::shared_ptr< T > fShared
The shared_ptr accessing a TUniWeak&#39;s weak_ptr.
Definition: TDrawable.hxx:69
T * fRaw
The raw, non-owning pointer accessing a TUniWeak&#39;s unique_ptr.
Definition: TDrawable.hxx:68
TUniWeakPtr(std::unique_ptr< T > &&ptr)
Definition: TDrawable.hxx:103
Abstract interface for painting a canvas.
The Canvas class.
Definition: TCanvas.h:31
TUniWeakPtr(const std::shared_ptr< T > &ptr)
Definition: TDrawable.hxx:102
virtual void PopulateMenu(TMenuItems &)
Method can be used to provide menu items for the drawn object.
Definition: TDrawable.hxx:43