Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RDrawable.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2015, 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_RDrawable
10 #define ROOT7_RDrawable
11 
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <ROOT/RAttrMap.hxx>
17 #include <ROOT/RStyle.hxx>
18 
19 
20 namespace ROOT {
21 namespace Experimental {
22 
23 class RMenuItems;
24 class RPadBase;
25 class RAttrBase;
26 class RDisplayItem;
27 class RLegend;
28 
29 
30 namespace Internal {
31 
32 /** \class RIOSharedBase
33 \ingroup GpadROOT7
34 \author Sergey Linev <s.linev@gsi.de>
35 \date 2019-09-24
36 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
37 */
38 
39 class RIOSharedBase {
40 public:
41  virtual const void *GetIOPtr() const = 0;
42  virtual bool HasShared() const = 0;
43  virtual void *MakeShared() = 0;
44  virtual void SetShared(void *shared) = 0;
45  virtual ~RIOSharedBase() = default;
46 };
47 
48 using RIOSharedVector_t = std::vector<RIOSharedBase *>;
49 
50 template<class T>
51 class RIOShared final : public RIOSharedBase {
52  std::shared_ptr<T> fShared; ///<! holder of object
53  T* fIO{nullptr}; ///< plain pointer for IO
54 public:
55  const void *GetIOPtr() const final { return fIO; }
56  virtual bool HasShared() const final { return fShared.get() != nullptr; }
57  virtual void *MakeShared() final { fShared.reset(fIO); return &fShared; }
58  virtual void SetShared(void *shared) final { fShared = *((std::shared_ptr<T> *) shared); }
59 
60  RIOShared() = default;
61 
62  RIOShared(const std::shared_ptr<T> &ptr) : RIOSharedBase()
63  {
64  fShared = ptr;
65  fIO = ptr.get();
66  }
67 
68  RIOShared &operator=(const std::shared_ptr<T> &ptr)
69  {
70  fShared = ptr;
71  fIO = ptr.get();
72  return *this;
73  }
74 
75  operator bool() const { return !!fShared || !!fIO; }
76 
77  const T *get() const { return fShared ? fShared.get() : fIO; }
78  T *get() { return fShared ? fShared.get() : fIO; }
79 
80  const T *operator->() const { return get(); }
81  T *operator->() { return get(); }
82 
83  std::shared_ptr<T> get_shared() const { return fShared; }
84 
85  void reset() { fShared.reset(); fIO = nullptr; }
86 };
87 
88 }
89 
90 /** \class RDrawable
91 \ingroup GpadROOT7
92 \brief Base class for drawable entities: objects that can be painted on a `RPad`.
93 \author Axel Naumann <axel@cern.ch>
94 \author Sergey Linev <s.linev@gsi.de>
95 \date 2015-08-07
96 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
97 */
98 
99 class RDrawable {
100 
101 friend class RPadBase; // to access Display method
102 friend class RAttrBase;
103 friend class RStyle;
104 friend class RLegend; // to access CollectShared method
105 
106 private:
107  RAttrMap fAttr; ///< attributes values
108  std::weak_ptr<RStyle> fStyle; ///<! style applied for RDrawable
109  std::string fCssType; ///<! drawable type, not stored in the root file, must be initialized in constructor
110  std::string fCssClass; ///< user defined drawable class, can later go inside map
111  std::string fId; ///< optional object identifier, may be used in CSS as well
112 
113 protected:
114 
115  virtual void CollectShared(Internal::RIOSharedVector_t &) {}
116 
117  RAttrMap &GetAttrMap() { return fAttr; }
118  const RAttrMap &GetAttrMap() const { return fAttr; }
119 
120  bool MatchSelector(const std::string &selector) const;
121 
122  virtual std::unique_ptr<RDisplayItem> Display() const;
123 
124 public:
125 
126  explicit RDrawable(const std::string &type) : fCssType(type) {}
127 
128  virtual ~RDrawable();
129 
130  // copy constructor and assign operator !!!
131 
132  /** Method can be used to provide menu items for the drawn object */
133  virtual void PopulateMenu(RMenuItems &){};
134 
135  virtual void Execute(const std::string &);
136 
137  virtual void UseStyle(const std::shared_ptr<RStyle> &style) { fStyle = style; }
138  void ClearStyle() { UseStyle(nullptr); }
139 
140  void SetCssClass(const std::string &cl) { fCssClass = cl; }
141  const std::string &GetCssClass() const { return fCssClass; }
142 
143  const std::string &GetCssType() const { return fCssType; }
144 
145  const std::string &GetId() const { return fId; }
146  void SetId(const std::string &id) { fId = id; }
147 
148 };
149 
150 /// Central method to insert drawable in list of pad primitives
151 /// By default drawable placed as is.
152 template <class DRAWABLE, std::enable_if_t<std::is_base_of<RDrawable, DRAWABLE>{}>* = nullptr>
153 inline auto GetDrawable(const std::shared_ptr<DRAWABLE> &drawable)
154 {
155  return drawable;
156 }
157 
158 } // namespace Experimental
159 } // namespace ROOT
160 
161 #endif