Logo ROOT   master
Reference Guide
RAttrBase.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2019, 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_RAttrBase
10 #define ROOT7_RAttrBase
11 
12 #include <ROOT/RAttrMap.hxx>
13 #include <ROOT/RStyle.hxx>
14 #include <ROOT/RDrawable.hxx>
15 
16 namespace ROOT {
17 namespace Experimental {
18 
19 /** \class RAttrBase
20 \ingroup GpadROOT7
21 \author Sergey Linev <s.linev@gsi.de>
22 \date 2019-09-17
23 \brief Base class for all attributes, used with RDrawable
24 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
25 */
26 
27 class RAttrBase {
28 
29  friend class RAttrMap;
30 
31  RDrawable *fDrawable{nullptr}; ///<! drawable used to store attributes
32  std::unique_ptr<RAttrMap> fOwnAttr; ///< own instance when deep copy is created, persistent for RColor and similar classes
33  std::string fPrefix; ///<! name prefix for all attributes values
34  RAttrBase *fParent{nullptr}; ///<! parent attributes, prefix applied to it
35 
36 protected:
37 
38  virtual const RAttrMap &GetDefaults() const;
39 
40  bool CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type = true);
41 
42  bool IsValueEqual(const std::string &name, const RAttrMap::Value_t &value, bool use_style = false) const;
43 
44  ///////////////////////////////////////////////////////////////////////////////
45 
46  void AssignDrawable(RDrawable *drawable, const std::string &prefix);
47 
48  void AssignParent(RAttrBase *parent, const std::string &prefix);
49 
50  struct Rec_t {
51  RAttrMap *attr{nullptr};
52  std::string fullname;
53  RDrawable *drawable{nullptr};
54  operator bool() const { return !!attr; }
55  };
56 
57  /// Find attributes container and full-qualified name for value
58  const Rec_t AccessAttr(const std::string &name) const
59  {
60  const RAttrBase *prnt = this;
61  std::string fullname = name;
62  while (prnt) {
63  fullname.insert(0, prnt->fPrefix); // fullname = prnt->fPrefix + fullname
64  if (prnt->fDrawable)
65  return {&(prnt->fDrawable->fAttr), fullname, prnt->fDrawable};
66  if (prnt->fOwnAttr)
67  return {prnt->fOwnAttr.get(), fullname, nullptr};
68  prnt = prnt->fParent;
69  }
70  return {nullptr, fullname, nullptr};
71  }
72 
73  struct Val_t {
74  const RAttrMap::Value_t *value{nullptr};
75  std::shared_ptr<RStyle> style;
76  operator bool() const { return !!value; }
77  };
78 
79  /** Search value with given name in attributes */
80  const Val_t AccessValue(const std::string &name, bool use_style = true) const
81  {
82  if (auto access = AccessAttr(name)) {
83  if (auto rec = access.attr->Find(access.fullname))
84  return {rec, nullptr};
85  if (access.drawable && use_style)
86  if (auto observe = access.drawable->fStyle.lock()) {
87  if (auto rec = observe->Eval(access.fullname, *access.drawable))
88  return {rec, observe};
89  }
90  }
91 
92  return {nullptr, nullptr};
93  }
94 
95  /// Ensure attribute with give name exists - creates container for attributes if required
96 
97  Rec_t EnsureAttr(const std::string &name)
98  {
99  auto prnt = this;
100  std::string fullname = name;
101  while (prnt) {
102  fullname.insert(0, prnt->fPrefix); // fullname = prnt->fPrefix + fullname
103  if (prnt->fDrawable)
104  return {&(prnt->fDrawable->fAttr), fullname, prnt->fDrawable};
105  if (!prnt->fParent && !prnt->fOwnAttr)
106  prnt->fOwnAttr = std::make_unique<RAttrMap>();
107  if (prnt->fOwnAttr)
108  return {prnt->fOwnAttr.get(), fullname, nullptr};
109  prnt = prnt->fParent;
110  }
111  return {nullptr, fullname, nullptr};
112  }
113 
114  /// Evaluate attribute value
115 
116  template <typename RET_TYPE,typename MATCH_TYPE = void>
117  auto Eval(const std::string &name, bool use_dflts = true) const
118  {
119  if (auto v = AccessValue(name, true))
120  return RAttrMap::Value_t::GetValue<RET_TYPE,MATCH_TYPE>(v.value);
121 
122  const RAttrMap::Value_t *rec = nullptr;
123 
124  if (use_dflts)
125  rec = GetDefaults().Find(name);
126 
127  return RAttrMap::Value_t::GetValue<RET_TYPE,MATCH_TYPE>(rec);
128  }
129 
130  void CopyTo(RAttrBase &tgt, bool use_style = true) const;
131 
132  void MoveTo(RAttrBase &tgt);
133 
134  bool IsSame(const RAttrBase &src, bool use_style = true) const;
135 
136  RAttrBase(RDrawable *drawable, const std::string &prefix) { AssignDrawable(drawable, prefix); }
137 
138  RAttrBase(RAttrBase *parent, const std::string &prefix) { AssignParent(parent, prefix); }
139 
140  RAttrBase(const RAttrBase &src) { src.CopyTo(*this); }
141 
143  {
144  Clear();
145  src.CopyTo(*this);
146  return *this;
147  }
148 
149  void SetNoValue(const std::string &name);
150  void SetValue(const std::string &name, bool value);
151  void SetValue(const std::string &name, double value);
152  void SetValue(const std::string &name, int value);
153  void SetValue(const std::string &name, const std::string &value);
154  void SetValue(const std::string &name, const RPadLength &value);
155 
156  const std::string &GetPrefix() const { return fPrefix; }
157 
158  void ClearValue(const std::string &name);
159 
160  void Clear();
161 
162  template <typename T = void>
163  bool HasValue(const std::string &name, bool check_defaults = false) const
164  {
165  auto res = Eval<const RAttrMap::Value_t *, T>(name, check_defaults);
166  return res ? (res->Kind() != RAttrMap::kNoValue) : false;
167  }
168 
169  template <typename T>
170  T GetValue(const std::string &name) const
171  {
172  return Eval<T>(name);
173  }
174 
175 public:
176  RAttrBase() = default;
177 
178  virtual ~RAttrBase() = default;
179 
180  friend bool operator==(const RAttrBase& lhs, const RAttrBase& rhs) { return lhs.IsSame(rhs) && rhs.IsSame(lhs); }
181  friend bool operator!=(const RAttrBase& lhs, const RAttrBase& rhs) { return !lhs.IsSame(rhs) || !rhs.IsSame(lhs); }
182 };
183 
184 
185 } // namespace Experimental
186 } // namespace ROOT
187 
188 #define R__ATTR_CLASS(ClassName,dflt_prefix,dflt_values) \
189 protected: \
190 const RAttrMap &GetDefaults() const override \
191 { \
192  static auto dflts = RAttrMap().dflt_values; \
193  return dflts; \
194 } \
195 public: \
196  ClassName() = default; \
197  ClassName(RDrawable *drawable, const std::string &prefix = dflt_prefix) { AssignDrawable(drawable, prefix); } \
198  ClassName(RAttrBase *parent, const std::string &prefix = dflt_prefix) { AssignParent(parent, prefix); } \
199  ClassName(const ClassName &src) : ClassName() { src.CopyTo(*this); } \
200  ClassName(ClassName &&src) : ClassName() { src.MoveTo(*this); } \
201  ClassName &operator=(ClassName &&src) { src.MoveTo(*this); return *this; } \
202  ClassName &operator=(const ClassName &src) { Clear(); src.CopyTo(*this); return *this; } \
203 
204 #endif
RAttrMap fAttr
attributes values
Definition: RDrawable.hxx:164
Base class for drawable entities: objects that can be painted on a RPad.
Definition: RDrawable.hxx:102
virtual ~RAttrBase()=default
Returns the available number of logical cores.
Definition: StringConv.hxx:21
bool CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type=true)
Copy attributes from other object.
Definition: RAttrBase.cxx:28
friend bool operator==(const RAttrBase &lhs, const RAttrBase &rhs)
Definition: RAttrBase.hxx:180
std::shared_ptr< RStyle > style
Definition: RAttrBase.hxx:75
double T(double x)
Definition: ChebyshevPol.h:34
void Clear()
Clear all respective values from drawable. Only defaults can be used.
Definition: RAttrBase.cxx:180
const RAttrMap::Value_t * value
Definition: RAttrBase.hxx:74
RDrawable * fDrawable
! drawable used to store attributes
Definition: RAttrBase.hxx:31
void CopyTo(RAttrBase &tgt, bool use_style=true) const
Copy attributes into target object.
Definition: RAttrBase.cxx:58
friend bool operator!=(const RAttrBase &lhs, const RAttrBase &rhs)
Definition: RAttrBase.hxx:181
RAttrBase(RDrawable *drawable, const std::string &prefix)
Definition: RAttrBase.hxx:136
RAttrBase(RAttrBase *parent, const std::string &prefix)
Definition: RAttrBase.hxx:138
virtual const RAttrMap & GetDefaults() const
Return default values for attributes, empty for base class.
Definition: RAttrBase.cxx:19
void SetValue(const std::string &name, bool value)
Set boolean value.
Definition: RAttrBase.cxx:133
bool HasValue(const std::string &name, bool check_defaults=false) const
Definition: RAttrBase.hxx:163
Rec_t EnsureAttr(const std::string &name)
Ensure attribute with give name exists - creates container for attributes if required.
Definition: RAttrBase.hxx:97
std::string fPrefix
! name prefix for all attributes values
Definition: RAttrBase.hxx:33
const std::string & GetPrefix() const
Definition: RAttrBase.hxx:156
std::unique_ptr< RAttrMap > fOwnAttr
own instance when deep copy is created, persistent for RColor and similar classes ...
Definition: RAttrBase.hxx:32
void MoveTo(RAttrBase &tgt)
Move all fields into target object.
Definition: RAttrBase.cxx:69
void SetNoValue(const std::string &name)
Set <NoValue> for attribute.
Definition: RAttrBase.cxx:124
auto Eval(const std::string &name, bool use_dflts=true) const
Evaluate attribute value.
Definition: RAttrBase.hxx:117
void ClearValue(const std::string &name)
Clear value if any with specified name.
Definition: RAttrBase.cxx:114
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:27
void AssignParent(RAttrBase *parent, const std::string &prefix)
Assign parent object for this RAttrBase.
Definition: RAttrBase.cxx:103
T GetValue(const std::string &name) const
Definition: RAttrBase.hxx:170
void AssignDrawable(RDrawable *drawable, const std::string &prefix)
Return value from attributes container - no style or defaults are used.
Definition: RAttrBase.cxx:92
RAttrBase & operator=(const RAttrBase &src)
Definition: RAttrBase.hxx:142
bool IsValueEqual(const std::string &name, const RAttrMap::Value_t &value, bool use_style=false) const
Check if provided value equal to attribute in the map.
Definition: RAttrBase.cxx:47
bool IsSame(const RAttrBase &src, bool use_style=true) const
Check if all values which are evaluated in this object are exactly the same as in tgt object...
Definition: RAttrBase.cxx:80
RAttrBase * fParent
! parent attributes, prefix applied to it
Definition: RAttrBase.hxx:34
RAttrBase(const RAttrBase &src)
Definition: RAttrBase.hxx:140
const Rec_t AccessAttr(const std::string &name) const
Find attributes container and full-qualified name for value.
Definition: RAttrBase.hxx:58
const Val_t AccessValue(const std::string &name, bool use_style=true) const
Search value with given name in attributes.
Definition: RAttrBase.hxx:80
const Value_t * Find(const std::string &name) const
Definition: RAttrMap.hxx:164
char name[80]
Definition: TGX11.cxx:109