libzypp  17.31.31
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 
18 namespace zyppng {
19 
21  {
22  public:
23  using value_type = std::variant<std::monostate, std::string, int32_t, int64_t, double, bool>;
24 
25  HeaderValue();
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:
72  using Value = HeaderValue;
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  }
162  const_iterator end() const {
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 
192 namespace zypp {
193  template<>
194  inline zyppng::HeaderValue::value_type* rwcowClone<zyppng::HeaderValue::value_type>( const zyppng::HeaderValue::value_type * rhs )
195  { return new zyppng::HeaderValue::value_type(*rhs); }
196 }
197 
198 
199 #endif
bool operator==(const HeaderValue &other) const
void add(const std::string &key, const Value &val)
std::variant< std::monostate, std::string, int32_t, int64_t, double, bool > value_type
const std::string & asString() const
bool isDouble() const
const_iterator(const HeaderValueMap::const_iterator &other)
const_iterator begin() const
ValueMap::size_type size() const noexcept
HeaderValue & operator=(const HeaderValue &other)
const_iterator end() const
ValueMap::const_iterator cbeginList() const
std::map< std::string, std::vector< Value > > ValueMap
const std::string & key() const
String related utilities and Regular expression matching.
zypp::RWCOW_pointer< value_type > _val
ValueMap::iterator endList()
Definition: Arch.h:363
bool isString() const
int64_t asInt64() const
const_iterator erase(const const_iterator &i)
std::vector< Value > & values(const std::string &key)
Value value(const std::string_view &str, const Value &defaultVal=Value()) const
const_iterator(const ValueMap::const_iterator &val)
const std::vector< Value > & values(const std::string_view &key) const
ValueMap::iterator beginList()
SolvableIdType size_type
Definition: PoolMember.h:126
std::pair< std::string, Value > dereference() const
static Value InvalidValue
value_type & asVariant()
bool isInt64() const
ValueMap::const_iterator endList() const
int32_t asInt() const
Value & operator[](const std::string &key)
ValueMap::const_iterator beginList() const
bool contains(const std::string_view &key) const
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
double asDouble() const
ValueMap::const_iterator cendList() const
bool contains(const std::string &key) const