libzypp: zypp-core/UserData.h Source File

libzypp 17.38.15

UserData.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11#ifndef ZYPP_CORE_USERDATA_H
12#define ZYPP_CORE_USERDATA_H
13
14#include <iosfwd>
15#include <string>
16#include <map>
17#include <boost/any.hpp>
18#include <ostream>
19
20#include <zypp-core/base/PtrTypes.h>
21#include <zypp-core/ContentType.h>
22
24namespace zypp
25{
27 namespace callback
28 {
39 class UserData
40 {
41 public:
42 typedef boost::any AnyType;
43 typedef boost::bad_any_cast bad_AnyType_cast;
44
45 typedef std::map<std::string,AnyType> DataType;
46 typedef DataType::size_type size_type;
47 typedef DataType::key_type key_type;
48 typedef DataType::value_type value_type;
49 typedef DataType::const_iterator const_iterator;
50
51 typedef zypp::ContentType ContentType;
52
53 public:
55 UserData()
56 {}
57
59 explicit UserData( ContentType type_r )
60 : _type( std::move(type_r) )
61 {}
62
63 explicit UserData( std::string type_r )
64 : UserData( ContentType( std::move(type_r) ) )
65 {}
66
67 UserData( std::string type_r, std::string subtype_r )
68 : UserData( ContentType( std::move(type_r), std::move(subtype_r) ) )
69 {}
70
71 public:
73 const ContentType & type() const
74 { return _type; }
75
77 void type( ContentType type_r )
78 { _type = std::move(type_r); }
79
80 public:
82 explicit operator bool() const
83 { return !empty(); }
84
86 bool empty() const
87 { return !_dataP || _dataP->empty(); }
88
90 size_type size() const
91 { return _dataP ? _dataP->size() : 0; }
92
94 const DataType & data() const
95 { return dataRef(); }
96
98 bool haskey( const std::string & key_r ) const
99 { return _dataP && _dataP->find( key_r ) != _dataP->end(); }
100
102 bool hasvalue( const std::string & key_r ) const
103 {
104 bool ret = false;
105 if ( _dataP )
106 {
107 const_iterator it = _dataP->find( key_r );
108 if ( it != _dataP->end() && ! it->second.empty() )
109 {
110 ret = true;
111 }
112 }
113 return ret;
114 }
115
119 bool set( const std::string & key_r, AnyType val_r )
120 { dataRef()[key_r] = std::move(val_r); return true; }
121
122 bool set( const std::string & key_r, AnyType val_r ) const
123 {
124 bool ret = false;
125 AnyType & val( dataRef()[key_r] );
126 if ( val.empty() )
127 {
128 val = std::move(val_r);
129 ret = true;
130 }
131 return ret;
132 }
133
135 bool reset( const std::string & key_r )
136 { return set( key_r, AnyType() ); }
137
138 bool reset( const std::string & key_r ) const
139 { return set( key_r, AnyType() ); }
140
142 void erase( const std::string & key_r )
143 { if ( _dataP ) _dataP->erase( key_r ); }
144
146 const AnyType & getvalue( const std::string & key_r ) const
147 {
148 if ( _dataP )
149 {
150 const_iterator it = _dataP->find( key_r );
151 if ( it != _dataP->end() )
152 {
153 return it->second;
154 }
155 }
156 static const AnyType none;
157 return none;
158 }
159
175 template <class Tp>
176 const Tp & get( const std::string & key_r ) const
177 { return boost::any_cast<const Tp &>( getvalue( key_r ) ); }
178
186 template <class Tp>
187 Tp get( const std::string & key_r, const Tp & default_r ) const
188 { Tp ret( default_r ); get( key_r, ret ); return ret; }
189
200 template <class Tp>
201 bool get( const std::string & key_r, Tp & ret_r ) const
202 {
203 bool ret = false;
204 if ( _dataP )
205 {
206 const_iterator it = _dataP->find( key_r );
207 if ( it != _dataP->end() )
208 {
209 auto ptr = boost::any_cast<const Tp>(&it->second);
210 if ( ptr )
211 {
212 ret_r = *ptr;
213 ret = true;
214 }
215 }
216 }
217 return ret;
218 }
219
220 private:
221 DataType & dataRef() const
222 { if ( ! _dataP ) _dataP.reset( new DataType ); return *_dataP; }
223
224 private:
225 ContentType _type;
226 mutable shared_ptr<DataType> _dataP;
227 };
228
230 inline std::ostream & operator<<( std::ostream & str, const UserData & obj )
231 { return str << "UserData(" << obj.type() << ":" << obj.size() << ")";}
232
233 } // namespace callback
235} // namespace zypp
237#endif // ZYPP_CORE_USERDATA_H
ContentType.h
PtrTypes.h
bool
shared_ptr
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:40
zypp::callback::UserData::const_iterator
DataType::const_iterator const_iterator
Definition UserData.h:49
zypp::callback::UserData::AnyType
boost::any AnyType
Definition UserData.h:42
zypp::callback::UserData::reset
bool reset(const std::string &key_r)
Set an empty value for key_r (if possible).
Definition UserData.h:135
zypp::callback::UserData::type
void type(ContentType type_r)
Set type.
Definition UserData.h:77
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:176
zypp::callback::UserData::UserData
UserData()
Default ctor.
Definition UserData.h:55
zypp::callback::UserData::data
const DataType & data() const
The data.
Definition UserData.h:94
zypp::callback::UserData::_dataP
shared_ptr< DataType > _dataP
Definition UserData.h:226
zypp::callback::UserData::type
const ContentType & type() const
Get type.
Definition UserData.h:73
zypp::callback::UserData::UserData
UserData(ContentType type_r)
Ctor taking ContentType.
Definition UserData.h:59
zypp::callback::UserData::dataRef
DataType & dataRef() const
Definition UserData.h:221
zypp::callback::UserData::size_type
DataType::size_type size_type
Definition UserData.h:46
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:102
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:201
zypp::callback::UserData::erase
void erase(const std::string &key_r)
Remove key from data.
Definition UserData.h:142
zypp::callback::UserData::bad_AnyType_cast
boost::bad_any_cast bad_AnyType_cast
Definition UserData.h:43
zypp::callback::UserData::size
size_type size() const
Size of data.
Definition UserData.h:90
zypp::callback::UserData::key_type
DataType::key_type key_type
Definition UserData.h:47
zypp::callback::UserData::_type
ContentType _type
Definition UserData.h:225
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:119
zypp::callback::UserData::set
bool set(const std::string &key_r, AnyType val_r) const
Definition UserData.h:122
zypp::callback::UserData::value_type
DataType::value_type value_type
Definition UserData.h:48
zypp::callback::UserData::haskey
bool haskey(const std::string &key_r) const
Whether key_r is in data.
Definition UserData.h:98
zypp::callback::UserData::UserData
UserData(std::string type_r, std::string subtype_r)
Ctor taking ContentType.
Definition UserData.h:67
zypp::callback::UserData::ContentType
zypp::ContentType ContentType
Definition UserData.h:51
zypp::callback::UserData::DataType
std::map< std::string, AnyType > DataType
Definition UserData.h:45
zypp::callback::UserData::UserData
UserData(std::string type_r)
Ctor taking ContentType.
Definition UserData.h:63
zypp::callback::UserData::empty
bool empty() const
Whether data is empty.
Definition UserData.h:86
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:146
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:187
zypp::callback::UserData::reset
bool reset(const std::string &key_r) const
Definition UserData.h:138
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::callback
Callbacks light.
Definition Callback.h:146
zypp::callback::operator<<
std::ostream & operator<<(std::ostream &str, const UserData &obj)
relates: UserData Stream output
Definition UserData.h:230
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2