Logo ROOT  
Reference Guide
RAttrValue.hxx
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
9#ifndef ROOT7_RAttrValue
10#define ROOT7_RAttrValue
11
12#include <ROOT/RAttrBase.hxx>
13
14namespace ROOT {
15namespace Experimental {
16
17/** \class RAttrValue
18\ingroup GpadROOT7
19\author Sergey Linev <s.linev@gsi.de>
20\date 2020-06-24
21\brief Template class to access single value from drawable or other attributes
22\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
23*/
24
25template<typename T>
26class RAttrValue : public RAttrBase {
27
28 template <typename Q, bool = std::is_enum<Q>::value>
30 Q Get(const RAttrMap::Value_t *value)
31 {
32 return (Q) RAttrMap::Value_t::GetValue<int>(value);
33 }
34 };
35
36 template <typename Q>
37 struct ValueExtractor<Q, false> {
38 Q Get(const RAttrMap::Value_t *value) {
39 return RAttrMap::Value_t::GetValue<Q>(value);
40 }
41 };
42
43protected:
44
45 T fDefault{}; ///<! default value
46
47 RAttrMap CollectDefaults() const override
48 {
49 return RAttrMap().AddValue(GetName(), fDefault);
50 }
51
52public:
53
55
56 RAttrValue(const T& dflt) : RAttrBase(""), fDefault(dflt) {}
57
58 RAttrValue(RDrawable *drawable, const char *name, const T &dflt = T()) : RAttrBase(drawable, name ? name : ""), fDefault(dflt) { }
59
60 RAttrValue(RAttrBase *parent, const char *name, const T &dflt = T()) : RAttrBase(parent, name ? name : ""), fDefault(dflt) { }
61
63 {
64 Set(src.Get());
65 fDefault = src.GetDefault();
66 }
67
68 T GetDefault() const { return fDefault; }
69
70 void Set(const T &v)
71 {
72 if (auto access = EnsureAttr(GetName()))
73 access.attr->AddValue(access.fullname, v);
74 }
75
76 T Get() const
77 {
78 if (auto v = AccessValue(GetName(), true))
79 return ValueExtractor<T>().Get(v.value);
80 return fDefault;
81 }
82
83 const char *GetName() const { return GetPrefix(); }
84
85 void Clear() override { ClearValue(GetName()); }
86
87 bool Has() const
88 {
89 if (auto v = AccessValue(GetName(), true)) {
90 auto res = RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,T>(v.value);
91 return res ? (res->Kind() != RAttrMap::kNoValue) : false;
92 }
93
94 return false;
95 }
96
97 RAttrValue &operator=(const T &v) { Set(v); return *this; }
98
99 RAttrValue &operator=(const RAttrValue &v) { Set(v.Get()); return *this; }
100
101 operator T() const { return Get(); }
102
103 friend bool operator==(const RAttrValue& lhs, const RAttrValue& rhs) { return lhs.Get() == rhs.Get(); }
104 friend bool operator!=(const RAttrValue& lhs, const RAttrValue& rhs) { return lhs.Get() != rhs.Get(); }
105
106 friend bool operator==(const RAttrValue& lhs, const T& rhs) { return lhs.Get() == rhs; }
107 friend bool operator!=(const RAttrValue& lhs, const T& rhs) { return lhs.Get() != rhs; }
108
109};
110
111} // namespace Experimental
112} // namespace ROOT
113
114#endif // ROOT7_RAttrValue
char name[80]
Definition: TGX11.cxx:110
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
const char * GetPrefix() const
Definition: RAttrBase.hxx:151
Rec_t EnsureAttr(const std::string &name)
Ensure attribute with give name exists - creates container for attributes if required.
Definition: RAttrBase.hxx:110
Template class to access single value from drawable or other attributes.
Definition: RAttrValue.hxx:26
friend bool operator!=(const RAttrValue &lhs, const RAttrValue &rhs)
Definition: RAttrValue.hxx:104
friend bool operator==(const RAttrValue &lhs, const RAttrValue &rhs)
Definition: RAttrValue.hxx:103
RAttrValue(RAttrBase *parent, const char *name, const T &dflt=T())
Definition: RAttrValue.hxx:60
friend bool operator!=(const RAttrValue &lhs, const T &rhs)
Definition: RAttrValue.hxx:107
RAttrValue(RDrawable *drawable, const char *name, const T &dflt=T())
Definition: RAttrValue.hxx:58
friend bool operator==(const RAttrValue &lhs, const T &rhs)
Definition: RAttrValue.hxx:106
RAttrMap CollectDefaults() const override
Definition: RAttrValue.hxx:47
const char * GetName() const
Definition: RAttrValue.hxx:83
RAttrValue & operator=(const RAttrValue &v)
Definition: RAttrValue.hxx:99
RAttrValue & operator=(const T &v)
Definition: RAttrValue.hxx:97
RAttrValue(const RAttrValue &src)
Definition: RAttrValue.hxx:62
Base class for drawable entities: objects that can be painted on a RPad.
Definition: RDrawable.hxx:108
static double Q[]
double T(double x)
Definition: ChebyshevPol.h:34
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Q Get(const RAttrMap::Value_t *value)
Definition: RAttrValue.hxx:30