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

libzypp 17.36.6

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 <boost/iterator/iterator_adaptor.hpp>
16#include <zypp-core/base/PtrTypes.h>
17
18namespace zyppng {
19
20 class HeaderValue
21 {
22 public:
23 using value_type = std::variant<std::monostate, std::string, int32_t, int64_t, bool>;
24
25 HeaderValue();
26
27 HeaderValue( const HeaderValue &other );
28 HeaderValue( HeaderValue &&other ) noexcept;
29
30 HeaderValue( const bool val );
31 HeaderValue( const int32_t val );
32 HeaderValue( const int64_t val );
33 HeaderValue( std::string &&val );
34 HeaderValue( const std::string &val );
35 HeaderValue( const char *val );
36
37 bool valid () const;
38
39 bool isString () const;
40 bool isInt () const;
41 bool isInt64 () const;
42 bool isDouble () const;
43 bool isBool () const;
44
45 const std::string &asString () const;
46 int32_t asInt () const;
47 int64_t asInt64 () const;
48 bool asBool () const;
49
50 value_type &asVariant ();
51 const value_type &asVariant () const;
52
53 HeaderValue &operator= ( const HeaderValue &other );
54 HeaderValue &operator= ( HeaderValue &&other ) noexcept;
55 HeaderValue &operator= ( const std::string &val );
56 HeaderValue &operator= ( int32_t val );
57 HeaderValue &operator= ( int64_t val );
58 HeaderValue &operator= ( bool val );
59
60 bool operator== ( const HeaderValue &other ) const;
61
62 private:
63 zypp::RWCOW_pointer<value_type> _val;
64 };
65
66 class HeaderValueMap
67 {
68 public:
69 using Value = HeaderValue;
70 using ValueMap = std::map<std::string, std::vector<Value>>;
71
72 static Value InvalidValue;
73
74 class const_iterator
75 : public boost::iterator_adaptor<
76 HeaderValueMap::const_iterator // Derived
77 , ValueMap::const_iterator // Base
78 , std::pair<std::string, Value> // Value
79 , boost::use_default // CategoryOrTraversal
80 , const std::pair<const std::string&, const Value&> // Reference
81 >
82 {
83 public:
84 const_iterator()
85 : const_iterator::iterator_adaptor_() {}
86
87 explicit const_iterator( const ValueMap::const_iterator &val )
88 { this->base_reference() = val; }
89
90 const_iterator( const HeaderValueMap::const_iterator &other )
91 : const_iterator::iterator_adaptor_( other.base() ) {}
92
93 const std::string &key () const {
94 return this->base_reference()->first;
95 }
96
97 const Value &value() const {
98 auto &l = base_reference ()->second;
99 if ( l.empty() ) {
100 return InvalidValue;
101 }
102 return l.back();
103 }
104
105 private:
106 friend class boost::iterator_core_access;
107 void increment() {
108 this->base_reference() = ++this->base_reference();
109 }
110
111 std::pair<const std::string&, const Value&> dereference() const
112 {
113 return std::pair<const std::string&, const Value&>( key(), value() );
114 }
115 };
116
117 HeaderValueMap() = default;
118 HeaderValueMap( std::initializer_list<ValueMap::value_type> init );
119
120 bool contains( const std::string &key ) const;
121 bool contains( const std::string_view &key ) const {
122 return contains(std::string(key));
123 }
124
125 void set( const std::string &key, Value val );
126 void set( const std::string &key, std::vector<Value> val );
127 void add( const std::string &key, const Value &val);
128 void clear ();
129 ValueMap::size_type size() const noexcept;
130
131 std::vector<Value> &values ( const std::string &key );
132 const std::vector<Value> &values ( const std::string &key ) const;
133
134 std::vector<Value> &values ( const std::string_view &key ) {
135 return values( std::string(key) );
136 }
137
138 const std::vector<Value> &values ( const std::string_view &key ) const {
139 return values( std::string(key) );
140 }
141
146 Value value ( const std::string_view &str, const Value &defaultVal = Value() ) const;
147 Value value ( const std::string &str, const Value &defaultVal = Value() ) const;
148
149 Value &operator[]( const std::string &key );
150 Value &operator[]( const std::string_view &key );
151 const Value &operator[]( const std::string &key ) const;
152 const Value &operator[]( const std::string_view &key ) const;
153
154 const_iterator erase( const const_iterator &i );
155 bool erase( const std::string &key );
156
157 const_iterator begin() const {
158 return const_iterator( _values.begin() );
159 }
160 const_iterator end() const {
161 return const_iterator( _values.end() );
162 }
163
164 ValueMap::iterator beginList() {
165 return _values.begin();
166 }
167 ValueMap::iterator endList() {
168 return _values.end();
169 }
170
171 ValueMap::const_iterator beginList() const {
172 return _values.begin();
173 }
174 ValueMap::const_iterator endList() const {
175 return _values.end();
176 }
177
178 ValueMap::const_iterator cbeginList() const {
179 return _values.cbegin();
180 }
181 ValueMap::const_iterator cendList() const {
182 return _values.cend();
183 }
184
185 private:
186 ValueMap _values;
187 };
188}
189
190namespace zypp {
191 template<>
192 inline zyppng::HeaderValue::value_type* rwcowClone<zyppng::HeaderValue::value_type>( const zyppng::HeaderValue::value_type * rhs )
193 { return new zyppng::HeaderValue::value_type(*rhs); }
194}
195
196
197#endif
zyppng::HeaderValueMap::const_iterator
Definition headervaluemap.h:82
zyppng::HeaderValueMap::const_iterator::value
const Value & value() const
Definition headervaluemap.h:97
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator(const HeaderValueMap::const_iterator &other)
Definition headervaluemap.h:90
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator(const ValueMap::const_iterator &val)
Definition headervaluemap.h:87
zyppng::HeaderValueMap::const_iterator::const_iterator
const_iterator()
Definition headervaluemap.h:84
zyppng::HeaderValueMap::const_iterator::dereference
std::pair< const std::string &, const Value & > dereference() const
Definition headervaluemap.h:111
zyppng::HeaderValueMap::const_iterator::iterator_core_access
friend class boost::iterator_core_access
Definition headervaluemap.h:106
zyppng::HeaderValueMap::const_iterator::increment
void increment()
Definition headervaluemap.h:107
zyppng::HeaderValueMap::const_iterator::key
const std::string & key() const
Definition headervaluemap.h:93
zyppng::HeaderValueMap::HeaderValueMap
HeaderValueMap(std::initializer_list< ValueMap::value_type > init)
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::ValueMap
std::map< std::string, std::vector< Value > > ValueMap
Definition headervaluemap.h:70
zyppng::HeaderValueMap::beginList
ValueMap::const_iterator beginList() const
Definition headervaluemap.h:171
zyppng::HeaderValueMap::cendList
ValueMap::const_iterator cendList() const
Definition headervaluemap.h:181
zyppng::HeaderValueMap::Value
HeaderValue Value
Definition headervaluemap.h:69
zyppng::HeaderValueMap::clear
void clear()
Definition headervaluemap.cc:182
zyppng::HeaderValueMap::beginList
ValueMap::iterator beginList()
Definition headervaluemap.h:164
zyppng::HeaderValueMap::values
const std::vector< Value > & values(const std::string_view &key) const
Definition headervaluemap.h:138
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::endList
ValueMap::const_iterator endList() const
Definition headervaluemap.h:174
zyppng::HeaderValueMap::begin
const_iterator begin() const
Definition headervaluemap.h:157
zyppng::HeaderValueMap::HeaderValueMap
HeaderValueMap()=default
zyppng::HeaderValueMap::endList
ValueMap::iterator endList()
Definition headervaluemap.h:167
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:121
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:178
zyppng::HeaderValueMap::end
const_iterator end() const
Definition headervaluemap.h:160
zyppng::HeaderValue
Definition headervaluemap.h:21
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::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 Arch.h:364
str
String related utilities and Regular expression matching.
zypp::base
Definition Random.cc:7
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::rwcowClone< zyppng::HeaderValue::value_type >
zyppng::HeaderValue::value_type * rwcowClone< zyppng::HeaderValue::value_type >(const zyppng::HeaderValue::value_type *rhs)
Definition headervaluemap.h:192
zyppng
Definition MediaCurl2.h:22
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469
PtrTypes.h