Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAttrMap.hxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2020, 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_RAttrMap
10#define ROOT7_RAttrMap
11
12#include <memory>
13#include <string>
14#include <type_traits>
15#include <unordered_map>
16
17#include <ROOT/RPadLength.hxx>
18#include <ROOT/RColor.hxx>
19
20namespace ROOT {
21namespace Experimental {
22
23class RAttrBase;
24class RStyle;
25
26/** \class RAttrMap
27\ingroup GpadROOT7
28\authors Axel Naumann <axel@cern.ch> Sergey Linev <s.linev@gsi.de>
29\date 2017-09-26
30\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
31*/
32
33class RAttrMap {
34
35 friend class RAttrBase;
36 friend class RStyle;
37
38public:
39
41
42 class Value_t {
43 public:
44 virtual ~Value_t() = default;
45 virtual EValuesKind Kind() const = 0;
46 virtual bool CanConvertFrom(EValuesKind kind) const { return kind == Kind(); }
47 virtual bool CanConvertTo(EValuesKind kind) const { return kind == Kind(); }
48 virtual bool GetBool() const { return false; }
49 virtual int GetInt() const { return 0; }
50 virtual double GetDouble() const { return 0; }
51 virtual std::string GetString() const { return ""; }
52 virtual bool IsEqual(const Value_t &) const { return false; }
53 virtual std::unique_ptr<Value_t> Copy() const = 0;
54
55 template<typename T> T Get() const;
56
57 template <typename RET_TYPE, typename MATCH_TYPE = void>
58 static RET_TYPE GetValue(const Value_t *rec);
59 };
60
61 class NoValue_t : public Value_t {
62 public:
63 explicit NoValue_t() {}
64 EValuesKind Kind() const final { return kNoValue; }
65 std::unique_ptr<Value_t> Copy() const final { return std::make_unique<NoValue_t>(); }
66 bool IsEqual(const Value_t &tgt) const final { return (tgt.Kind() == kNoValue); }
67 };
68
69 class BoolValue_t : public Value_t {
70 bool v{false}; ///< integer value
71 public:
72 explicit BoolValue_t(bool _v = false) : v(_v) {}
73 EValuesKind Kind() const final { return kBool; }
74 bool CanConvertFrom(EValuesKind kind) const final { return (kind == kDouble) || (kind == kInt) || (kind == kBool) || (kind == kString); }
75 bool CanConvertTo(EValuesKind kind) const final { return (kind == kDouble) || (kind == kInt) || (kind == kBool) || (kind == kString); }
76 bool GetBool() const final { return v; }
77 int GetInt() const final { return v ? 1 : 0; }
78 double GetDouble() const final { return v ? 1 : 0; }
79 std::string GetString() const final { return v ? "true" : "false"; }
80 std::unique_ptr<Value_t> Copy() const final { return std::make_unique<BoolValue_t>(v); }
81 bool IsEqual(const Value_t &tgt) const final { return tgt.GetBool() == v; }
82 };
83
84 class IntValue_t : public Value_t {
85 int v{0}; ///< integer value
86 public:
87 IntValue_t(int _v = 0) : v(_v) {}
88 EValuesKind Kind() const final { return kInt; }
89 bool CanConvertFrom(EValuesKind kind) const final { return (kind == kInt) || (kind == kBool); }
90 bool CanConvertTo(EValuesKind kind) const final { return (kind == kDouble) || (kind == kInt) || (kind == kBool) || (kind == kString); }
91 bool GetBool() const final { return v ? true : false; }
92 int GetInt() const final { return v; }
93 double GetDouble() const final { return v; }
94 std::string GetString() const final { return std::to_string(v); }
95 std::unique_ptr<Value_t> Copy() const final { return std::make_unique<IntValue_t>(v); }
96 bool IsEqual(const Value_t &tgt) const final { return tgt.GetInt() == v; }
97 };
98
99 class DoubleValue_t : public Value_t {
100 double v{0}; ///< double value
101 public:
102 DoubleValue_t(double _v = 0) : v(_v) {}
103 EValuesKind Kind() const final { return kDouble; }
104 bool CanConvertFrom(EValuesKind kind) const final { return (kind == kDouble) || (kind == kInt) || (kind == kBool); }
105 bool CanConvertTo(EValuesKind kind) const final { return (kind == kDouble) || (kind == kBool) || (kind == kString); }
106 bool GetBool() const final { return v ? true : false; }
107 int GetInt() const final { return (int) v; }
108 double GetDouble() const final { return v; }
109 std::string GetString() const final { return std::to_string(v); }
110 std::unique_ptr<Value_t> Copy() const final { return std::make_unique<DoubleValue_t>(v); }
111 bool IsEqual(const Value_t &tgt) const final { return tgt.GetDouble() == v; }
112 };
113
114 class StringValue_t : public Value_t {
115 std::string v; ///< string value
116 public:
117 StringValue_t(const std::string _v = "") : v(_v) {}
118 EValuesKind Kind() const final { return kString; }
119 // all values can be converted into the string
120 bool CanConvertFrom(EValuesKind) const final { return true; }
121 // can convert into string and boolean
122 bool CanConvertTo(EValuesKind kind) const final { return kind == kString; }
123 bool GetBool() const final { return v.compare("true") == 0; }
124 std::string GetString() const final { return v; }
125 bool IsEqual(const Value_t &tgt) const final { return tgt.GetString() == v; }
126 std::unique_ptr<Value_t> Copy() const final { return std::make_unique<StringValue_t>(v); }
127 };
128
129private:
130
131 // FIXME: due to ROOT-10306 only data member of such kind can be correctly stored by ROOT I/O
132 // Once problem fixed, one could make this container a base class
133 std::unordered_map<std::string, std::unique_ptr<Value_t>> m; ///< JSON_object
134
135 void AddBestMatch(const std::string &name, const std::string &value);
136
137public:
138
139 RAttrMap() = default; ///< JSON_asbase - store as map object
140
141 RAttrMap &Add(const std::string &name, std::unique_ptr<Value_t> &&value) { m[name] = std::move(value); return *this; }
142 RAttrMap &AddNoValue(const std::string &name) { m[name] = std::make_unique<NoValue_t>(); return *this; }
143 RAttrMap &AddBool(const std::string &name, bool value) { m[name] = std::make_unique<BoolValue_t>(value); return *this; }
144 RAttrMap &AddInt(const std::string &name, int value) { m[name] = std::make_unique<IntValue_t>(value); return *this; }
145 RAttrMap &AddDouble(const std::string &name, double value) { m[name] = std::make_unique<DoubleValue_t>(value); return *this; }
146 RAttrMap &AddString(const std::string &name, const std::string &value) { m[name] = std::make_unique<StringValue_t>(value); return *this; }
147 RAttrMap &AddPadLength(const std::string &name, const RPadLength &value)
148 {
149 if (value.Empty())
150 Clear(name);
151 else
152 m[name] = std::make_unique<StringValue_t>(value.AsString());
153 return *this;
154 }
155 RAttrMap &AddColor(const std::string &name, const RColor &value)
156 {
157 if (value.IsEmpty())
158 Clear(name);
159 else
160 m[name] = std::make_unique<StringValue_t>(value.AsString());
161 return *this;
162 }
163 RAttrMap &AddDefaults(const RAttrBase &vis);
164
165 RAttrMap &AddValue(const std::string &name, bool value) { return AddBool(name, value); }
166 RAttrMap &AddValue(const std::string &name, int value) { return AddInt(name, value); }
167 RAttrMap &AddValue(const std::string &name, double value) { return AddDouble(name, value); }
168 RAttrMap &AddValue(const std::string &name, const std::string &value) { return AddString(name, value); }
169 RAttrMap &AddValue(const std::string &name, const RPadLength &value) { return AddPadLength(name, value); }
170 RAttrMap &AddValue(const std::string &name, const RColor &value) { return AddColor(name, value); }
171
173 {
174 for (const auto &pair : src.m)
175 m[pair.first] = pair.second->Copy();
176 }
177
179 {
180 m.clear();
181 for (const auto &pair : src.m)
182 m[pair.first] = pair.second->Copy();
183 return *this;
184 }
185
186 const Value_t *Find(const std::string &name) const
187 {
188 auto entry = m.find(name);
189 return (entry != m.end()) ? entry->second.get() : nullptr;
190 }
191
192 /** Clear specified attribute */
193 void Clear(const std::string &name)
194 {
195 auto entry = m.find(name);
196 if (entry != m.end())
197 m.erase(entry);
198 }
199
200 bool Change(const std::string &name, Value_t *value = nullptr);
201
202 auto begin() const { return m.begin(); }
203 auto end() const { return m.end(); }
204};
205
206template<> bool RAttrMap::Value_t::Get<bool>() const;
207template<> int RAttrMap::Value_t::Get<int>() const;
208template<> double RAttrMap::Value_t::Get<double>() const;
209template<> std::string RAttrMap::Value_t::Get<std::string>() const;
210template<> RPadLength RAttrMap::Value_t::Get<RPadLength>() const;
211template<> RColor RAttrMap::Value_t::Get<RColor>() const;
212
213template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
214template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
215template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
216template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
217template<> RPadLength RAttrMap::Value_t::GetValue<RPadLength,void>(const Value_t *rec);
218template<> RColor RAttrMap::Value_t::GetValue<RColor,void>(const Value_t *rec);
219
220template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
221template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
222template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
223template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
224template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,std::string>(const Value_t *rec);
225template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,RPadLength>(const Value_t *rec);
226template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,RColor>(const Value_t *rec);
227
228} // namespace Experimental
229} // namespace ROOT
230
231#endif // ROOT7_RAttrMap
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
char name[80]
Definition TGX11.cxx:110
Base class for all attributes, used with RDrawable.
Definition RAttrBase.hxx:31
std::unique_ptr< Value_t > Copy() const final
Definition RAttrMap.hxx:80
bool CanConvertFrom(EValuesKind kind) const final
Definition RAttrMap.hxx:74
bool CanConvertTo(EValuesKind kind) const final
Definition RAttrMap.hxx:75
std::string GetString() const final
Definition RAttrMap.hxx:79
bool IsEqual(const Value_t &tgt) const final
Definition RAttrMap.hxx:81
std::unique_ptr< Value_t > Copy() const final
Definition RAttrMap.hxx:110
bool CanConvertFrom(EValuesKind kind) const final
Definition RAttrMap.hxx:104
bool IsEqual(const Value_t &tgt) const final
Definition RAttrMap.hxx:111
bool CanConvertTo(EValuesKind kind) const final
Definition RAttrMap.hxx:105
EValuesKind Kind() const final
Definition RAttrMap.hxx:88
bool CanConvertFrom(EValuesKind kind) const final
Definition RAttrMap.hxx:89
std::string GetString() const final
Definition RAttrMap.hxx:94
bool CanConvertTo(EValuesKind kind) const final
Definition RAttrMap.hxx:90
std::unique_ptr< Value_t > Copy() const final
Definition RAttrMap.hxx:95
bool IsEqual(const Value_t &tgt) const final
Definition RAttrMap.hxx:96
bool IsEqual(const Value_t &tgt) const final
Definition RAttrMap.hxx:66
EValuesKind Kind() const final
Definition RAttrMap.hxx:64
std::unique_ptr< Value_t > Copy() const final
Definition RAttrMap.hxx:65
bool IsEqual(const Value_t &tgt) const final
Definition RAttrMap.hxx:125
bool CanConvertTo(EValuesKind kind) const final
Definition RAttrMap.hxx:122
std::unique_ptr< Value_t > Copy() const final
Definition RAttrMap.hxx:126
bool CanConvertFrom(EValuesKind) const final
Definition RAttrMap.hxx:120
virtual std::string GetString() const
Definition RAttrMap.hxx:51
virtual bool CanConvertTo(EValuesKind kind) const
Definition RAttrMap.hxx:47
virtual EValuesKind Kind() const =0
virtual std::unique_ptr< Value_t > Copy() const =0
virtual bool CanConvertFrom(EValuesKind kind) const
Definition RAttrMap.hxx:46
static RET_TYPE GetValue(const Value_t *rec)
virtual double GetDouble() const
Definition RAttrMap.hxx:50
virtual bool IsEqual(const Value_t &) const
Definition RAttrMap.hxx:52
RAttrMap & AddString(const std::string &name, const std::string &value)
Definition RAttrMap.hxx:146
const Value_t * Find(const std::string &name) const
Definition RAttrMap.hxx:186
RAttrMap & AddValue(const std::string &name, bool value)
Definition RAttrMap.hxx:165
RAttrMap & AddDouble(const std::string &name, double value)
Definition RAttrMap.hxx:145
RAttrMap & AddNoValue(const std::string &name)
Definition RAttrMap.hxx:142
RAttrMap & operator=(const RAttrMap &src)
Definition RAttrMap.hxx:178
RAttrMap(const RAttrMap &src)
Definition RAttrMap.hxx:172
RAttrMap & AddValue(const std::string &name, double value)
Definition RAttrMap.hxx:167
std::unordered_map< std::string, std::unique_ptr< Value_t > > m
JSON_object.
Definition RAttrMap.hxx:133
RAttrMap & AddInt(const std::string &name, int value)
Definition RAttrMap.hxx:144
bool Change(const std::string &name, Value_t *value=nullptr)
Change attribute using string value and kind Used to change attributes from JS side Returns true if v...
Definition RAttrMap.cxx:137
RAttrMap & AddColor(const std::string &name, const RColor &value)
Definition RAttrMap.hxx:155
RAttrMap & AddValue(const std::string &name, const RColor &value)
Definition RAttrMap.hxx:170
RAttrMap & AddPadLength(const std::string &name, const RPadLength &value)
Definition RAttrMap.hxx:147
RAttrMap & AddBool(const std::string &name, bool value)
Definition RAttrMap.hxx:143
RAttrMap & AddValue(const std::string &name, const RPadLength &value)
Definition RAttrMap.hxx:169
RAttrMap & AddDefaults(const RAttrBase &vis)
Add defaults values form sub attribute.
Definition RAttrMap.cxx:49
void Clear(const std::string &name)
Clear specified attribute.
Definition RAttrMap.hxx:193
void AddBestMatch(const std::string &name, const std::string &value)
Add attribute, converting to best possible type Tested boolean, int, double.
Definition RAttrMap.cxx:69
RAttrMap & Add(const std::string &name, std::unique_ptr< Value_t > &&value)
Definition RAttrMap.hxx:141
RAttrMap & AddValue(const std::string &name, const std::string &value)
Definition RAttrMap.hxx:168
RAttrMap()=default
JSON_asbase - store as map object.
RAttrMap & AddValue(const std::string &name, int value)
Definition RAttrMap.hxx:166
The color class.
Definition RColor.hxx:33
A set of defaults for graphics attributes, e.g.
Definition RStyle.hxx:32
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.