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

libzypp 17.38.14

headervaluemap.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPP_MEDIA_NG_HEADERVALUEMAP_H_INCLUDED
10#define ZYPP_MEDIA_NG_HEADERVALUEMAP_H_INCLUDED
11
12#include <variant>
13#include <string>
14#include <map>
15#include <zypp-core/base/PtrTypes.h>
16
17namespace zyppng {
18
19 class HeaderValue
20 {
21 public:
22 using value_type = std::variant<std::monostate, std::string, int32_t, int64_t, bool>;
23
24 HeaderValue();
25
26 HeaderValue( const HeaderValue &other );
27 HeaderValue( HeaderValue &&other ) noexcept;
28
29 HeaderValue( const bool val );
30 HeaderValue( const int32_t val );
31 HeaderValue( const int64_t val );
32 HeaderValue( std::string &&val );
33 HeaderValue( const std::string &val );
34 HeaderValue( const char *val );
35
36 bool valid () const;
37
38 bool isString () const;
39 bool isInt () const;
40 bool isInt64 () const;
41 bool isDouble () const;
42 bool isBool () const;
43
44 const std::string &asString () const;
45 int32_t asInt () const;
46 int64_t asInt64 () const;
47 bool asBool () const;
48
49 value_type &asVariant ();
50 const value_type &asVariant () const;
51
52 HeaderValue &operator= ( const HeaderValue &other );
53 HeaderValue &operator= ( HeaderValue &&other ) noexcept;
54 HeaderValue &operator= ( const std::string &val );
55 HeaderValue &operator= ( int32_t val );
56 HeaderValue &operator= ( int64_t val );
57 HeaderValue &operator= ( bool val );
58
59 bool operator== ( const HeaderValue &other ) const;
60
61 private:
62 zypp::RWCOW_pointer<value_type> _val;
63 };
64
65 class HeaderValueMap
66 {
67 public:
68 using Value = HeaderValue;
69 using ValueMap = std::map<std::string, std::vector<Value>>;
70
71 static Value InvalidValue;
72
73 class const_iterator
74 {
75 public:
76 using iterator_category = std::forward_iterator_tag;
77 using value_type = std::pair<std::string, Value>;
78 using difference_type = std::ptrdiff_t;
79 using pointer = void;
80 using reference = const std::pair<const std::string&, const Value&>;
81
82 private:
83 ValueMap::const_iterator _it;
84
85 public:
86 const_iterator() = default;
87
88 explicit const_iterator( const ValueMap::const_iterator &val )
89 : _it(val) {}
90
91 const_iterator( const HeaderValueMap::const_iterator &other )
92 : _it(other.base()) {}
93
94 const std::string &key () const {
95 return _it->first;
96 }
97
98 const Value &value() const {
99 auto &l = _it->second;
100 if ( l.empty() ) {
101 return InvalidValue;
102 }
103 return l.back();
104 }
105
106 reference operator*() const {
107 return reference( key(), value() );
108 }
109
110 const_iterator& operator++() {
111 ++_it;
112 return *this;
113 }
114
115 const_iterator operator++(int) {
116 const_iterator tmp = *this;
117 ++_it;
118 return tmp;
119 }
120
121 bool operator==(const const_iterator& other) const {
122 return _it == other._it;
123 }
124
125 bool operator!=(const const_iterator& other) const {
126 return !(*this == other);
127 }
128
129 const ValueMap::const_iterator& base() const { return _it; }
130 };
131
132 HeaderValueMap() = default;
133 HeaderValueMap( std::initializer_list<ValueMap::value_type> init );
134
135 bool contains( const std::string &key ) const;
136 bool contains( const std::string_view &key ) const {
137 return contains(std::string(key));
138 }
139
140 void set( const std::string &key, Value val );
141 void set( const std::string &key, std::vector<Value> val );
142 void add( const std::string &key, const Value &val);
143 void clear ();
144 ValueMap::size_type size() const noexcept;
145
146 std::vector<Value> &values ( const std::string &key );
147 const std::vector<Value> &values ( const std::string &key ) const;
148
149 std::vector<Value> &values ( const std::string_view &key ) {
150 return values( std::string(key) );
151 }
152
153 const std::vector<Value> &values ( const std::string_view &key ) const {
154 return values( std::string(key) );
155 }
156
161 Value value ( const std::string_view &str, const Value &defaultVal = Value() ) const;
162 Value value ( const std::string &str, const Value &defaultVal = Value() ) const;
163
164 Value &operator[]( const std::string &key );
165 Value &operator[]( const std::string_view &key );
166 const Value &operator[]( const std::string &key ) const;
167 const Value &operator[]( const std::string_view &key ) const;
168
169 const_iterator erase( const const_iterator &i );
170 bool erase( const std::string &key );
171
172 const_iterator begin() const {
173 return const_iterator( _values.begin() );
174 }
175 const_iterator end() const {
176 return const_iterator( _values.end() );
177 }
178
179 ValueMap::iterator beginList() {
180 return _values.begin();
181 }
182 ValueMap::iterator endList() {
183 return _values.end();
184 }
185
186 ValueMap::const_iterator beginList() const {
187 return _values.begin();
188 }
189 ValueMap::const_iterator endList() const {
190 return _values.end();
191 }
192
193 ValueMap::const_iterator cbeginList() const {
194 return _values.cbegin();
195 }
196 ValueMap::const_iterator cendList() const {
197 return _values.cend();
198 }
199
200 private:
201 ValueMap _values;
202 };
203}
204
205namespace zypp {
206 template<>
207 inline zyppng::HeaderValue::value_type* rwcowClone<zyppng::HeaderValue::value_type>( const zyppng::HeaderValue::value_type * rhs )
208 { return new zyppng::HeaderValue::value_type(*rhs); }
209}
210
211
212#endif
PtrTypes.h
zyppng::HeaderValueMap::const_iterator
Definition headervaluemap.h:74
zyppng::HeaderValueMap::const_iterator::value
const Value & value() const
Definition headervaluemap.h:98
zyppng::HeaderValueMap::const_iterator::pointer
void pointer
Definition headervaluemap.h:79
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator()=default
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator(const HeaderValueMap::const_iterator &other)
Definition headervaluemap.h:91
zyppng::HeaderValueMap::const_iterator::reference
const std::pair< const std::string &, const Value & > reference
Definition headervaluemap.h:80
zyppng::HeaderValueMap::const_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition headervaluemap.h:76
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator(const ValueMap::const_iterator &val)
Definition headervaluemap.h:88
zyppng::HeaderValueMap::const_iterator::value_type
std::pair< std::string, Value > value_type
Definition headervaluemap.h:77
zyppng::HeaderValueMap::const_iterator::operator!=
bool operator!=(const const_iterator &other) const
Definition headervaluemap.h:125
zyppng::HeaderValueMap::const_iterator::difference_type
std::ptrdiff_t difference_type
Definition headervaluemap.h:78
zyppng::HeaderValueMap::const_iterator::_it
ValueMap::const_iterator _it
Definition headervaluemap.h:83
zyppng::HeaderValueMap::const_iterator::base
const ValueMap::const_iterator & base() const
Definition headervaluemap.h:129
zyppng::HeaderValueMap::const_iterator::operator*
reference operator*() const
Definition headervaluemap.h:106
zyppng::HeaderValueMap::const_iterator::operator++
const_iterator & operator++()
Definition headervaluemap.h:110
zyppng::HeaderValueMap::const_iterator::operator==
bool operator==(const const_iterator &other) const
Definition headervaluemap.h:121
zyppng::HeaderValueMap::const_iterator::operator++
const_iterator operator++(int)
Definition headervaluemap.h:115
zyppng::HeaderValueMap::const_iterator::key
const std::string & key() const
Definition headervaluemap.h:94
zyppng::HeaderValueMap::HeaderValueMap
HeaderValueMap(std::initializer_list< ValueMap::value_type > init)
zyppng::HeaderValueMap::InvalidValue
static Value InvalidValue
Definition headervaluemap.h:71
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::ValueMap
std::map< std::string, std::vector< Value > > ValueMap
Definition headervaluemap.h:69
zyppng::HeaderValueMap::beginList
ValueMap::const_iterator beginList() const
Definition headervaluemap.h:186
zyppng::HeaderValueMap::cendList
ValueMap::const_iterator cendList() const
Definition headervaluemap.h:196
zyppng::HeaderValueMap::Value
HeaderValue Value
Definition headervaluemap.h:68
zyppng::HeaderValueMap::clear
void clear()
Definition headervaluemap.cc:182
zyppng::HeaderValueMap::beginList
ValueMap::iterator beginList()
Definition headervaluemap.h:179
zyppng::HeaderValueMap::values
const std::vector< Value > & values(const std::string_view &key) const
Definition headervaluemap.h:153
zyppng::HeaderValueMap::_values
ValueMap _values
Definition headervaluemap.h:201
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::endList
ValueMap::const_iterator endList() const
Definition headervaluemap.h:189
zyppng::HeaderValueMap::begin
const_iterator begin() const
Definition headervaluemap.h:172
zyppng::HeaderValueMap::HeaderValueMap
HeaderValueMap()=default
zyppng::HeaderValueMap::endList
ValueMap::iterator endList()
Definition headervaluemap.h:182
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::contains
bool contains(const std::string_view &key) const
Definition headervaluemap.h:136
zyppng::HeaderValueMap::values
std::vector< Value > & values(const std::string &key)
Definition headervaluemap.cc:192
zyppng::HeaderValueMap::cbeginList
ValueMap::const_iterator cbeginList() const
Definition headervaluemap.h:193
zyppng::HeaderValueMap::end
const_iterator end() const
Definition headervaluemap.h:175
zyppng::HeaderValue
Definition headervaluemap.h:20
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:62
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:22
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::isDouble
bool isDouble() const
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
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::rwcowClone< zyppng::HeaderValue::value_type >
zyppng::HeaderValue::value_type * rwcowClone< zyppng::HeaderValue::value_type >(const zyppng::HeaderValue::value_type *rhs)
Definition headervaluemap.h:207
zyppng
Definition MediaNetworkRequestExecutor.h:16
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469