libzypp: zypp/UserData.h Source File

libzypp 17.25.10

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
23 namespace zypp
24 {
26 namespace callback
27 {
38 class UserData
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
50 typedef zypp::ContentType ContentType;
51
52 public:
54 UserData()
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
89 size_type size() const
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:
220 DataType & dataRef() const
221 { if ( ! _dataP ) _dataP.reset( new DataType ); return *_dataP; }
222
223 private:
224 ContentType _type;
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
ContentType.h
PtrTypes.h
zypp::ContentType
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:30
zypp::callback::UserData
Typesafe passing of user data via callbacks.
Definition: UserData.h:39
zypp::callback::UserData::const_iterator
DataType::const_iterator const_iterator
Definition: UserData.h:48
zypp::callback::UserData::AnyType
boost::any AnyType
Definition: UserData.h:41
zypp::callback::UserData::dataRef
DataType & dataRef() const
Definition: UserData.h:220
zypp::callback::UserData::reset
bool reset(const std::string &key_r)
Set an empty value for key_r (if possible).
Definition: UserData.h:134
zypp::callback::UserData::type
void type(ContentType type_r)
Set type.
Definition: UserData.h:76
zypp::callback::UserData::UserData
UserData()
Default ctor.
Definition: UserData.h:54
zypp::callback::UserData::_dataP
shared_ptr< DataType > _dataP
Definition: UserData.h:225
zypp::callback::UserData::UserData
UserData(ContentType type_r)
Ctor taking ContentType.
Definition: UserData.h:58
zypp::callback::UserData::size_type
DataType::size_type size_type
Definition: UserData.h:45
zypp::callback::UserData::type
const ContentType & type() const
Get type.
Definition: UserData.h:72
zypp::callback::UserData::hasvalue
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
Definition: UserData.h:101
zypp::callback::UserData::get
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
zypp::callback::UserData::erase
void erase(const std::string &key_r)
Remove key from data.
Definition: UserData.h:141
zypp::callback::UserData::bad_AnyType_cast
boost::bad_any_cast bad_AnyType_cast
Definition: UserData.h:42
zypp::callback::UserData::size
size_type size() const
Size of data.
Definition: UserData.h:89
zypp::callback::UserData::key_type
DataType::key_type key_type
Definition: UserData.h:46
zypp::callback::UserData::_type
ContentType _type
Definition: UserData.h:224
zypp::callback::UserData::operator<<
std::ostream & operator<<(std::ostream &str, const UserData &obj)
Stream output.
Definition: UserData.h:229
zypp::callback::UserData::set
bool set(const std::string &key_r, AnyType val_r)
Set the value for key (nonconst version always returns true).
Definition: UserData.h:118
zypp::callback::UserData::getvalue
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
zypp::callback::UserData::set
bool set(const std::string &key_r, AnyType val_r) const
Definition: UserData.h:121
zypp::callback::UserData::value_type
DataType::value_type value_type
Definition: UserData.h:47
zypp::callback::UserData::haskey
bool haskey(const std::string &key_r) const
Whether key_r is in data.
Definition: UserData.h:97
zypp::callback::UserData::UserData
UserData(std::string type_r, std::string subtype_r)
Ctor taking ContentType.
Definition: UserData.h:66
zypp::callback::UserData::ContentType
zypp::ContentType ContentType
Definition: UserData.h:50
zypp::callback::UserData::get
const Tp & get(const std::string &key_r) const
Pass back a const Tp & reference to key_r value.
Definition: UserData.h:175
zypp::callback::UserData::DataType
std::map< std::string, AnyType > DataType
Definition: UserData.h:44
zypp::callback::UserData::UserData
UserData(std::string type_r)
Ctor taking ContentType.
Definition: UserData.h:62
zypp::callback::UserData::data
const DataType & data() const
The data.
Definition: UserData.h:93
zypp::callback::UserData::empty
bool empty() const
Whether data is empty.
Definition: UserData.h:85
zypp::callback::UserData::get
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
zypp::callback::UserData::reset
bool reset(const std::string &key_r) const
Definition: UserData.h:137
std
Definition: Arch.h:348
str
String related utilities and Regular expression matching.
zypp::sat::detail::size_type
SolvableIdType size_type
Definition: PoolMember.h:126
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2