Logo ROOT  
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  virtual RAttrMap CollectDefaults() const;
176 
177  virtual bool IsValue() const { return false; }
178 
179 public:
180  RAttrBase() = default;
181 
182  virtual ~RAttrBase() = default;
183 
184  friend bool operator==(const RAttrBase& lhs, const RAttrBase& rhs) { return lhs.IsSame(rhs) && rhs.IsSame(lhs); }
185  friend bool operator!=(const RAttrBase& lhs, const RAttrBase& rhs) { return !lhs.IsSame(rhs) || !rhs.IsSame(lhs); }
186 
187 };
188 
189 
190 } // namespace Experimental
191 } // namespace ROOT
192 
193 #define R__ATTR_CLASS(ClassName,dflt_prefix) \
194 protected: \
195 const RAttrMap &GetDefaults() const override \
196 { \
197  static auto dflts = CollectDefaults(); \
198  return dflts; \
199 } \
200 public: \
201  ClassName() = default; \
202  ClassName(RDrawable *drawable, const std::string &prefix = dflt_prefix) { AssignDrawable(drawable, prefix); } \
203  ClassName(RAttrBase *parent, const std::string &prefix = dflt_prefix) { AssignParent(parent, prefix); } \
204  ClassName(const ClassName &src) : ClassName() { src.CopyTo(*this); } \
205  ClassName(ClassName &&src) : ClassName() { src.MoveTo(*this); } \
206  ClassName &operator=(ClassName &&src) { src.MoveTo(*this); return *this; } \
207  ClassName &operator=(const ClassName &src) { Clear(); src.CopyTo(*this); return *this; } \
208 
209 #endif
ROOT::Experimental::RAttrBase::fDrawable
RDrawable * fDrawable
! drawable used to store attributes
Definition: RAttrBase.hxx:31
ROOT::Experimental::RAttrBase::CopyTo
void CopyTo(RAttrBase &tgt, bool use_style=true) const
Copy attributes into target object.
Definition: RAttrBase.cxx:61
ROOT::Experimental::RAttrBase::RAttrBase
RAttrBase(RDrawable *drawable, const std::string &prefix)
Definition: RAttrBase.hxx:136
ROOT::Experimental::RAttrBase::AccessAttr
const Rec_t AccessAttr(const std::string &name) const
Find attributes container and full-qualified name for value.
Definition: RAttrBase.hxx:58
ROOT::Experimental::RAttrBase::fParent
RAttrBase * fParent
! parent attributes, prefix applied to it
Definition: RAttrBase.hxx:34
ROOT::Experimental::RAttrBase::EnsureAttr
Rec_t EnsureAttr(const std::string &name)
Ensure attribute with give name exists - creates container for attributes if required.
Definition: RAttrBase.hxx:97
ROOT::Experimental::RAttrMap::Value_t
Definition: RAttrMap.hxx:42
ROOT::Experimental::RAttrMap
Definition: RAttrMap.hxx:33
ROOT::Experimental::RAttrBase
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:27
ROOT::Experimental::RAttrBase::MoveTo
void MoveTo(RAttrBase &tgt)
Move all fields into target object.
Definition: RAttrBase.cxx:72
ROOT::Experimental::RAttrBase::fOwnAttr
std::unique_ptr< RAttrMap > fOwnAttr
own instance when deep copy is created, persistent for RColor and similar classes
Definition: RAttrBase.hxx:32
ROOT::Experimental::RAttrBase::fPrefix
std::string fPrefix
! name prefix for all attributes values
Definition: RAttrBase.hxx:33
ROOT::Experimental::RAttrBase::~RAttrBase
virtual ~RAttrBase()=default
ROOT::Experimental::RDrawable::fAttr
RAttrMap fAttr
attributes values
Definition: RDrawable.hxx:172
ROOT::Experimental::RAttrMap::Find
const Value_t * Find(const std::string &name) const
Definition: RAttrMap.hxx:177
ROOT::Experimental::RAttrBase::Clear
void Clear()
Clear all respective values from drawable. Only defaults can be used.
Definition: RAttrBase.cxx:185
v
@ v
Definition: rootcling_impl.cxx:3635
bool
ROOT::Experimental::RAttrBase::RAttrBase
RAttrBase(const RAttrBase &src)
Definition: RAttrBase.hxx:140
ROOT::Experimental::RAttrBase::ClearValue
void ClearValue(const std::string &name)
Clear value if any with specified name.
Definition: RAttrBase.cxx:119
ROOT::Experimental::RAttrBase::IsValue
virtual bool IsValue() const
Definition: RAttrBase.hxx:177
ROOT::Experimental::RPadLength
A length in RPad.
Definition: RPadLength.hxx:31
ROOT::Experimental::RAttrBase::Rec_t::drawable
RDrawable * drawable
Definition: RAttrBase.hxx:53
ROOT::Experimental::RAttrBase::GetValue
T GetValue(const std::string &name) const
Definition: RAttrBase.hxx:170
ROOT::Experimental::RAttrBase::operator=
RAttrBase & operator=(const RAttrBase &src)
Definition: RAttrBase.hxx:142
ROOT::Experimental::RAttrBase::SetValue
void SetValue(const std::string &name, bool value)
Set boolean value.
Definition: RAttrBase.cxx:138
ROOT::Experimental::RAttrBase::Rec_t::attr
RAttrMap * attr
Definition: RAttrBase.hxx:51
ROOT::Experimental::RAttrBase::HasValue
bool HasValue(const std::string &name, bool check_defaults=false) const
Definition: RAttrBase.hxx:163
ROOT::Experimental::RAttrMap::kNoValue
@ kNoValue
Definition: RAttrMap.hxx:40
RAttrMap.hxx
ROOT::Experimental::RAttrBase::CopyValue
bool CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type=true)
Copy attributes from other object.
Definition: RAttrBase.cxx:31
ROOT::Experimental::RAttrBase::Rec_t::fullname
std::string fullname
Definition: RAttrBase.hxx:52
ROOT::Experimental::RAttrBase::operator==
friend bool operator==(const RAttrBase &lhs, const RAttrBase &rhs)
Definition: RAttrBase.hxx:184
ROOT::Experimental::RAttrBase::GetDefaults
virtual const RAttrMap & GetDefaults() const
Return default values for attributes, empty for base class.
Definition: RAttrBase.cxx:22
ROOT::Experimental::RAttrBase::Eval
auto Eval(const std::string &name, bool use_dflts=true) const
Evaluate attribute value.
Definition: RAttrBase.hxx:117
ROOT::Experimental::RAttrBase::AssignDrawable
void AssignDrawable(RDrawable *drawable, const std::string &prefix)
Return value from attributes container - no style or defaults are used.
Definition: RAttrBase.cxx:95
ROOT::Experimental::RAttrBase::RAttrBase
RAttrBase(RAttrBase *parent, const std::string &prefix)
Definition: RAttrBase.hxx:138
ROOT::Experimental::RAttrBase::CollectDefaults
virtual RAttrMap CollectDefaults() const
Collect all attributes in derived class Works only if such class has dictionary.
Definition: RAttrBase.cxx:197
RDrawable.hxx
ROOT::Experimental::RDrawable
Base class for drawable entities: objects that can be painted on a RPad.
Definition: RDrawable.hxx:109
ROOT::Experimental::RAttrBase::Val_t
Definition: RAttrBase.hxx:73
ROOT::Experimental::RAttrBase::SetNoValue
void SetNoValue(const std::string &name)
Set <NoValue> for attribute.
Definition: RAttrBase.cxx:129
ROOT::Experimental::RAttrBase::Val_t::style
std::shared_ptr< RStyle > style
Definition: RAttrBase.hxx:75
ROOT::Experimental::RAttrBase::operator!=
friend bool operator!=(const RAttrBase &lhs, const RAttrBase &rhs)
Definition: RAttrBase.hxx:185
ROOT::Experimental::RAttrBase::Rec_t
Definition: RAttrBase.hxx:50
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
ROOT::Experimental::RAttrBase::AccessValue
const Val_t AccessValue(const std::string &name, bool use_style=true) const
Search value with given name in attributes.
Definition: RAttrBase.hxx:80
ROOT::Experimental::RAttrBase::GetPrefix
const std::string & GetPrefix() const
Definition: RAttrBase.hxx:156
ROOT::Experimental::RAttrBase::RAttrBase
RAttrBase()=default
ROOT::Experimental::RAttrBase::IsValueEqual
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:50
ROOT::Experimental::RAttrBase::IsSame
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:83
ROOT::Experimental::RAttrBase::AssignParent
void AssignParent(RAttrBase *parent, const std::string &prefix)
Assign parent object for this RAttrBase.
Definition: RAttrBase.cxx:107
ROOT::Experimental::RAttrBase::Val_t::value
const RAttrMap::Value_t * value
Definition: RAttrBase.hxx:74
RStyle.hxx
ROOT
VSD Structures.
Definition: StringConv.hxx:21