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