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