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