1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/UserData.h
11 #ifndef ZYPP_USERDATA_H
12 #define ZYPP_USERDATA_H
17 #include <boost/any.hpp>
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/ContentType.h"
22 ///////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 /// \brief Typesafe passing of user data via callbacks
32 /// Basically a <tt>std::map<std::string,boost::any></tt> plus
33 /// associated \ref ContentType.
35 /// Constness protects non-empty values from being modified.
36 /// It is possible to overwrite empty values or to add new ones.
37 ///////////////////////////////////////////////////////////////////
40 typedef std::map<std::string,boost::any> DataType;
41 typedef DataType::size_type size_type;
42 typedef DataType::key_type key_type;
43 typedef DataType::value_type value_type;
44 typedef DataType::const_iterator const_iterator;
50 /** Ctor taking ContentType. */
51 explicit UserData( ContentType type_r )
52 : _type( std::move(type_r) )
54 /** Ctor taking ContentType. */
55 explicit UserData( std::string type_r )
56 : UserData( ContentType( std::move(type_r) ) )
58 /** Ctor taking ContentType. */
59 UserData( std::string type_r, std::string subtype_r )
60 : UserData( ContentType( std::move(type_r), std::move(subtype_r) ) )
65 const ContentType & type() const
69 void type( ContentType type_r )
70 { _type = std::move(type_r); }
73 /** Validate object in a boolean context: has data */
74 explicit operator bool() const
77 /** Whether \ref data is empty. */
79 { return !_dataP || _dataP->empty(); }
81 /** Size of \ref data. */
82 size_type size() const
83 { return _dataP ? _dataP->size() : 0; }
86 const DataType & data() const
89 /** Whether \a key_r is in \ref data. */
90 bool haskey( const std::string & key_r ) const
91 { return _dataP && _dataP->find( key_r ) != _dataP->end(); }
93 /** Whether \a key_r is in \ref data and value is not empty. */
94 bool hasvalue( const std::string & key_r ) const
99 const_iterator it = _dataP->find( key_r );
100 if ( it != _dataP->end() && ! it->second.empty() )
108 /** Set the value for key (nonconst version always returns true).
109 * Const version is allowed to set empty values or to add new ones only.
111 bool set( const std::string & key_r, boost::any val_r )
112 { dataRef()[key_r] = std::move(val_r); return true; }
113 /** \overload const version */
114 bool set( const std::string & key_r, boost::any val_r ) const
117 boost::any & val( dataRef()[key_r] );
120 val = std::move(val_r);
126 /** Set an empty value for \a key_r (if possible). */
127 bool reset( const std::string & key_r )
128 { return set( key_r, boost::any() ); }
129 /** \overload const version */
130 bool reset( const std::string & key_r ) const
131 { return set( key_r, boost::any() ); }
133 /** Remove key from data.*/
134 void erase( const std::string & key_r )
135 { if ( _dataP ) _dataP->erase( key_r ); }
137 /** Return the keys boost::any value or an empty value if key does not exist. */
138 const boost::any & getvalue( const std::string & key_r ) const
142 const_iterator it = _dataP->find( key_r );
143 if ( it != _dataP->end() )
148 static const boost::any none;
152 /** Pass back a <tt>const Tp &</tt> reference to \a key_r value.
153 * \throws boost::bad_any_cast if key is not set or value is not of appropriate type
156 * std::string value( "defaultvalue" );
159 * value = data.get<std::string>( "mykey" );
161 * catch ( const boost::bad_any_cast & )
163 * // no "mykey" or not a std::sting
168 const Tp & get( const std::string & key_r ) const
169 { return boost::any_cast<const Tp &>( getvalue( key_r ) ); }
171 /** Pass back a \a Tp copy of \a key_r value.
172 * \throws boost::bad_any_cast if key is not set or value is not of appropriate type
175 * std::string value = data.get<std::string>( "mykey", "defaultvalue" );
179 Tp get( const std::string & key_r, const Tp & default_r ) const
180 { Tp ret( default_r ); get( key_r, ret ); return ret; }
182 /** If the value for \a key_r is of the same type as \a ret_r, pass it back in \a ret_r and return \c true;.
185 * std::string value( "defaultvalue" );
186 * if ( ! data.get<std::string>( "mykey", value )
188 * // no "mykey" or not a std::sting
193 bool get( const std::string & key_r, Tp & ret_r ) const
198 const_iterator it = _dataP->find( key_r );
199 if ( it != _dataP->end() )
201 auto ptr = boost::any_cast<const Tp>(&it->second);
213 DataType & dataRef() const
214 { if ( ! _dataP ) _dataP.reset( new DataType ); return *_dataP; }
218 mutable shared_ptr<DataType> _dataP;
221 /** \relates UserData Stream output */
222 inline std::ostream & operator<<( std::ostream & str, const UserData & obj )
223 { return str << "UserData(" << obj.type() << ":" << obj.size() << ")";}
225 } // namespace callback
226 ///////////////////////////////////////////////////////////////////
228 ///////////////////////////////////////////////////////////////////
229 #endif // ZYPP_USERDATA_H