Logo ROOT   6.12/07
Reference Guide
TDrawingAttrs.hxx
Go to the documentation of this file.
1 /// \file ROOT/TDrawingOptsBase.hxx
2 /// \ingroup Gpad ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2017-09-26
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-2017, 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_TDrawingAttrs
17 #define ROOT7_TDrawingAttrs
18 
19 #include <ROOT/TColor.hxx>
20 
21 #include <RStringView.h>
22 
23 #include <map>
24 #include <string>
25 #include <vector>
26 
27 namespace ROOT {
28 namespace Experimental {
29 class TCanvas;
30 class TPadBase;
31 class TDrawingOptsBaseNoDefault;
32 
33 namespace Internal {
34 
35 template <class PRIMITIVE>
37 }
38 
39 /** \class ROOT::Experimental::TDrawingAttrRef
40  The `TCanvas` keep track of `TColor`s, integer and floating point attributes used by the drawing options,
41  making them accessible from other drawing options. The index into the table of the active attributes is
42  wrapped into `TDrawingAttrRef` to make them type-safe (i.e. distinct for `TColor`, `long long` and `double`).
43  */
44 
45 template <class PRIMITIVE>
46 class TDrawingAttrRef {
47 private:
48  size_t fIdx = (size_t)-1; ///< The index in the relevant attribute table of `TCanvas`.
49 
50  /// Construct a reference given the index.
51  explicit TDrawingAttrRef(size_t idx): fIdx(idx) {}
52 
53  friend class Internal::TDrawingAttrTable<PRIMITIVE>;
54 
55 public:
56  /// Construct an invalid reference.
57  TDrawingAttrRef() = default;
58 
59  /// Construct a reference from its options object, name, default value and set of string options.
60  ///
61  /// Initializes the PRIMITIVE to the default value, as available in TDrawingOptsBase::GetDefaultCanvas(),
62  /// or to `deflt` if no entry exists under the attribute name. The attribute name is `opts.GetName() + "." + attrName`.
63  /// `optStrings` is only be used if `PRIMITIVE` is `long long`; the style setting is expected to be one of
64  /// the strings, with the attribute's value the index of the string.
65  TDrawingAttrRef(TDrawingOptsBaseNoDefault &opts, const std::string &attrName, const PRIMITIVE &deflt,
66  const std::vector<std::string_view> &optStrings = {});
67 
68  /// Get the underlying index.
69  operator size_t() const { return fIdx; }
70 
71  /// Whether the reference is valid.
72  explicit operator bool() const { return fIdx != (size_t)-1; }
73 };
74 
75 extern template class TDrawingAttrRef<TColor>;
76 extern template class TDrawingAttrRef<long long>;
77 extern template class TDrawingAttrRef<double>;
78 
79 namespace Internal {
80 
81 template <class PRIMITIVE>
83  /// The value.
84  PRIMITIVE fVal;
85  /// The value's use count.
86  size_t fUseCount = 1;
87 
88  /// Clear the value; use count must be 0.
89  void Clear();
90 
91 public:
92  /// Default constructor: a default-constructed value that is unused.
93  TDrawingAttrAndUseCount(): fVal(), fUseCount(0) {}
94 
95  /// Initialize with a value, setting use count to 1.
96  explicit TDrawingAttrAndUseCount(const PRIMITIVE &val): fVal(val) {}
97 
98  /// Create a value, initializing use count to 1.
99  void Create(const PRIMITIVE &val);
100 
101  /// Increase the use count; use count must be >= 1 before (i.e. does not create or "resurrect" values).
102  void IncrUse();
103 
104  /// Decrease the use count; use count must be >= 1 before the call. Calls Clear() if use count drops to 0.
105  void DecrUse();
106 
107  /// Whether the use count is 0 and this object has space for a new value.
108  bool IsFree() const { return fUseCount == 0; }
109 
110  /// Value access (non-const).
111  PRIMITIVE &Get() { return fVal; }
112 
113  /// Value access (const).
114  const PRIMITIVE &Get() const { return fVal; }
115 };
116 
117 // Only these specializations are used and provided:
118 extern template class TDrawingAttrAndUseCount<TColor>;
119 extern template class TDrawingAttrAndUseCount<long long>;
120 extern template class TDrawingAttrAndUseCount<double>;
121 
122 template <class PRIMITIVE>
123 class TDrawingAttrTable {
124 public:
126 
127 private:
128  /// Table of attribute primitives. Slots can be freed and re-used.
129  /// Drawing options will reference slots in here through their index.
130  std::vector<value_type> fTable;
131 
132 public:
133  /// Register an attribute with the table.
134  /// \returns the index in the table.
135  TDrawingAttrRef<PRIMITIVE> Register(const PRIMITIVE &val);
136 
137  /// Add a use of the attribute at table index idx.
138  void IncrUse(TDrawingAttrRef<PRIMITIVE> idx) { fTable[idx].IncrUse(); }
139 
140  /// Remove a use of the attribute at table index idx.
141  void DecrUse(TDrawingAttrRef<PRIMITIVE> idx) { fTable[idx].DecrUse(); }
142 
143  /// Update an existing attribute entry in the table.
144  void Update(TDrawingAttrRef<PRIMITIVE> idx, const PRIMITIVE &val) { fTable[idx] = value_type(val); }
145 
146  /// Get the value at index `idx` (const version).
147  const PRIMITIVE &Get(TDrawingAttrRef<PRIMITIVE> idx) const { return fTable[idx].Get(); }
148 
149  /// Get the value at index `idx` (non-const version).
150  PRIMITIVE &Get(TDrawingAttrRef<PRIMITIVE> idx) { return fTable[idx].Get(); }
151 
152  /// Find the index belonging to the attribute at the given address and add a use.
153  /// \returns the reference to `val`, which might be `IsInvalid()` if `val` is not part of this table.
154  TDrawingAttrRef<PRIMITIVE> SameAs(const PRIMITIVE &val);
155 
156  /// Access to the underlying attribute table (non-const version).
157  std::vector<value_type> &GetTable() { return fTable; }
158 
159  /// Access to the underlying attribute table (const version).
160  const std::vector<value_type> &GetTable() const { return fTable; }
161 };
162 
163 extern template class TDrawingAttrTable<TColor>;
164 extern template class TDrawingAttrTable<long long>;
165 extern template class TDrawingAttrTable<double>;
166 } // namespace Internal
167 
168 struct TLineAttrs {
171 
172  struct Width {
173  long long fVal;
174  explicit operator long long() const { return fVal; }
175  };
176 
177  TLineAttrs() = default;
178  /// Construct from the pad that holds our `TDrawable`, passing the configuration name of this line attribute.
179  TLineAttrs(TDrawingOptsBaseNoDefault &opts, const std::string &name, const TColor &col, Width width)
180  : fColor(opts, name + ".Color", col), fWidth(opts, name + ".Width", (long long)width)
181  {}
182 };
183 
184 struct TFillAttrs {
186 
187  TFillAttrs() = default;
188 
189  /// Construct from the pad that holds our `TDrawable`, passing the configuration name of this line attribute.
190  TFillAttrs(TDrawingOptsBaseNoDefault &opts, const std::string &name, const TColor &col)
191  : fColor(opts, name + ".Color", col)
192  {}
193 };
194 
195 } // namespace Experimental
196 } // namespace ROOT
197 
198 #endif // ROOT7_TDrawingAttrs
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
void DecrUse(TDrawingAttrRef< PRIMITIVE > idx)
Remove a use of the attribute at table index idx.
std::vector< value_type > fTable
Table of attribute primitives.
const PRIMITIVE & Get(TDrawingAttrRef< PRIMITIVE > idx) const
Get the value at index idx (const version).
void IncrUse(TDrawingAttrRef< PRIMITIVE > idx)
Add a use of the attribute at table index idx.
const std::vector< value_type > & GetTable() const
Access to the underlying attribute table (const version).
TDrawingAttrRef< TColor > fColor
Fill color.
PRIMITIVE & Get(TDrawingAttrRef< PRIMITIVE > idx)
Get the value at index idx (non-const version).
TDrawingAttrAndUseCount(const PRIMITIVE &val)
Initialize with a value, setting use count to 1.
TLineAttrs(TDrawingOptsBaseNoDefault &opts, const std::string &name, const TColor &col, Width width)
Construct from the pad that holds our TDrawable, passing the configuration name of this line attribut...
TDrawingAttrRef< long long > fWidth
Line width.
bool IsFree() const
Whether the use count is 0 and this object has space for a new value.
void Update(TDrawingAttrRef< PRIMITIVE > idx, const PRIMITIVE &val)
Update an existing attribute entry in the table.
const PRIMITIVE & Get() const
Value access (const).
The Canvas class.
Definition: TCanvas.h:31
TDrawingAttrAndUseCount()
Default constructor: a default-constructed value that is unused.
std::vector< value_type > & GetTable()
Access to the underlying attribute table (non-const version).
The TCanvas keep track of TColors, integer and floating point attributes used by the drawing options...
Definition: TCanvas.hxx:32
PRIMITIVE & Get()
Value access (non-const).
TDrawingAttrRef(size_t idx)
Construct a reference given the index.
A color: Red|Green|Blue|Alpha, or a position in a TPalette.
Definition: TColor.hxx:27
TDrawingAttrRef< TColor > fColor
Line color.
TFillAttrs(TDrawingOptsBaseNoDefault &opts, const std::string &name, const TColor &col)
Construct from the pad that holds our TDrawable, passing the configuration name of this line attribut...
char name[80]
Definition: TGX11.cxx:109