Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAttrAggregation.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2021, 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
10
11#include <ROOT/RLogger.hxx>
12
13#include <utility>
14
15#include "TROOT.h"
16#include "TList.h"
17#include "TClass.h"
18#include "TDataMember.h"
19
20using namespace ROOT::Experimental;
21
22
23///////////////////////////////////////////////////////////////////////////////
24/// Return default values for attributes, empty for base class
25
27{
28 static RAttrMap empty;
29 return empty;
30}
31
32///////////////////////////////////////////////////////////////////////////////
33/// Collect all attributes in derived class
34/// Works only if such class has dictionary.
35/// In special cases one has to provide special implementation directly
36
38{
39 RAttrMap res;
40
41 const std::type_info &info = typeid(*this);
42 auto thisClass = TClass::GetClass(info);
43 auto baseClass = TClass::GetClass<RAttrBase>();
44 if (thisClass && baseClass) {
45 for (auto data_member: TRangeDynCast<TDataMember>(thisClass->GetListOfDataMembers())) {
46 TClass *cl = data_member && !data_member->IsBasic() && !data_member->IsEnum() ? gROOT->GetClass(data_member->GetFullTypeName()) : nullptr;
47 if (cl && cl->InheritsFrom(baseClass) && (cl->GetBaseClassOffset(baseClass) == 0))
48 res.AddDefaults(*((const RAttrBase *)((char*) this + data_member->GetOffset())));
49 }
50 } else {
51 R__LOG_ERROR(GPadLog()) << "Missing dictionary for " << info.name() << " class";
52 }
53
54 return res;
55}
56
57///////////////////////////////////////////////////////////////////////////////
58/// Copy attributes into target object
59
60void RAttrAggregation::CopyTo(RAttrAggregation &tgt, bool use_style) const
61{
62 for (const auto &entry : GetDefaults()) {
63 if (auto v = AccessValue(entry.first, use_style))
64 tgt.CopyValue(entry.first, *v.value);
65 }
66}
67
68///////////////////////////////////////////////////////////////////////////////
69/// Copy attributes from other object
70
71bool RAttrAggregation::CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type)
72{
73 if (check_type) {
74 const auto *dvalue = GetDefaults().Find(name);
75 if (!dvalue || !dvalue->CanConvertFrom(value.Kind()))
76 return false;
77 }
78
79 if (auto access = EnsureAttr(name)) {
80 access.attr->Add(access.fullname, value.Copy());
81 return true;
82 }
83
84 return false;
85}
86
87
88///////////////////////////////////////////////////////////////////////////////
89/// Check if provided value equal to attribute in the map
90
91bool RAttrAggregation::IsValueEqual(const std::string &name, const RAttrMap::Value_t &value, bool use_style) const
92{
93 if (auto v = AccessValue(name, use_style))
94 return v.value->CanConvertFrom(value.Kind()) && v.value->IsEqual(value);
95
96 return value.Kind() == RAttrMap::kNoValue;
97}
98
99
100///////////////////////////////////////////////////////////////////////////////
101/// Check if all values which are evaluated in this object are exactly the same as in tgt object
102
103bool RAttrAggregation::IsSame(const RAttrAggregation &tgt, bool use_style) const
104{
105 for (const auto &entry : GetDefaults()) {
106 // R__LOG_DEBUG(0, GPadLog()) << "Comparing entry " << entry.first;
107 if (auto v = AccessValue(entry.first, use_style))
108 if (!tgt.IsValueEqual(entry.first, *v.value, use_style))
109 return false;
110 }
111
112 return true;
113}
114
115///////////////////////////////////////////////////////////////////////////////
116/// Clear all respective values from drawable. Only defaults can be used
117
119{
120 for (const auto &entry : GetDefaults())
121 ClearValue(entry.first);
122}
123
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
Base class for attributes aggregations like lines or fill attributes.
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.
void Clear() override
Clear all respective values from drawable. Only defaults can be used.
bool IsSame(const RAttrAggregation &src, bool use_style=true) const
Check if all values which are evaluated in this object are exactly the same as in tgt object.
bool CopyValue(const std::string &name, const RAttrMap::Value_t &value, bool check_type=true)
Copy attributes from other object.
virtual const RAttrMap & GetDefaults() const
Return default values for attributes, empty for base class.
RAttrMap CollectDefaults() const override
Collect all attributes in derived class Works only if such class has dictionary.
void CopyTo(RAttrAggregation &tgt, bool use_style=true) const
Copy attributes into target object.
Base class for all attributes, used with RDrawable.
Definition RAttrBase.hxx:31
void ClearValue(const std::string &name)
Clear value if any with specified name.
Definition RAttrBase.cxx:54
const Val_t AccessValue(const std::string &name, bool use_style=true) const
Search value with given name in attributes.
Definition RAttrBase.hxx:93
Rec_t EnsureAttr(const std::string &name)
Ensure attribute with give name exists - creates container for attributes if required.
const Value_t * Find(const std::string &name) const
Definition RAttrMap.hxx:186
RAttrMap & AddDefaults(const RAttrBase &vis)
Add defaults values form sub attribute.
Definition RAttrMap.cxx:49
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4874
Int_t GetBaseClassOffset(const TClass *toBase, void *address=nullptr, bool isDerivedObject=true)
Definition TClass.cxx:2791
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:2968
RLogChannel & GPadLog()
Log channel for GPad diagnostics.
Definition RAttrBase.cxx:17