libzypp  14.48.5
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  typedef std::map<std::string,boost::any> DataType;
42  typedef DataType::key_type key_type;
43  typedef DataType::value_type value_type;
44  typedef DataType::const_iterator const_iterator;
45  public:
48  {}
49 
51  explicit UserData( ContentType type_r )
52  : _type( std::move(type_r) )
53  {}
55  explicit UserData( std::string type_r )
56  : UserData( ContentType( std::move(type_r) ) )
57  {}
59  UserData( std::string type_r, std::string subtype_r )
60  : UserData( ContentType( std::move(type_r), std::move(subtype_r) ) )
61  {}
62 
63  public:
65  const ContentType & type() const
66  { return _type; }
67 
69  void type( ContentType type_r )
70  { _type = std::move(type_r); }
71 
72  public:
74  explicit operator bool() const
75  { return !empty(); }
76 
78  bool empty() const
79  { return !_dataP || _dataP->empty(); }
80 
82  size_type size() const
83  { return _dataP ? _dataP->size() : 0; }
84 
86  const DataType & data() const
87  { return dataRef(); }
88 
90  bool haskey( const std::string & key_r ) const
91  { return _dataP && _dataP->find( key_r ) != _dataP->end(); }
92 
94  bool hasvalue( const std::string & key_r ) const
95  {
96  bool ret = false;
97  if ( _dataP )
98  {
99  const_iterator it = _dataP->find( key_r );
100  if ( it != _dataP->end() && ! it->second.empty() )
101  {
102  ret = true;
103  }
104  }
105  return ret;
106  }
107 
111  bool set( const std::string & key_r, boost::any val_r )
112  { dataRef()[key_r] = std::move(val_r); return true; }
114  bool set( const std::string & key_r, boost::any val_r ) const
115  {
116  bool ret = false;
117  boost::any & val( dataRef()[key_r] );
118  if ( val.empty() )
119  {
120  val = std::move(val_r);
121  ret = true;
122  }
123  return ret;
124  }
125 
127  bool reset( const std::string & key_r )
128  { return set( key_r, boost::any() ); }
130  bool reset( const std::string & key_r ) const
131  { return set( key_r, boost::any() ); }
132 
134  void erase( const std::string & key_r )
135  { if ( _dataP ) _dataP->erase( key_r ); }
136 
138  const boost::any & getvalue( const std::string & key_r ) const
139  {
140  if ( _dataP )
141  {
142  const_iterator it = _dataP->find( key_r );
143  if ( it != _dataP->end() )
144  {
145  return it->second;
146  }
147  }
148  static const boost::any none;
149  return none;
150  }
151 
167  template <class Tp>
168  const Tp & get( const std::string & key_r ) const
169  { return boost::any_cast<const Tp &>( getvalue( key_r ) ); }
170 
178  template <class Tp>
179  Tp get( const std::string & key_r, const Tp & default_r ) const
180  { Tp ret( default_r ); get( key_r, ret ); return ret; }
181 
192  template <class Tp>
193  bool get( const std::string & key_r, Tp & ret_r ) const
194  {
195  bool ret = false;
196  if ( _dataP )
197  {
198  const_iterator it = _dataP->find( key_r );
199  if ( it != _dataP->end() )
200  {
201  auto ptr = boost::any_cast<const Tp>(&it->second);
202  if ( ptr )
203  {
204  ret_r = *ptr;
205  ret = true;
206  }
207  }
208  }
209  return ret;
210  }
211 
212  private:
213  DataType & dataRef() const
214  { if ( ! _dataP ) _dataP.reset( new DataType ); return *_dataP; }
215 
216  private:
218  mutable shared_ptr<DataType> _dataP;
219  };
220 
222  inline std::ostream & operator<<( std::ostream & str, const UserData & obj )
223  { return str << "UserData(" << obj.type() << ":" << obj.size() << ")";}
224 
225  } // namespace callback
227 } // namespace zypp
229 #endif // ZYPP_USERDATA_H
bool empty() const
Whether data is empty.
Definition: UserData.h:78
std::ostream & operator<<(std::ostream &str, const UserData &obj)
Definition: UserData.h:222
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
Definition: UserData.h:94
bool reset(const std::string &key_r) const
Definition: UserData.h:130
DataType::size_type size_type
Definition: UserData.h:41
void erase(const std::string &key_r)
Remove key from data.
Definition: UserData.h:134
bool set(const std::string &key_r, boost::any val_r) const
Definition: UserData.h:114
DataType::const_iterator const_iterator
Definition: UserData.h:44
size_type size() const
Size of data.
Definition: UserData.h:82
void type(ContentType type_r)
Set type.
Definition: UserData.h:69
UserData(std::string type_r, std::string subtype_r)
Ctor taking ContentType.
Definition: UserData.h:59
bool reset(const std::string &key_r)
Set an empty value for key_r (if possible).
Definition: UserData.h:127
UserData(std::string type_r)
Ctor taking ContentType.
Definition: UserData.h:55
shared_ptr< DataType > _dataP
Definition: UserData.h:218
DataType::value_type value_type
Definition: UserData.h:43
SolvableIdType size_type
Definition: PoolMember.h:147
UserData()
Default ctor.
Definition: UserData.h:47
const boost::any & getvalue(const std::string &key_r) const
Return the keys boost::any value or an empty value if key does not exist.
Definition: UserData.h:138
UserData(ContentType type_r)
Ctor taking ContentType.
Definition: UserData.h:51
std::map< std::string, boost::any > DataType
Definition: UserData.h:40
DataType::key_type key_type
Definition: UserData.h:42
Typesafe passing of user data via callbacks.
Definition: UserData.h:38
bool haskey(const std::string &key_r) const
Whether key_r is in data.
Definition: UserData.h:90
DataType & dataRef() const
Definition: UserData.h:213
const ContentType & type() const
Get type.
Definition: UserData.h:65
const DataType & data() const
The data.
Definition: UserData.h:86
Mime type like 'type/subtype' classification of content.
Definition: ContentType.h:29
bool set(const std::string &key_r, boost::any val_r)
Set the value for key (nonconst version always returns true).
Definition: UserData.h:111