libzypp: zypp-media/ng/headervaluemap.cc Source File

libzypp 17.36.6

headervaluemap.cc
Go to the documentation of this file.
1#include "headervaluemap.h"
2#include <zypp-core/base/String.h>
3
4namespace zyppng {
5
6 HeaderValueMap::Value HeaderValueMap::InvalidValue;
7
8 HeaderValue::HeaderValue()
9 : _val ( new value_type() )
10 {}
11
12 HeaderValue::HeaderValue( const HeaderValue &other )
13 : _val ( new value_type( *other._val ) )
14 {}
15
16 HeaderValue::HeaderValue( HeaderValue &&other ) noexcept
17 : _val ( new value_type( std::move(*other._val) ) )
18 {}
19
20 HeaderValue::HeaderValue( const bool val )
21 : _val ( new value_type(val) )
22 {}
23
24 HeaderValue::HeaderValue( const int32_t val )
25 : _val ( new value_type(val) )
26 {}
27
28 HeaderValue::HeaderValue( const int64_t val )
29 : _val ( new value_type(val) )
30 {}
31
32 HeaderValue::HeaderValue( const std::string &val )
33 : _val ( new value_type(val) )
34 {}
35
36 HeaderValue::HeaderValue(const char *val)
37 : HeaderValue( zypp::str::asString (val) )
38 {}
39
40 HeaderValue::HeaderValue( std::string &&val )
41 : _val ( new value_type( std::move(val) ) )
42 {}
43
44 bool HeaderValue::valid() const
45 {
46 return ( _val->index () > 0 );
47 }
48
49 bool HeaderValue::isString() const
50 {
51 return std::holds_alternative<std::string>(*_val);
52 }
53
54 bool HeaderValue::isInt() const
55 {
56 return std::holds_alternative<int32_t>(*_val);
57 }
58
59 bool HeaderValue::isInt64() const
60 {
61 return std::holds_alternative<int64_t>(*_val);
62 }
63
64 bool HeaderValue::isBool() const
65 {
66 return std::holds_alternative<bool>(*_val);
67 }
68
69 const std::string &HeaderValue::asString() const
70 {
71 return std::get<std::string>(*_val);
72 }
73
74 int32_t HeaderValue::asInt() const
75 {
76 return std::get<int32_t>(*_val);
77 }
78
79 int64_t HeaderValue::asInt64() const
80 {
81 if ( std::holds_alternative<int32_t>(*_val) )
82 return std::get<int32_t>( *_val );
83 return std::get<int64_t>(*_val);
84 }
85
86 bool HeaderValue::asBool() const
87 {
88 return std::get<bool>(*_val);
89 }
90
91 HeaderValue::value_type &HeaderValue::asVariant()
92 {
93 return *_val;
94 }
95
96 const HeaderValue::value_type &HeaderValue::asVariant() const
97 {
98 return *_val;
99 }
100
101 HeaderValue &HeaderValue::operator=(const HeaderValue &other)
102 {
103 *_val = *other._val;
104 return *this;
105 }
106
107 bool HeaderValue::operator==(const HeaderValue &other) const
108 {
109 return ( *_val == *other._val );
110 }
111
112 HeaderValue &HeaderValue::operator= ( HeaderValue &&other ) noexcept
113 {
114 *_val = std::move( *other._val );
115 return *this;
116 }
117
118 HeaderValue &HeaderValue::operator= ( const std::string &val )
119 {
120 *_val = val;
121 return *this;
122 }
123
124 HeaderValue &HeaderValue::operator= ( int32_t val )
125 {
126 *_val = val;
127 return *this;
128 }
129
130 HeaderValue &HeaderValue::operator= ( int64_t val )
131 {
132 *_val = val;
133 return *this;
134 }
135
136 HeaderValue &HeaderValue::operator= ( bool val )
137 {
138 *_val = val;
139 return *this;
140 }
141
142
143 HeaderValueMap::HeaderValueMap( std::initializer_list<HeaderValueMap::ValueMap::value_type> init )
144 : _values( std::move(init) )
145 { }
146
147 bool HeaderValueMap::contains(const std::string &key) const
148 {
149 return _values.count (key) > 0 && _values.at(key).size () > 0 ;
150 }
151
152 void HeaderValueMap::set(const std::string &key, Value val)
153 {
154 auto i = _values.find (key);
155 if ( i == _values.end() ) {
156 _values.insert ( std::make_pair(key, std::vector<Value>{std::move(val)}) );
157 } else {
158 i->second = std::vector<Value>{std::move(val)};
159 }
160 }
161
162 void HeaderValueMap::set( const std::string &key, std::vector<Value> val )
163 {
164 auto i = _values.find (key);
165 if ( i == _values.end() ) {
166 _values.insert ( std::make_pair(key, std::move(val)) );
167 } else {
168 i->second = std::move(val);
169 }
170 }
171
172 void HeaderValueMap::add(const std::string &key, const Value &val)
173 {
174 auto i = _values.find (key);
175 if ( i == _values.end() ) {
176 _values.insert ( std::make_pair(key, std::vector<Value>{val}) );
177 } else {
178 i->second.push_back(val);
179 }
180 }
181
182 void HeaderValueMap::clear()
183 {
184 _values.clear();
185 }
186
187 HeaderValueMap::ValueMap::size_type HeaderValueMap::size() const noexcept
188 {
189 return _values.size();
190 }
191
192 std::vector<HeaderValueMap::Value> &HeaderValueMap::values(const std::string &key)
193 {
194 return _values[key];
195 }
196
197 const std::vector<HeaderValueMap::Value> &HeaderValueMap::values(const std::string &key) const
198 {
199 return _values.at(key);
200 }
201
202 HeaderValueMap::Value HeaderValueMap::value ( const std::string_view &str, const HeaderValueMap::Value &defaultVal) const
203 { return value( std::string(str), defaultVal ); }
204
205 HeaderValueMap::Value HeaderValueMap::value ( const std::string &str, const HeaderValueMap::Value &defaultVal) const
206 {
207 if ( !contains(str) || !_values.at(str).size() )
208 return defaultVal;
209 return _values.at(str).back();
210 }
211
212 HeaderValueMap::Value &HeaderValueMap::operator[](const std::string &key)
213 {
214 if ( !contains(key) )
215 return InvalidValue;
216 return _values[key].back();
217 }
218
219 HeaderValueMap::Value &HeaderValueMap::operator[]( const std::string_view &key )
220 {
221 return (*this)[std::string(key)];
222 }
223
224 const HeaderValueMap::Value &HeaderValueMap::operator[]( const std::string &key ) const
225 {
226 if ( !contains(key) )
227 return InvalidValue;
228 return _values.at(key).back();
229 }
230
231 const HeaderValueMap::Value &HeaderValueMap::operator[]( const std::string_view &key ) const
232 {
233 return (*this)[std::string(key)];
234 }
235
236 HeaderValueMap::const_iterator HeaderValueMap::erase(const const_iterator &i)
237 {
238 auto yi = _values.erase(i.base());
239 return HeaderValueMap::const_iterator(yi);
240 }
241
242 bool HeaderValueMap::erase(const std::string &key)
243 {
244 return ( _values.erase(key) > 0 );
245 }
246
247}
zyppng::HeaderValueMap::const_iterator
Definition headervaluemap.h:82
zyppng::HeaderValueMap::InvalidValue
static Value InvalidValue
Definition headervaluemap.h:72
zyppng::HeaderValueMap::add
void add(const std::string &key, const Value &val)
Definition headervaluemap.cc:172
zyppng::HeaderValueMap::operator[]
Value & operator[](const std::string &key)
Definition headervaluemap.cc:212
zyppng::HeaderValueMap::erase
const_iterator erase(const const_iterator &i)
Definition headervaluemap.cc:236
zyppng::HeaderValueMap::Value
HeaderValue Value
Definition headervaluemap.h:69
zyppng::HeaderValueMap::clear
void clear()
Definition headervaluemap.cc:182
zyppng::HeaderValueMap::_values
ValueMap _values
Definition headervaluemap.h:186
zyppng::HeaderValueMap::value
Value value(const std::string_view &str, const Value &defaultVal=Value()) const
Definition headervaluemap.cc:202
zyppng::HeaderValueMap::contains
bool contains(const std::string &key) const
Definition headervaluemap.cc:147
zyppng::HeaderValueMap::HeaderValueMap
HeaderValueMap()=default
zyppng::HeaderValueMap::size
ValueMap::size_type size() const noexcept
Definition headervaluemap.cc:187
zyppng::HeaderValueMap::set
void set(const std::string &key, Value val)
Definition headervaluemap.cc:152
zyppng::HeaderValueMap::values
std::vector< Value > & values(const std::string &key)
Definition headervaluemap.cc:192
zyppng::HeaderValue::asVariant
value_type & asVariant()
Definition headervaluemap.cc:91
zyppng::HeaderValue::operator==
bool operator==(const HeaderValue &other) const
Definition headervaluemap.cc:107
zyppng::HeaderValue::HeaderValue
HeaderValue()
Definition headervaluemap.cc:8
zyppng::HeaderValue::_val
zypp::RWCOW_pointer< value_type > _val
Definition headervaluemap.h:63
zyppng::HeaderValue::operator=
HeaderValue & operator=(const HeaderValue &other)
Definition headervaluemap.cc:101
zyppng::HeaderValue::asInt
int32_t asInt() const
Definition headervaluemap.cc:74
zyppng::HeaderValue::isInt
bool isInt() const
Definition headervaluemap.cc:54
zyppng::HeaderValue::isInt64
bool isInt64() const
Definition headervaluemap.cc:59
zyppng::HeaderValue::value_type
std::variant< std::monostate, std::string, int32_t, int64_t, bool > value_type
Definition headervaluemap.h:23
zyppng::HeaderValue::asString
const std::string & asString() const
Definition headervaluemap.cc:69
zyppng::HeaderValue::isString
bool isString() const
Definition headervaluemap.cc:49
zyppng::HeaderValue::asBool
bool asBool() const
Definition headervaluemap.cc:86
zyppng::HeaderValue::valid
bool valid() const
Definition headervaluemap.cc:44
zyppng::HeaderValue::isBool
bool isBool() const
Definition headervaluemap.cc:64
zyppng::HeaderValue::asInt64
int64_t asInt64() const
Definition headervaluemap.cc:79
headervaluemap.h
std
Definition Arch.h:364
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zyppng
Definition MediaCurl2.h:22
String.h