userver: userver/utils/default_dict.hpp Source File
Loading...
Searching...
No Matches
default_dict.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <optional>
7#include <string>
8#include <type_traits>
9
13#include <userver/utils/impl/transparent_hash.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace utils {
18
20inline constexpr std::string_view kDefaultDictDefaultName = "__default__";
21
22namespace impl {
23
24[[noreturn]] void ThrowNoValueException(std::string_view dict_name, std::string_view key);
25
26} // namespace impl
27
42template <typename ValueType>
43class DefaultDict final {
44public:
45 using DictType = utils::impl::TransparentMap<std::string, ValueType>;
46 using const_iterator = typename DictType::const_iterator;
47 using iterator = const_iterator;
48 using value_type = typename DictType::value_type;
49 using key_type = std::string;
50 using mapped_type = ValueType;
51 using init_list = std::initializer_list<std::pair<std::string_view, ValueType>>;
52
53 DefaultDict() = default;
54
55 DefaultDict(init_list contents)
56 : dict_(contents.begin(), contents.end())
57 {}
58
59 DefaultDict(DictType dict)
60 : dict_(std::move(dict))
61 {}
62
63 DefaultDict(std::string name, init_list contents)
64 : name_(std::move(name)),
65 dict_(contents.begin(), contents.end())
66 {}
67
68 DefaultDict(std::string name, DictType dict)
69 : name_(std::move(name)),
70 dict_(std::move(dict))
71 {}
72
75 bool HasDefaultValue() const noexcept { return HasValue(kDefaultDictDefaultName); }
76
78 bool HasValue(std::string_view key) const noexcept { return dict_.find(key) != dict_.end(); }
79
82 const ValueType& GetDefaultValue() const {
83 const auto it = dict_.find(kDefaultDictDefaultName);
84 if (it == dict_.end()) {
85 impl::ThrowNoValueException(name_, kDefaultDictDefaultName);
86 }
87 return it->second;
88 }
89
93 const ValueType& operator[](std::string_view key) const {
94 auto it = dict_.find(key);
95 if (it == dict_.end()) {
96 it = dict_.find(kDefaultDictDefaultName);
97 if (it == dict_.end()) {
98 impl::ThrowNoValueException(name_, key);
99 }
100 }
101 return it->second;
102 }
103
105 template <typename StringType>
106 const ValueType& operator[](const std::optional<StringType>& key) const {
107 return key ? Get(*key) : GetDefaultValue();
108 }
109
113 const ValueType& Get(std::string_view key) const { return (*this)[key]; }
114
116 template <typename StringType>
117 const ValueType& Get(const std::optional<StringType>& key) const {
118 return (*this)[key];
119 }
120
124 std::optional<ValueType> GetOptional(std::string_view key) const {
125 auto it = dict_.find(key);
126 if (it == dict_.end()) {
127 it = dict_.find(kDefaultDictDefaultName);
128 if (it == dict_.end()) {
129 return std::nullopt;
130 }
131 }
132
133 return it->second;
134 }
135
140 void SetDefault(ValueType value) { Set(kDefaultDictDefaultName, std::move(value)); }
141
146 template <typename StringType>
147 void Set(StringType&& key, ValueType value) {
148 using ForwardedKey =
149 std::conditional_t<std::is_same_v<std::decay_t<StringType>, std::string>, StringType&&, std::string>;
150 dict_.insert_or_assign(static_cast<ForwardedKey>(key), std::move(value));
151 }
152
153 auto begin() const noexcept { return dict_.begin(); }
154
155 auto end() const noexcept { return dict_.end(); }
156
157 const std::string& GetName() const noexcept { return name_; }
158
159 bool operator==(const DefaultDict& r) const noexcept { return dict_ == r.dict_; }
160
161private:
162 std::string name_;
163 DictType dict_;
164};
165
166template <formats::common::IsFormatValue Value, typename T>
167DefaultDict<T> Parse(const Value& value, formats::parse::To<DefaultDict<T>>) {
168 return DefaultDict<T>{value.GetPath(), value.template As<typename DefaultDict<T>::DictType>()};
169}
170
171} // namespace utils
172
173USERVER_NAMESPACE_END