1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ExplicitMap.h
12 #ifndef ZYPP_EXPLICITMAP_H
13 #define ZYPP_EXPLICITMAP_H
18 #include <boost/call_traits.hpp>
20 ///////////////////////////////////////////////////////////////////
22 { /////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 // CLASS NAME : ExplicitMap<_Key, _Tp>
28 /** A simple lookup map using default value for not existing entries.
30 * A std::map providing <tt>operator[] const</tt> only. Nor existing
31 * entries are mapped to a default value. Entries are maipulated vis
32 * methods \ref set and \ref unset. Helper classes \ref TmpSet,
33 * \ref TmpUnset and \ref TmpSetDefault are provided to temporarily
34 * change and automaticlly restore values.
36 template<class _Key, class _Tp>
40 typedef typename boost::call_traits<_Tp>::value_type value_type;
41 typedef typename boost::call_traits<_Tp>::reference reference;
42 typedef typename boost::call_traits<_Tp>::const_reference const_reference;
43 typedef typename boost::call_traits<_Tp>::param_type param_type;
46 typedef typename std::map<_Key,value_type> map_type;
47 typedef typename map_type::iterator iterator;
50 typedef typename map_type::key_type key_type;
51 typedef typename map_type::size_type size_type;
52 typedef typename map_type::const_iterator const_iterator;
59 ExplicitMap( param_type mapDefault_r )
60 : _mapDefault( mapDefault_r )
63 template <class _InputIterator>
64 ExplicitMap( _InputIterator first_r, _InputIterator last_r )
65 : _map( first_r, last_r )
68 template <class _InputIterator>
69 ExplicitMap( _InputIterator first_r, _InputIterator last_r,
70 param_type mapDefault_r )
71 : _map( first_r, last_r )
72 , _mapDefault( mapDefault_r )
76 size_type size() const
77 { return _map.size(); }
80 { return _map.empty(); }
82 const_iterator begin() const
83 { return _map.begin(); }
85 const_iterator end() const
86 { return _map.end(); }
88 const_iterator find( const key_type & key_r ) const
89 { return _map.find( key_r ); }
91 bool has( const key_type & key_r ) const
92 { return _map.find( key_r ) != end(); }
94 const_reference get( const key_type & key_r ) const
96 const_iterator it = _map.find( key_r );
97 return( it == _map.end() ? _mapDefault : it->second );
100 const_reference getDefault() const
101 { return _mapDefault; }
103 const_reference operator[]( const key_type & key_r ) const
104 { return get( key_r ); }
110 void set( const key_type & key_r, param_type value_r )
111 { _map[key_r] = value_r; }
113 template <typename _InputIterator>
114 void set( _InputIterator first_r, _InputIterator last_r )
115 { _map.insert( first_r, last_r ); }
117 void unset( const key_type & key_r )
118 { _map.erase( key_r ); }
120 void setDefault( param_type value_r )
121 { _mapDefault = value_r; }
129 value_type _mapDefault;
132 ///////////////////////////////////////////////////////////////////
134 ///////////////////////////////////////////////////////////////////
136 // CLASS NAME : ExplicitMap<_Key, _Tp>::TmpSet
138 /** Temporarily set a value. */
139 template<class _Key, class _Tp>
140 class ExplicitMap<_Key, _Tp>::TmpSet
143 TmpSet( ExplicitMap & map_r, const key_type & key_r, param_type value_r )
147 const_iterator it = _map.find( _key );
148 if ( it == _map.end() )
157 _map.set( _key, value_r );
168 _map.set( _key, _value );
178 ///////////////////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////////////////
182 // CLASS NAME : ExplicitMap<_Key, _Tp>::TmpUnset
184 /** Temporarily unset a value. */
185 template<class _Key, class _Tp>
186 class ExplicitMap<_Key, _Tp>::TmpUnset
189 TmpUnset( ExplicitMap & map_r, const key_type & key_r )
193 const_iterator it = _map.find( _key );
194 if ( it == _map.end() )
210 _map.set( _key, _value );
220 ///////////////////////////////////////////////////////////////////
222 ///////////////////////////////////////////////////////////////////
224 // CLASS NAME : ExplicitMap<_Key, _Tp>::TmpSetDefault
226 /** Temporarily change the default value. */
227 template<class _Key, class _Tp>
228 class ExplicitMap<_Key, _Tp>::TmpSetDefault
231 TmpSetDefault( ExplicitMap & map_r, param_type value_r )
233 , _value( _map.getDefault() )
235 _map.setDefault( value_r );
240 _map.setDefault( _value );
247 ///////////////////////////////////////////////////////////////////
249 /////////////////////////////////////////////////////////////////
251 ///////////////////////////////////////////////////////////////////
252 #endif // ZYPP_EXPLICITMAP_H