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 
20 namespace ROOT {
21 namespace Experimental {
22 
23 class RAttrBase;
24 class 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 
33 class RAttrMap {
34 
35  friend class RAttrBase;
36  friend class RStyle;
37 
38 public:
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 
129 private:
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 
137 public:
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  }
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 
197 template<> bool RAttrMap::Value_t::Get<bool>() const;
198 template<> int RAttrMap::Value_t::Get<int>() const;
199 template<> double RAttrMap::Value_t::Get<double>() const;
200 template<> std::string RAttrMap::Value_t::Get<std::string>() const;
201 template<> RPadLength RAttrMap::Value_t::Get<RPadLength>() const;
202 
203 template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
204 template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
205 template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
206 template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
207 template<> RPadLength RAttrMap::Value_t::GetValue<RPadLength,void>(const Value_t *rec);
208 
209 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
210 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
211 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
212 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
213 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,std::string>(const Value_t *rec);
214 template<> 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
ROOT::Experimental::RAttrMap::BoolValue_t::CanConvertFrom
bool CanConvertFrom(EValuesKind kind) const final
Definition: RAttrMap.hxx:86
ROOT::Experimental::RAttrMap::DoubleValue_t::v
double v
double value
Definition: RAttrMap.hxx:112
ROOT::Experimental::RAttrMap::BoolValue_t::IsEqual
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:93
ROOT::Experimental::RAttrMap::kString
@ kString
Definition: RAttrMap.hxx:52
ROOT::Experimental::RAttrMap::IntValue_t::GetDouble
double GetDouble() const final
Definition: RAttrMap.hxx:105
ROOT::Experimental::RAttrMap::m
std::unordered_map< std::string, std::unique_ptr< Value_t > > m
JSON_object.
Definition: RAttrMap.hxx:145
ROOT::Experimental::RAttrMap::AddValue
RAttrMap & AddValue(const std::string &name, bool value)
Definition: RAttrMap.hxx:169
ROOT::Experimental::RAttrMap::Value_t::CanConvertFrom
virtual bool CanConvertFrom(EValuesKind kind) const
Definition: RAttrMap.hxx:58
ROOT::Experimental::RAttrMap::StringValue_t::Copy
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:138
ROOT::Experimental::RAttrMap::DoubleValue_t::GetString
std::string GetString() const final
Definition: RAttrMap.hxx:121
ROOT::Experimental::RAttrMap::Value_t
Definition: RAttrMap.hxx:54
ROOT::Experimental::RAttrMap
Definition: RAttrMap.hxx:45
ROOT::Experimental::RAttrMap::DoubleValue_t::CanConvertTo
bool CanConvertTo(EValuesKind kind) const final
Definition: RAttrMap.hxx:117
ROOT::Experimental::RAttrMap::Value_t::Kind
virtual EValuesKind Kind() const =0
ROOT::Experimental::RAttrMap::AddBool
RAttrMap & AddBool(const std::string &name, bool value)
Definition: RAttrMap.hxx:155
ROOT::Experimental::RAttrBase
Base class for all attributes, used with RDrawable.
Definition: RAttrBase.hxx:39
ROOT::Experimental::RAttrMap::AddDefaults
RAttrMap & AddDefaults(const RAttrBase &vis)
Add defaults values form sub attribute.
Definition: RAttrMap.cxx:45
ROOT::Experimental::RAttrMap::StringValue_t::GetString
std::string GetString() const final
Definition: RAttrMap.hxx:136
ROOT::Experimental::RAttrMap::NoValue_t::IsEqual
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:78
ROOT::Experimental::RAttrMap::DoubleValue_t::CanConvertFrom
bool CanConvertFrom(EValuesKind kind) const final
Definition: RAttrMap.hxx:116
ROOT::Experimental::RAttrMap::DoubleValue_t::DoubleValue_t
DoubleValue_t(double _v=0)
Definition: RAttrMap.hxx:114
ROOT::Experimental::RAttrMap::StringValue_t::StringValue_t
StringValue_t(const std::string _v="")
Definition: RAttrMap.hxx:129
ROOT::Experimental::RAttrMap::EValuesKind
EValuesKind
Definition: RAttrMap.hxx:52
ROOT::Experimental::RAttrMap::Find
const Value_t * Find(const std::string &name) const
Definition: RAttrMap.hxx:189
ROOT::Experimental::RAttrMap::IntValue_t::GetInt
int GetInt() const final
Definition: RAttrMap.hxx:104
ROOT::Experimental::RAttrMap::IntValue_t::IntValue_t
IntValue_t(int _v=0)
Definition: RAttrMap.hxx:99
ROOT::Experimental::RAttrMap::IntValue_t::CanConvertFrom
bool CanConvertFrom(EValuesKind kind) const final
Definition: RAttrMap.hxx:101
ROOT::Experimental::RAttrMap::begin
auto begin() const
Definition: RAttrMap.hxx:205
ROOT::Experimental::RAttrMap::IntValue_t::Copy
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:107
ROOT::Experimental::RAttrMap::AddPadLength
RAttrMap & AddPadLength(const std::string &name, const RPadLength &value)
Definition: RAttrMap.hxx:159
ROOT::Experimental::RAttrMap::kDouble
@ kDouble
Definition: RAttrMap.hxx:52
ROOT::Experimental::RAttrMap::Value_t::~Value_t
virtual ~Value_t()=default
ROOT::Experimental::RPadLength
A length in RPad.
Definition: RPadLength.hxx:43
ROOT::Experimental::RAttrMap::BoolValue_t::CanConvertTo
bool CanConvertTo(EValuesKind kind) const final
Definition: RAttrMap.hxx:87
ROOT::Experimental::RAttrMap::DoubleValue_t::Copy
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:122
ROOT::Experimental::RAttrMap::IntValue_t
Definition: RAttrMap.hxx:96
ROOT::Experimental::RAttrMap::StringValue_t::GetBool
bool GetBool() const final
Definition: RAttrMap.hxx:135
ROOT::Experimental::RAttrMap::IntValue_t::v
int v
integer value
Definition: RAttrMap.hxx:97
ROOT::Experimental::RAttrMap::StringValue_t::v
std::string v
string value
Definition: RAttrMap.hxx:127
ROOT::Experimental::RAttrMap::Value_t::GetString
virtual std::string GetString() const
Definition: RAttrMap.hxx:63
ROOT::Experimental::RAttrMap::NoValue_t::Kind
EValuesKind Kind() const final
Definition: RAttrMap.hxx:76
ROOT::Experimental::RAttrMap::DoubleValue_t::Kind
EValuesKind Kind() const final
Definition: RAttrMap.hxx:115
ROOT::Experimental::RAttrMap::DoubleValue_t::IsEqual
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:123
ROOT::Experimental::RAttrMap::BoolValue_t::GetInt
int GetInt() const final
Definition: RAttrMap.hxx:89
ROOT::Experimental::RAttrMap::BoolValue_t::Kind
EValuesKind Kind() const final
Definition: RAttrMap.hxx:85
ROOT::Experimental::RAttrMap::IntValue_t::Kind
EValuesKind Kind() const final
Definition: RAttrMap.hxx:100
ROOT::Experimental::RAttrMap::kNoValue
@ kNoValue
Definition: RAttrMap.hxx:52
ROOT::Experimental::RAttrMap::Value_t::CanConvertTo
virtual bool CanConvertTo(EValuesKind kind) const
Definition: RAttrMap.hxx:59
ROOT::Experimental::RAttrMap::AddDouble
RAttrMap & AddDouble(const std::string &name, double value)
Definition: RAttrMap.hxx:157
ROOT::Experimental::RAttrMap::DoubleValue_t
Definition: RAttrMap.hxx:111
ROOT::Experimental::RAttrMap::StringValue_t::Kind
EValuesKind Kind() const final
Definition: RAttrMap.hxx:130
ROOT::Experimental::RAttrMap::StringValue_t
Definition: RAttrMap.hxx:126
ROOT::Experimental::RAttrMap::StringValue_t::CanConvertTo
bool CanConvertTo(EValuesKind kind) const final
Definition: RAttrMap.hxx:134
ROOT::Experimental::RAttrMap::BoolValue_t::BoolValue_t
BoolValue_t(bool _v=false)
Definition: RAttrMap.hxx:84
ROOT::Experimental::RAttrMap::NoValue_t::Copy
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:77
ROOT::Experimental::RAttrMap::Value_t::GetBool
virtual bool GetBool() const
Definition: RAttrMap.hxx:60
ROOT::Experimental::RAttrMap::Add
RAttrMap & Add(const std::string &name, std::unique_ptr< Value_t > &&value)
Definition: RAttrMap.hxx:153
ROOT::Experimental::RAttrMap::BoolValue_t::GetBool
bool GetBool() const final
Definition: RAttrMap.hxx:88
RPadLength.hxx
ROOT::Experimental::RAttrMap::DoubleValue_t::GetBool
bool GetBool() const final
Definition: RAttrMap.hxx:118
ROOT::Experimental::RAttrMap::Value_t::Copy
virtual std::unique_ptr< Value_t > Copy() const =0
ROOT::Experimental::RAttrMap::NoValue_t::NoValue_t
NoValue_t()
Definition: RAttrMap.hxx:75
ROOT::Experimental::RAttrMap::AddBestMatch
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
ROOT::Experimental::RAttrMap::RAttrMap
RAttrMap()=default
JSON_asbase - store as map object.
ROOT::Experimental::RAttrMap::RAttrBase
friend class RAttrBase
Definition: RAttrMap.hxx:47
ROOT::Experimental::RAttrMap::Value_t::GetInt
virtual int GetInt() const
Definition: RAttrMap.hxx:61
ROOT::Experimental::RAttrMap::Value_t::GetValue
static RET_TYPE GetValue(const Value_t *rec)
ROOT::Experimental::RAttrMap::IntValue_t::IsEqual
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:108
ROOT::Experimental::RAttrMap::Clear
void Clear(const std::string &name)
Clear specified attribute.
Definition: RAttrMap.hxx:196
ROOT::Experimental::RAttrMap::DoubleValue_t::GetDouble
double GetDouble() const final
Definition: RAttrMap.hxx:120
ROOT::Experimental::RAttrMap::IntValue_t::CanConvertTo
bool CanConvertTo(EValuesKind kind) const final
Definition: RAttrMap.hxx:102
ROOT::Experimental::RAttrMap::kInt
@ kInt
Definition: RAttrMap.hxx:52
ROOT::Experimental::RAttrMap::IntValue_t::GetBool
bool GetBool() const final
Definition: RAttrMap.hxx:103
ROOT::Experimental::RAttrMap::BoolValue_t::GetDouble
double GetDouble() const final
Definition: RAttrMap.hxx:90
ROOT::Experimental::RAttrMap::Change
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
ROOT::Experimental::RAttrMap::AddString
RAttrMap & AddString(const std::string &name, const std::string &value)
Definition: RAttrMap.hxx:158
ROOT::Experimental::RAttrMap::BoolValue_t::GetString
std::string GetString() const final
Definition: RAttrMap.hxx:91
ROOT::Experimental::RAttrMap::end
auto end() const
Definition: RAttrMap.hxx:206
ROOT::Experimental::RAttrMap::DoubleValue_t::GetInt
int GetInt() const final
Definition: RAttrMap.hxx:119
ROOT::Experimental::RAttrMap::Value_t::IsEqual
virtual bool IsEqual(const Value_t &) const
Definition: RAttrMap.hxx:64
name
char name[80]
Definition: TGX11.cxx:110
ROOT::Experimental::RAttrMap::kBool
@ kBool
Definition: RAttrMap.hxx:52
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:52
ROOT::Experimental::RAttrMap::Value_t::GetDouble
virtual double GetDouble() const
Definition: RAttrMap.hxx:62
ROOT::Experimental::RAttrMap::RStyle
friend class RStyle
Definition: RAttrMap.hxx:48
ROOT::Experimental::RAttrMap::BoolValue_t::Copy
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:92
ROOT::Experimental::RAttrMap::AddNoValue
RAttrMap & AddNoValue(const std::string &name)
Definition: RAttrMap.hxx:154
RMakeUnique.hxx
ROOT::Experimental::RAttrMap::IntValue_t::GetString
std::string GetString() const final
Definition: RAttrMap.hxx:106
ROOT::Experimental::RAttrMap::AddInt
RAttrMap & AddInt(const std::string &name, int value)
Definition: RAttrMap.hxx:156
ROOT::Experimental::RAttrMap::operator=
RAttrMap & operator=(const RAttrMap &src)
Definition: RAttrMap.hxx:181
ROOT::Experimental::RAttrMap::StringValue_t::CanConvertFrom
bool CanConvertFrom(EValuesKind) const final
Definition: RAttrMap.hxx:132
ROOT::Experimental::RAttrMap::NoValue_t
Definition: RAttrMap.hxx:73
ROOT::Experimental::RAttrMap::Value_t::Get
T Get() const
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Experimental::RAttrMap::StringValue_t::IsEqual
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:137
ROOT::Experimental::RAttrMap::BoolValue_t::v
bool v
integer value
Definition: RAttrMap.hxx:82