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/RMakeUnique.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 &AddDefaults(const RAttrBase &vis);
156
157 RAttrMap &AddValue(const std::string &name, bool value) { return AddBool(name, value); }
158 RAttrMap &AddValue(const std::string &name, int value) { return AddInt(name, value); }
159 RAttrMap &AddValue(const std::string &name, double value) { return AddDouble(name, value); }
160 RAttrMap &AddValue(const std::string &name, const std::string &value) { return AddString(name, value); }
161 RAttrMap &AddValue(const std::string &name, const RPadLength &value) { return AddPadLength(name, value); }
162
163 RAttrMap(const RAttrMap &src)
164 {
165 for (const auto &pair : src.m)
166 m[pair.first] = pair.second->Copy();
167 }
168
170 {
171 m.clear();
172 for (const auto &pair : src.m)
173 m[pair.first] = pair.second->Copy();
174 return *this;
175 }
176
177 const Value_t *Find(const std::string &name) const
178 {
179 auto entry = m.find(name);
180 return (entry != m.end()) ? entry->second.get() : nullptr;
181 }
182
183 /** Clear specified attribute */
184 void Clear(const std::string &name)
185 {
186 auto entry = m.find(name);
187 if (entry != m.end())
188 m.erase(entry);
189 }
190
191 bool Change(const std::string &name, Value_t *value = nullptr);
192
193 auto begin() const { return m.begin(); }
194 auto end() const { return m.end(); }
195};
196
197template<> bool RAttrMap::Value_t::Get<bool>() const;
198template<> int RAttrMap::Value_t::Get<int>() const;
199template<> double RAttrMap::Value_t::Get<double>() const;
200template<> std::string RAttrMap::Value_t::Get<std::string>() const;
201template<> RPadLength RAttrMap::Value_t::Get<RPadLength>() const;
202
203template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
204template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
205template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
206template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
207template<> RPadLength RAttrMap::Value_t::GetValue<RPadLength,void>(const Value_t *rec);
208
209template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
210template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
211template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
212template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
213template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,std::string>(const Value_t *rec);
214template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,RPadLength>(const Value_t *rec);
215
216} // namespace Experimental
217} // namespace ROOT
218
219#endif // ROOT7_RAttrMap
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:177
RAttrMap & AddValue(const std::string &name, bool value)
Definition RAttrMap.hxx:157
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:169
RAttrMap(const RAttrMap &src)
Definition RAttrMap.hxx:163
RAttrMap & AddValue(const std::string &name, double value)
Definition RAttrMap.hxx:159
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:127
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:161
RAttrMap & AddDefaults(const RAttrBase &vis)
Add defaults values form sub attribute.
Definition RAttrMap.cxx:45
void Clear(const std::string &name)
Clear specified attribute.
Definition RAttrMap.hxx:184
void AddBestMatch(const std::string &name, const std::string &value)
Add attribute, converting to best possible type Tested boolean, int, double.
Definition RAttrMap.cxx:59
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:160
RAttrMap()=default
JSON_asbase - store as map object.
RAttrMap & AddValue(const std::string &name, int value)
Definition RAttrMap.hxx:158
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
A set of defaults for graphics attributes, e.g.
Definition RStyle.hxx:32
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...