libzypp  17.37.5
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/base/PtrTypes.h>
21 #include <zypp/ContentType.h>
22 
24 namespace 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;
47  typedef DataType::key_type key_type;
48  typedef DataType::value_type value_type;
49  typedef DataType::const_iterator const_iterator;
50 
52 
53  public:
56  {}
57 
59  explicit UserData( ContentType type_r )
60  : _type( std::move(type_r) )
61  {}
63  explicit UserData( std::string type_r )
64  : UserData( ContentType( std::move(type_r) ) )
65  {}
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; }
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() ); }
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:
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
zypp::ContentType ContentType
Definition: UserData.h:51
std::ostream & operator<<(std::ostream &str, const UserData &obj)
Definition: UserData.h:230
size_type size() const
Size of data.
Definition: UserData.h:90
DataType & dataRef() const
Definition: UserData.h:221
String related utilities and Regular expression matching.
DataType::size_type size_type
Definition: UserData.h:46
Definition: Arch.h:363
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
std::map< std::string, AnyType > DataType
Definition: UserData.h:45
void erase(const std::string &key_r)
Remove key from data.
Definition: UserData.h:142
DataType::const_iterator const_iterator
Definition: UserData.h:49
void type(ContentType type_r)
Set type.
Definition: UserData.h:77
UserData(std::string type_r, std::string subtype_r)
Ctor taking ContentType.
Definition: UserData.h:67
bool reset(const std::string &key_r)
Set an empty value for key_r (if possible).
Definition: UserData.h:135
const DataType & data() const
The data.
Definition: UserData.h:94
UserData(std::string type_r)
Ctor taking ContentType.
Definition: UserData.h:63
shared_ptr< DataType > _dataP
Definition: UserData.h:226
const ContentType & type() const
Get type.
Definition: UserData.h:73
DataType::value_type value_type
Definition: UserData.h:48
UserData()
Default ctor.
Definition: UserData.h:55
UserData(ContentType type_r)
Ctor taking ContentType.
Definition: UserData.h:59
boost::bad_any_cast bad_AnyType_cast
Definition: UserData.h:43
DataType::key_type key_type
Definition: UserData.h:47
Typesafe passing of user data via callbacks.
Definition: UserData.h:39
bool haskey(const std::string &key_r) const
Whether key_r is in data.
Definition: UserData.h:98
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
Definition: UserData.h:102
bool reset(const std::string &key_r) const
Definition: UserData.h:138
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
SolvableIdType size_type
Definition: PoolMember.h:126
bool empty() const
Whether data is empty.
Definition: UserData.h:86
Mime type like &#39;type/subtype&#39; classification of content.
Definition: ContentType.h:29