Logo ROOT  
Reference Guide
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
20namespace ROOT {
21namespace Experimental {
22
23class 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
32class RAttrMap {
33
34 friend class RAttrBase;
35
36public:
37
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
98private:
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
105public:
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
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
147template<> bool RAttrMap::Value_t::Get<bool>() const;
148template<> int RAttrMap::Value_t::Get<int>() const;
149template<> double RAttrMap::Value_t::Get<double>() const;
150template<> std::string RAttrMap::Value_t::Get<std::string>() const;
151
152template<> bool RAttrMap::Value_t::GetValue<bool,void>(const Value_t *rec);
153template<> int RAttrMap::Value_t::GetValue<int,void>(const Value_t *rec);
154template<> double RAttrMap::Value_t::GetValue<double,void>(const Value_t *rec);
155template<> std::string RAttrMap::Value_t::GetValue<std::string,void>(const Value_t *rec);
156template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,void>(const Value_t *rec);
157template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,bool>(const Value_t *rec);
158template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,int>(const Value_t *rec);
159template<> const RAttrMap::Value_t *RAttrMap::Value_t::GetValue<const RAttrMap::Value_t *,double>(const Value_t *rec);
160template<> 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
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:62
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:64
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:65
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:84
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:85
EValuesKind Kind() const final
Definition: RAttrMap.hxx:72
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:74
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:75
std::string GetString() const final
Definition: RAttrMap.hxx:93
bool IsEqual(const Value_t &tgt) const final
Definition: RAttrMap.hxx:94
StringValue_t(const std::string _v="")
Definition: RAttrMap.hxx:91
std::unique_ptr< Value_t > Copy() const final
Definition: RAttrMap.hxx:95
virtual std::string GetString() const
Definition: RAttrMap.hxx:48
virtual EValuesKind Kind() const =0
virtual std::unique_ptr< Value_t > Copy() const =0
static RET_TYPE GetValue(const Value_t *rec)
virtual bool Compatible(EValuesKind kind) const
Definition: RAttrMap.hxx:44
virtual double GetDouble() const
Definition: RAttrMap.hxx:47
virtual bool IsEqual(const Value_t &) const
Definition: RAttrMap.hxx:49
RAttrMap & AddDefaults(const RAttrBase &vis)
Definition: RAttrMap.cxx:33
RAttrMap & AddString(const std::string &name, const std::string &value)
Definition: RAttrMap.hxx:113
const Value_t * Find(const std::string &name) const
Definition: RAttrMap.hxx:130
RAttrMap & AddDouble(const std::string &name, double value)
Definition: RAttrMap.hxx:112
RAttrMap & operator=(const RAttrMap &src)
Definition: RAttrMap.hxx:122
RAttrMap(const RAttrMap &src)
Definition: RAttrMap.hxx:116
std::unordered_map< std::string, std::unique_ptr< Value_t > > m
JSON_object.
Definition: RAttrMap.hxx:102
RAttrMap & AddInt(const std::string &name, int value)
Definition: RAttrMap.hxx:111
RAttrMap & AddBool(const std::string &name, bool value)
Definition: RAttrMap.hxx:110
void Clear(const std::string &name)
Definition: RAttrMap.hxx:136
RAttrMap & Add(const std::string &name, std::unique_ptr< Value_t > &&value)
Definition: RAttrMap.hxx:109
RAttrMap()=default
JSON_asbase - store as map object.
double T(double x)
Definition: ChebyshevPol.h:34
VSD Structures.
Definition: StringConv.hxx:21