10 #define ROOT7_RAttrMap
14 #include <type_traits>
15 #include <unordered_map>
21 namespace Experimental {
34 friend class RAttrBase;
38 enum EValuesKind { kBool, kInt, kDouble, kString };
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;
52 template<
typename T> T Get()
const;
54 template <
typename RET_TYPE,
typename MATCH_TYPE =
void>
55 static RET_TYPE GetValue(
const Value_t *rec);
58 class BoolValue_t :
public Value_t {
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); }
68 class IntValue_t :
public Value_t {
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); }
78 class DoubleValue_t :
public Value_t {
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); }
88 class StringValue_t :
public Value_t {
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); }
102 std::unordered_map<std::string, std::unique_ptr<Value_t>> m;
107 RAttrMap() =
default;
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);
116 RAttrMap(
const RAttrMap &src)
118 for (
const auto &pair : src.m)
119 m[pair.first] = pair.second->Copy();
122 RAttrMap &operator=(
const RAttrMap &src)
125 for (
const auto &pair : src.m)
126 m[pair.first] = pair.second->Copy();
130 const Value_t *Find(
const std::string &name)
const
132 auto entry = m.find(name);
133 return (entry != m.end()) ? entry->second.get() :
nullptr;
136 void Clear(
const std::string &name)
138 auto entry = m.find(name);
139 if (entry != m.end())
143 auto begin()
const {
return m.begin(); }
144 auto end()
const {
return m.end(); }
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;
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);
169 #endif // ROOT7_RAttrMap