libzypp 17.31.23
UserData.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11#ifndef ZYPP_USERDATA_H
12#define ZYPP_USERDATA_H
13
14#include <iosfwd>
15#include <string>
16#include <map>
17#include <boost/any.hpp>
18
19#include <zypp/base/PtrTypes.h>
20#include <zypp/ContentType.h>
21
23namespace zypp
24{
26 namespace callback
27 {
39 {
40 public:
41 typedef boost::any AnyType;
42 typedef boost::bad_any_cast bad_AnyType_cast;
43
44 typedef std::map<std::string,AnyType> DataType;
45 typedef DataType::size_type size_type;
46 typedef DataType::key_type key_type;
47 typedef DataType::value_type value_type;
48 typedef DataType::const_iterator const_iterator;
49
51
52 public:
55 {}
56
58 explicit UserData( ContentType type_r )
59 : _type( std::move(type_r) )
60 {}
62 explicit UserData( std::string type_r )
63 : UserData( ContentType( std::move(type_r) ) )
64 {}
66 UserData( std::string type_r, std::string subtype_r )
67 : UserData( ContentType( std::move(type_r), std::move(subtype_r) ) )
68 {}
69
70 public:
72 const ContentType & type() const
73 { return _type; }
74
76 void type( ContentType type_r )
77 { _type = std::move(type_r); }
78
79 public:
81 explicit operator bool() const
82 { return !empty(); }
83
85 bool empty() const
86 { return !_dataP || _dataP->empty(); }
87
90 { return _dataP ? _dataP->size() : 0; }
91
93 const DataType & data() const
94 { return dataRef(); }
95
97 bool haskey( const std::string & key_r ) const
98 { return _dataP && _dataP->find( key_r ) != _dataP->end(); }
99
101 bool hasvalue( const std::string & key_r ) const
102 {
103 bool ret = false;
104 if ( _dataP )
105 {
106 const_iterator it = _dataP->find( key_r );
107 if ( it != _dataP->end() && ! it->second.empty() )
108 {
109 ret = true;
110 }
111 }
112 return ret;
113 }
114
118 bool set( const std::string & key_r, AnyType val_r )
119 { dataRef()[key_r] = std::move(val_r); return true; }
121 bool set( const std::string & key_r, AnyType val_r ) const
122 {
123 bool ret = false;
124 AnyType & val( dataRef()[key_r] );
125 if ( val.empty() )
126 {
127 val = std::move(val_r);
128 ret = true;
129 }
130 return ret;
131 }
132
134 bool reset( const std::string & key_r )
135 { return set( key_r, AnyType() ); }
137 bool reset( const std::string & key_r ) const
138 { return set( key_r, AnyType() ); }
139
141 void erase( const std::string & key_r )
142 { if ( _dataP ) _dataP->erase( key_r ); }
143
145 const AnyType & getvalue( const std::string & key_r ) const
146 {
147 if ( _dataP )
148 {
149 const_iterator it = _dataP->find( key_r );
150 if ( it != _dataP->end() )
151 {
152 return it->second;
153 }
154 }
155 static const AnyType none;
156 return none;
157 }
158
174 template <class Tp>
175 const Tp & get( const std::string & key_r ) const
176 { return boost::any_cast<const Tp &>( getvalue( key_r ) ); }
177
185 template <class Tp>
186 Tp get( const std::string & key_r, const Tp & default_r ) const
187 { Tp ret( default_r ); get( key_r, ret ); return ret; }
188
199 template <class Tp>
200 bool get( const std::string & key_r, Tp & ret_r ) const
201 {
202 bool ret = false;
203 if ( _dataP )
204 {
205 const_iterator it = _dataP->find( key_r );
206 if ( it != _dataP->end() )
207 {
208 auto ptr = boost::any_cast<const Tp>(&it->second);
209 if ( ptr )
210 {
211 ret_r = *ptr;
212 ret = true;
213 }
214 }
215 }
216 return ret;
217 }
218
219 private:
221 { if ( ! _dataP ) _dataP.reset( new DataType ); return *_dataP; }
222
223 private:
225 mutable shared_ptr<DataType> _dataP;
226 };
227
229 inline std::ostream & operator<<( std::ostream & str, const UserData & obj )
230 { return str << "UserData(" << obj.type() << ":" << obj.size() << ")";}
231
232 } // namespace callback
234} // namespace zypp
236#endif // ZYPP_USERDATA_H
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:30
Typesafe passing of user data via callbacks.
Definition: UserData.h:39
DataType::const_iterator const_iterator
Definition: UserData.h:48
bool reset(const std::string &key_r)
Set an empty value for key_r (if possible).
Definition: UserData.h:134
void type(ContentType type_r)
Set type.
Definition: UserData.h:76
const Tp & get(const std::string &key_r) const
Pass back a const Tp & reference to key_r value.
Definition: UserData.h:175
UserData()
Default ctor.
Definition: UserData.h:54
const DataType & data() const
The data.
Definition: UserData.h:93
shared_ptr< DataType > _dataP
Definition: UserData.h:225
const ContentType & type() const
Get type.
Definition: UserData.h:72
UserData(ContentType type_r)
Ctor taking ContentType.
Definition: UserData.h:58
DataType & dataRef() const
Definition: UserData.h:220
DataType::size_type size_type
Definition: UserData.h:45
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
Definition: UserData.h:101
bool get(const std::string &key_r, Tp &ret_r) const
If the value for key_r is of the same type as ret_r, pass it back in ret_r and return true;.
Definition: UserData.h:200
void erase(const std::string &key_r)
Remove key from data.
Definition: UserData.h:141
boost::bad_any_cast bad_AnyType_cast
Definition: UserData.h:42
size_type size() const
Size of data.
Definition: UserData.h:89
DataType::key_type key_type
Definition: UserData.h:46
std::ostream & operator<<(std::ostream &str, const UserData &obj)
Stream output.
Definition: UserData.h:229
bool set(const std::string &key_r, AnyType val_r)
Set the value for key (nonconst version always returns true).
Definition: UserData.h:118
bool set(const std::string &key_r, AnyType val_r) const
Definition: UserData.h:121
DataType::value_type value_type
Definition: UserData.h:47
bool haskey(const std::string &key_r) const
Whether key_r is in data.
Definition: UserData.h:97
UserData(std::string type_r, std::string subtype_r)
Ctor taking ContentType.
Definition: UserData.h:66
zypp::ContentType ContentType
Definition: UserData.h:50
std::map< std::string, AnyType > DataType
Definition: UserData.h:44
UserData(std::string type_r)
Ctor taking ContentType.
Definition: UserData.h:62
bool empty() const
Whether data is empty.
Definition: UserData.h:85
const AnyType & getvalue(const std::string &key_r) const
get helper returning the keys AnyType value or an empty value if key does not exist.
Definition: UserData.h:145
Tp get(const std::string &key_r, const Tp &default_r) const
Pass back a Tp copy of key_r value.
Definition: UserData.h:186
bool reset(const std::string &key_r) const
Definition: UserData.h:137
Definition: Arch.h:361
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2