Logo ROOT  
Reference Guide
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
16///////////////////////////////////////////////////////////////////////////////
17/// Return default values for attributes, empty for base class
18
20{
21 static RAttrMap empty;
22 return empty;
23}
24
25///////////////////////////////////////////////////////////////////////////////
26/// Copy attributes from other object
27
28bool ROOT::Experimental::RAttrBase::CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type)
29{
30 if (check_type) {
31 const auto *dvalue = GetDefaults().Find(name);
32 if (!dvalue || !dvalue->CanConvertFrom(value.Kind()))
33 return false;
34 }
35
36 if (auto access = EnsureAttr(name)) {
37 access.attr->Add(access.fullname, value.Copy());
38 return true;
39 }
40
41 return false;
42}
43
44///////////////////////////////////////////////////////////////////////////////
45/// Check if provided value equal to attribute in the map
46
47bool ROOT::Experimental::RAttrBase::IsValueEqual(const std::string &name, const RAttrMap::Value_t &value, bool use_style) const
48{
49 if (auto v = AccessValue(name, use_style))
50 return v.value->CanConvertFrom(value.Kind()) && v.value->IsEqual(value);
51
52 return value.Kind() == RAttrMap::kNoValue;
53}
54
55///////////////////////////////////////////////////////////////////////////////
56/// Copy attributes into target object
57
58void ROOT::Experimental::RAttrBase::CopyTo(RAttrBase &tgt, bool use_style) const
59{
60 for (const auto &entry : GetDefaults()) {
61 if (auto v = AccessValue(entry.first, use_style))
62 tgt.CopyValue(entry.first, *v.value);
63 }
64}
65
66///////////////////////////////////////////////////////////////////////////////
67/// Move all fields into target object
68
70{
71 std::swap(fOwnAttr, tgt.fOwnAttr);
72 std::swap(fPrefix, tgt.fPrefix);
73 std::swap(fDrawable, tgt.fDrawable);
74 std::swap(fParent, tgt.fParent);
75}
76
77///////////////////////////////////////////////////////////////////////////////
78/// Check if all values which are evaluated in this object are exactly the same as in tgt object
79
80bool ROOT::Experimental::RAttrBase::IsSame(const RAttrBase &tgt, bool use_style) const
81{
82 for (const auto &entry : GetDefaults()) {
83 if (auto v = AccessValue(entry.first, use_style))
84 if (!tgt.IsValueEqual(entry.first, *v.value, use_style)) return false;
85 }
86 return true;
87}
88
89///////////////////////////////////////////////////////////////////////////////
90/// Return value from attributes container - no style or defaults are used
91
92void ROOT::Experimental::RAttrBase::AssignDrawable(RDrawable *drawable, const std::string &prefix)
93{
94 fDrawable = drawable;
95 fOwnAttr.reset();
96 fPrefix = prefix;
97 fParent = nullptr;
98}
99
100///////////////////////////////////////////////////////////////////////////////
101/// Assign parent object for this RAttrBase
102
103void ROOT::Experimental::RAttrBase::AssignParent(RAttrBase *parent, const std::string &prefix)
104{
105 fDrawable = nullptr;
106 fOwnAttr.reset();
107 fPrefix = prefix;
108 fParent = parent;
109}
110
111///////////////////////////////////////////////////////////////////////////////
112/// Clear value if any with specified name
113
115{
116 if (auto access = AccessAttr(name))
117 access.attr->Clear(access.fullname);
118}
119
120///////////////////////////////////////////////////////////////////////////////
121/// Set <NoValue> for attribute. Ensure that value can not be configured via style - defaults will be used
122/// Equivalent to css syntax { attrname:; }
123
125{
126 if (auto access = AccessAttr(name))
127 access.attr->AddNoValue(access.fullname);
128}
129
130///////////////////////////////////////////////////////////////////////////////
131/// Set boolean value
132
133void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, bool value)
134{
135 if (auto access = EnsureAttr(name))
136 access.attr->AddBool(access.fullname, value);
137}
138
139///////////////////////////////////////////////////////////////////////////////
140/// Set integer value
141
142void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, int value)
143{
144 if (auto access = EnsureAttr(name))
145 access.attr->AddInt(access.fullname, value);
146}
147
148///////////////////////////////////////////////////////////////////////////////
149/// Set double value
150
151void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, double value)
152{
153 if (auto access = EnsureAttr(name))
154 access.attr->AddDouble(access.fullname, value);
155}
156
157///////////////////////////////////////////////////////////////////////////////
158/// Set string value
159
160void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, const std::string &value)
161{
162 if (auto access = EnsureAttr(name))
163 access.attr->AddString(access.fullname, value);
164}
165
166///////////////////////////////////////////////////////////////////////////////
167/// Set PadLength value
168
169void ROOT::Experimental::RAttrBase::SetValue(const std::string &name, const RPadLength &value)
170{
171 if (value.Empty())
172 ClearValue(name);
173 else
174 SetValue(name, value.AsString());
175}
176
177///////////////////////////////////////////////////////////////////////////////
178/// Clear all respective values from drawable. Only defaults can be used
179
181{
182 for (const auto &entry : GetDefaults())
183 ClearValue(entry.first);
184}
char name[80]
Definition: TGX11.cxx:109
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:27
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
std::unique_ptr< RAttrMap > fOwnAttr
own instance when deep copy is created, persistent for RColor and similar classes
Definition: RAttrBase.hxx:32
void Clear()
Clear all respective values from drawable. Only defaults can be used.
Definition: RAttrBase.cxx:180
void SetNoValue(const std::string &name)
Set <NoValue> for attribute.
Definition: RAttrBase.cxx:124
bool CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type=true)
Copy attributes from other object.
Definition: RAttrBase.cxx:28
void MoveTo(RAttrBase &tgt)
Move all fields into target object.
Definition: RAttrBase.cxx:69
RDrawable * fDrawable
! drawable used to store attributes
Definition: RAttrBase.hxx:31
void ClearValue(const std::string &name)
Clear value if any with specified name.
Definition: RAttrBase.cxx:114
void AssignDrawable(RDrawable *drawable, const std::string &prefix)
Return value from attributes container - no style or defaults are used.
Definition: RAttrBase.cxx:92
RAttrBase * fParent
! parent attributes, prefix applied to it
Definition: RAttrBase.hxx:34
void SetValue(const std::string &name, bool value)
Set boolean value.
Definition: RAttrBase.cxx:133
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
void AssignParent(RAttrBase *parent, const std::string &prefix)
Assign parent object for this RAttrBase.
Definition: RAttrBase.cxx:103
std::string fPrefix
! name prefix for all attributes values
Definition: RAttrBase.hxx:33
virtual const RAttrMap & GetDefaults() const
Return default values for attributes, empty for base class.
Definition: RAttrBase.cxx:19
void CopyTo(RAttrBase &tgt, bool use_style=true) const
Copy attributes into target object.
Definition: RAttrBase.cxx:58
virtual EValuesKind Kind() const =0
virtual std::unique_ptr< Value_t > Copy() const =0
Base class for drawable entities: objects that can be painted on a RPad.
Definition: RDrawable.hxx:102
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
Definition: RPadLength.cxx:19
void swap(RDirectoryEntry &e1, RDirectoryEntry &e2) noexcept