Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RAttrMap.hxx
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 1995-2019, 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 #include <vector>
17 #include <list>
18 #include <ROOT/RMakeUnique.hxx>
19 
20 namespace ROOT {
21 namespace Experimental {
22 
23 class RAttrBase;
24 
25 /** \class RAttrMap
26 \ingroup GpadROOT7
27 \authors Axel Naumann <axel@cern.ch> Sergey Linev <s.linev@gsi.de>
28 \date 2017-09-26
29 \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
30 */
31 
32 class RAttrMap {
33 
34  friend class RAttrBase;
35 
36 public:
37 
38  enum EValuesKind { kBool, kInt, kDouble, kString };
39 
40  class Value_t {
41  public:
42  virtual ~Value_t() = default;
43  virtual EValuesKind Kind() const = 0;
44  virtual bool Compatible(EValuesKind kind) const { return kind == Kind(); }
45  virtual bool GetBool() const { return false; }
46  virtual int GetInt() const { return 0; }
47  virtual double GetDouble() const { return 0; }
48  virtual std::string GetString() const { return ""; }
49  virtual bool IsEqual(const Value_t &) const { return false; }
50  virtual std::unique_ptr<Value_t> Copy() const = 0;
51 
52  template<typename T> T Get() const;
53 
54  template <typename RET_TYPE, typename MATCH_TYPE = void>
55  static RET_TYPE GetValue(const Value_t *rec);
56  };
57 
58  class BoolValue_t : public Value_t {
59  bool v{false}; ///< integer value
60  public:
61  explicit BoolValue_t(bool _v = false) : v(_v) {}
62  EValuesKind Kind() const final { return kBool; }
63  bool GetBool() const final { return v; }
64  std::unique_ptr<Value_t> Copy() const final { return std::make_unique<BoolValue_t>(v); }
65  bool IsEqual(const Value_t &tgt) const final { return (tgt.Kind() == kBool) && (tgt.GetBool() == v); }
66  };
67 
68  class IntValue_t : public Value_t {
69  int v{0}; ///< integer value
70  public:
71  IntValue_t(int _v = 0) : v(_v) {}
72  EValuesKind Kind() const final { return kInt; }
73  int GetInt() const final { return v; }
74  std::unique_ptr<Value_t> Copy() const final { return std::make_unique<IntValue_t>(v); }
75  bool IsEqual(const Value_t &tgt) const final { return (tgt.Kind() == kInt) && (tgt.GetInt() == v); }
76  };
77 
78  class DoubleValue_t : public Value_t {
79  double v{0}; ///< double value
80  public:
81  DoubleValue_t(double _v = 0) : v(_v) {}
82  EValuesKind Kind() const final { return kDouble; }
83  double GetDouble() const final { return v; }
84  std::unique_ptr<Value_t> Copy() const final { return std::make_unique<DoubleValue_t>(v); }
85  bool IsEqual(const Value_t &tgt) const final { return (tgt.Kind() == kDouble) && (tgt.GetDouble() == v); }
86  };
87 
88  class StringValue_t : public Value_t {
89  std::string v; ///< string value
90  public:
91  StringValue_t(const std::string _v = "") : v(_v) {}
92  EValuesKind Kind() const final { return kString; }
93  std::string GetString() const final { return v; }
94  bool IsEqual(const Value_t &tgt) const final { return (tgt.Kind() == kString) && (tgt.GetString() == v); }
95  std::unique_ptr<Value_t> Copy() const final { return std::make_unique<StringValue_t>(v); }
96  };
97 
98 private:
99 
100  // FIXME: due to ROOT-10306 only data member of such kind can be correctly stored by ROOT I/O
101  // Once problem fixed, one could make this container a base class
102  std::unordered_map<std::string, std::unique_ptr<Value_t>> m; ///< JSON_object
103 
104 
105 public:
106 
107  RAttrMap() = default; ///< JSON_asbase - store as map object
108 
109  RAttrMap &Add(const std::string &name, std::unique_ptr<Value_t> &&value) { m[name] = std::move(value); return *this; }
110  RAttrMap &AddBool(const std::string &name, bool value) { m[name] = std::make_unique<BoolValue_t>(value); return *this; }
111  RAttrMap &AddInt(const std::string &name, int value) { m[name] = std::make_unique<IntValue_t>(value); return *this; }
112  RAttrMap &AddDouble(const std::string &name, double value) { m[name] = std::make_unique<DoubleValue_t>(value); return *this; }
113  RAttrMap &AddString(const std::string &name, const std::string &value) { m[name] = std::make_unique<StringValue_t>(value); return *this; }
114  RAttrMap &AddDefaults(const RAttrBase &vis);
115 
116  RAttrMap(const RAttrMap &src)
117  {
118  for (const auto &pair : src.m)
119  m[pair.first] = pair.second->Copy();
120  }
121 
122  RAttrMap &operator=(const RAttrMap &src)
123  {
124  m.clear();
125  for (const auto &pair : src.m)
126  m[pair.first] = pair.second->Copy();
127  return *this;
128  }
129 
130  const Value_t *Find(const std::string &name) const
131  {
132  auto entry = m.find(name);
133  return (entry != m.end()) ? entry->second.get() : nullptr;
134  }
135 
136  void Clear(const std::string &name)
137  {
138  auto entry = m.find(name);
139  if (entry != m.end())
140  m.erase(entry);
141  }
142 
143  auto begin() const { return m.begin(); }
144  auto end() const { return m.end(); }
145 };
146 
147 template<> bool RAttrMap::Value_t::Get<bool>() const;
148 template<> int RAttrMap::Value_t::Get<int>() const;
149 template<> double RAttrMap::Value_t::Get<double>() const;
150 template<> std::string RAttrMap::Value_t::Get<std::string>() const;
151 
152 template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
153 template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
154 template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
155 template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
156 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
157 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
158 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
159 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
160 template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,std::string>(const Value_t *rec);
161 
162 
163 //////////////////////////////////////////////////////////////////////////
164 
165 
166 } // namespace Experimental
167 } // namespace ROOT
168 
169 #endif // ROOT7_RAttrMap