Logo ROOT  
Reference Guide
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) { m[name] = std::make_unique<StringValue_t>(value.AsString()); return *this; }
148 RAttrMap &AddDefaults(const RAttrBase &vis);
149
150 RAttrMap(const RAttrMap &src)
151 {
152 for (const auto &pair : src.m)
153 m[pair.first] = pair.second->Copy();
154 }
155
157 {
158 m.clear();
159 for (const auto &pair : src.m)
160 m[pair.first] = pair.second->Copy();
161 return *this;
162 }
163
164 const Value_t *Find(const std::string &name) const
165 {
166 auto entry = m.find(name);
167 return (entry != m.end()) ? entry->second.get() : nullptr;
168 }
169
170 /** Clear specified attribute */
171 void Clear(const std::string &name)
172 {
173 auto entry = m.find(name);
174 if (entry != m.end())
175 m.erase(entry);
176 }
177
178 bool Change(const std::string &name, Value_t *value = nullptr);
179
180 auto begin() const { return m.begin(); }
181 auto end() const { return m.end(); }
182};
183
184template<> bool RAttrMap::Value_t::Get<bool>() const;
185template<> int RAttrMap::Value_t::Get<int>() const;
186template<> double RAttrMap::Value_t::Get<double>() const;
187template<> std::string RAttrMap::Value_t::Get<std::string>() const;
188template<> RPadLength RAttrMap::Value_t::Get<RPadLength>() const;
189
190template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
191template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
192template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
193template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
194template<> RPadLength RAttrMap::Value_t::GetValue<RPadLength,void>(const Value_t *rec);
195
196template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
197template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
198template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
199template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
200template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,std::string>(const Value_t *rec);
201template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,RPadLength>(const Value_t *rec);
202
203} // namespace Experimental
204} // namespace ROOT
205
206#endif // ROOT7_RAttrMap
char name[80]
Definition: TGX11.cxx:109
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:27
EValuesKind Kind() const final
Definition: RAttrMap.hxx:73
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
std::string GetString() const final
Definition: RAttrMap.hxx:109
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
std::string GetString() const final
Definition: RAttrMap.hxx:124
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:125
StringValue_t(const std::string _v="")
Definition: RAttrMap.hxx:117
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:164
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:156
RAttrMap(const RAttrMap &src)
Definition: RAttrMap.hxx:150
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 & 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:171
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()=default
JSON_asbase - store as map object.
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
Definition: RPadLength.cxx:19
A set of defaults for graphics attributes, e.g.
Definition: RStyle.hxx:31
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...
Definition: StringConv.hxx:21