Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAttrBase.cxx
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#include <ROOT/RAttrBase.hxx>
10
11#include <ROOT/RLogger.hxx>
12
13#include <utility>
14
15#include "TList.h"
16#include "TClass.h"
17#include "TDataMember.h"
18
20 static RLogChannel sLog("ROOT.GPad");
21 return sLog;
22}
23
24///////////////////////////////////////////////////////////////////////////////
25/// Return default values for attributes, empty for base class
26
28{
29 static RAttrMap empty;
30 return empty;
31}
32
33///////////////////////////////////////////////////////////////////////////////
34/// Copy attributes from other object
35
36bool ROOT::Experimental::RAttrBase::CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type)
37{
38 if (check_type) {
39 const auto *dvalue = GetDefaults().Find(name);
40 if (!dvalue || !dvalue->CanConvertFrom(value.Kind()))
41 return false;
42 }
43
44 if (auto access = EnsureAttr(name)) {
45 access.attr->Add(access.fullname, value.Copy());
46 return true;
47 }
48
49 return false;
50}
51
52///////////////////////////////////////////////////////////////////////////////
53/// Check if provided value equal to attribute in the map
54
55bool ROOT::Experimental::RAttrBase::IsValueEqual(const std::string &name, const RAttrMap::Value_t &value, bool use_style) const
56{
57 if (auto v = AccessValue(name, use_style))
58 return v.value->CanConvertFrom(value.Kind()) && v.value->IsEqual(value);
59
60 return value.Kind() == RAttrMap::kNoValue;
61}
62
63///////////////////////////////////////////////////////////////////////////////
64/// Copy attributes into target object
65
66void ROOT::Experimental::RAttrBase::CopyTo(RAttrBase &tgt, bool use_style) const
67{
68 for (const auto &entry : GetDefaults()) {
69 if (auto v = AccessValue(entry.first, use_style))
70 tgt.CopyValue(entry.first, *v.value);
71 }
72}
73
74///////////////////////////////////////////////////////////////////////////////
75/// Move all fields into target object
76
78{
79 std::swap(fOwnAttr, tgt.fOwnAttr);
80 std::swap(fPrefix, tgt.fPrefix);
81 std::swap(fDrawable, tgt.fDrawable);
82 std::swap(fParent, tgt.fParent);
83}
84
85///////////////////////////////////////////////////////////////////////////////
86/// Check if all values which are evaluated in this object are exactly the same as in tgt object
87
88bool ROOT::Experimental::RAttrBase::IsSame(const RAttrBase &tgt, bool use_style) const
89{
90 for (const auto &entry : GetDefaults()) {
91 if (auto v = AccessValue(entry.first, use_style))
92 if (!tgt.IsValueEqual(entry.first, *v.value, use_style)) return false;
93 }
94 return true;
95}
96
97///////////////////////////////////////////////////////////////////////////////
98/// Return value from attributes container - no style or defaults are used
99
100void ROOT::Experimental::RAttrBase::AssignDrawable(RDrawable *drawable, const std::string &prefix)
101{
102 fDrawable = drawable;
103 fOwnAttr.reset();
104 fPrefix = prefix;
105 if (!IsValue() && !fPrefix.empty()) fPrefix.append("_"); // naming convention
106 fParent = nullptr;
107}
108
109///////////////////////////////////////////////////////////////////////////////
110/// Assign parent object for this RAttrBase
111
112void ROOT::Experimental::RAttrBase::AssignParent(RAttrBase *parent, const std::string &prefix)
113{
114 fDrawable = nullptr;
115 fOwnAttr.reset();
116 fPrefix = prefix;
117 if (!IsValue() && !fPrefix.empty()) fPrefix.append("_"); // naming convention
118 fParent = parent;
119}
120
121///////////////////////////////////////////////////////////////////////////////
122/// Clear value if any with specified name
123
125{
126 if (auto access = AccessAttr(name))
127 access.attr->Clear(access.fullname);
128}
129
130///////////////////////////////////////////////////////////////////////////////
131/// Set <NoValue> for attribute. Ensure that value can not be configured via style - defaults will be used
132/// Equivalent to css syntax { attrname:; }
133
135{
136 if (auto access = AccessAttr(name))
137 access.attr->AddNoValue(access.fullname);
138}
139
140///////////////////////////////////////////////////////////////////////////////
141/// Set boolean value
142
143void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, bool value)
144{
145 if (auto access = EnsureAttr(name))
146 access.attr->AddBool(access.fullname, value);
147}
148
149///////////////////////////////////////////////////////////////////////////////
150/// Set integer value
151
152void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, int value)
153{
154 if (auto access = EnsureAttr(name))
155 access.attr->AddInt(access.fullname, value);
156}
157
158///////////////////////////////////////////////////////////////////////////////
159/// Set double value
160
161void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, double value)
162{
163 if (auto access = EnsureAttr(name))
164 access.attr->AddDouble(access.fullname, value);
165}
166
167///////////////////////////////////////////////////////////////////////////////
168/// Set string value
169
170void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, const std::string &value)
171{
172 if (auto access = EnsureAttr(name))
173 access.attr->AddString(access.fullname, value);
174}
175
176///////////////////////////////////////////////////////////////////////////////
177/// Set PadLength value
178
179void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, const RPadLength &value)
180{
181 if (value.Empty())
182 ClearValue(name);
183 else
184 SetValue(name, value.AsString());
185}
186
187///////////////////////////////////////////////////////////////////////////////
188/// Clear all respective values from drawable. Only defaults can be used
189
191{
192 for (const auto &entry : GetDefaults())
193 ClearValue(entry.first);
194}
195
196
197///////////////////////////////////////////////////////////////////////////////
198/// Collect all attributes in derived class
199/// Works only if such class has dictionary.
200/// In special cases one has to provide implementation - see RAttrColor::CollectDefaults() example
201
203{
205
206 const std::type_info &info = typeid(*this);
207 auto thisClass = TClass::GetClass(info);
208 auto baseClass = TClass::GetClass<ROOT::Experimental::RAttrBase>();
209 if (thisClass && baseClass) {
210 for (auto data_member: TRangeDynCast<TDataMember>(thisClass->GetListOfDataMembers())) {
211 if (data_member && data_member->GetClass() && data_member->GetClass()->InheritsFrom(baseClass) &&
212 (data_member->GetClass()->GetBaseClassOffset(baseClass) == 0)) {
213 res.AddDefaults(*((const RAttrBase *)((char*) this + data_member->GetOffset())));
214 }
215 }
216 } else {
217 R__LOG_ERROR(GPadLog()) << "Missing dictionary for " << info.name() << " class, implement CollectDefaults() like in RAttrColor";
218 }
219
220 return res;
221}
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
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
std::unique_ptr< RAttrMap > fOwnAttr
own instance when deep copy is created, persistent for RColor and similar classes
Definition RAttrBase.hxx:36
void Clear()
Clear all respective values from drawable. Only defaults can be used.
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.
RAttrBase * fParent
! parent attributes, prefix applied to it
Definition RAttrBase.hxx:38
void SetValue(const std::string &name, bool value)
Set boolean value.
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
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
virtual const RAttrMap & GetDefaults() const
Return default values for attributes, empty for base class.
Definition RAttrBase.cxx:27
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
virtual EValuesKind Kind() const =0
virtual std::unique_ptr< Value_t > Copy() const =0
RAttrMap & AddDefaults(const RAttrBase &vis)
Add defaults values form sub attribute.
Definition RAttrMap.cxx:45
Base class for drawable entities: objects that can be painted on a RPad.
A log configuration for a channel, e.g.
Definition RLogger.hxx:101
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2957
TRangeDynCast is an adaptater class that allows the typed iteration through a TCollection.
RLogChannel & GPadLog()
Log channel for GPad diagnostics.
Definition RAttrBase.cxx:19