File: ExplicitMap.h

package info (click to toggle)
libzypp 17.25.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,144 kB
  • sloc: cpp: 94,656; xml: 2,575; sh: 700; makefile: 34; python: 12
file content (252 lines) | stat: -rw-r--r-- 7,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*---------------------------------------------------------------------\
 |                          ____ _   __ __ ___                          |
 |                         |__  / \ / / . \ . \                         |
 |                           / / \ V /|  _/  _/                         |
 |                          / /__ | | | | | |                           |
 |                         /_____||_| |_| |_|                           |
 |                                                                      |
 \---------------------------------------------------------------------*/
/** \file zypp/ExplicitMap.h
 *
*/
#ifndef ZYPP_EXPLICITMAP_H
#define ZYPP_EXPLICITMAP_H

#include <iosfwd>
#include <map>

#include <boost/call_traits.hpp>

///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////
  //
  //    CLASS NAME : ExplicitMap<TKey, Tp>
  //
  /** A simple lookup map using default value for not existing entries.
   *
   * A std::map providing <tt>operator[] const</tt> only. Nor existing
   * entries are mapped to a default value. Entries are maipulated vis
   * methods \ref set and \ref unset. Helper classes \ref TmpSet,
   * \ref TmpUnset and \ref TmpSetDefault are provided to temporarily
   * change and automaticlly restore values.
   */
  template<class TKey, class Tp>
    class ExplicitMap
    {
    public:
      typedef typename boost::call_traits<Tp>::value_type       value_type;
      typedef typename boost::call_traits<Tp>::reference        reference;
      typedef typename boost::call_traits<Tp>::const_reference  const_reference;
      typedef typename boost::call_traits<Tp>::param_type       param_type;

    private:
      typedef typename std::map<TKey,value_type> map_type;
      typedef typename map_type::iterator        iterator;

    public:
      typedef typename map_type::key_type       key_type;
      typedef typename map_type::size_type      size_type;
      typedef typename map_type::const_iterator const_iterator;

    public:
      ExplicitMap()
      {}

      explicit
      ExplicitMap( param_type mapDefault_r )
      : _mapDefault( mapDefault_r )
      {}

      template <class TInputIterator>
        ExplicitMap( TInputIterator first_r, TInputIterator last_r )
        : _map( first_r, last_r )
        {}

      template <class TInputIterator>
        ExplicitMap( TInputIterator first_r, TInputIterator last_r,
                     param_type mapDefault_r )
        : _map( first_r, last_r )
        , _mapDefault( mapDefault_r )
        {}

    public:
      size_type size() const
      { return _map.size(); }

      bool empty() const
      { return _map.empty(); }

      const_iterator begin() const
      { return _map.begin(); }

      const_iterator end() const
      { return _map.end(); }

      const_iterator find( const key_type & key_r ) const
      { return _map.find( key_r ); }

      bool has( const key_type & key_r ) const
      { return _map.find( key_r ) != end(); }

      const_reference get( const key_type & key_r ) const
      {
        const_iterator it = _map.find( key_r );
        return( it == _map.end() ? _mapDefault : it->second );
      }

      const_reference getDefault() const
      { return _mapDefault; }

      const_reference operator[]( const key_type & key_r ) const
      { return get( key_r ); }

    public:
      void clear()
      { _map.clear(); }

      void set( const key_type & key_r, param_type value_r )
      { _map[key_r] = value_r; }

      template <typename TInputIterator>
        void set( TInputIterator first_r, TInputIterator last_r )
        { _map.insert( first_r, last_r ); }

      void unset( const key_type & key_r )
      { _map.erase( key_r ); }

      void setDefault( param_type value_r )
      { _mapDefault = value_r; }

    public:
      class TmpSet;
      class TmpUnset;
      class TmpSetDefault;

      //private:
      value_type _mapDefault;
      map_type   _map;
    };
  ///////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////
  //
  //    CLASS NAME : ExplicitMap<TKey, Tp>::TmpSet
  //
  /** Temporarily set a value. */
  template<class TKey, class Tp>
    class ExplicitMap<TKey, Tp>::TmpSet
    {
    public:
      TmpSet( ExplicitMap & map_r, const key_type & key_r, param_type value_r )
      : _map( map_r )
      , _key( key_r )
      {
        const_iterator it = _map.find( _key );
        if ( it == _map.end() )
          {
            _wasDefault = true;
          }
        else
          {
            _wasDefault = false;
            _value = it->second;
          }
        _map.set( _key, value_r );
      }

      ~TmpSet()
      {
        if ( _wasDefault )
          {
            _map.unset( _key );
          }
        else
          {
            _map.set( _key, _value );
          }
      }

    private:
      ExplicitMap & _map;
      key_type      _key;
      param_type    _value;
      bool          _wasDefault;
    };
  ///////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////
  //
  //    CLASS NAME : ExplicitMap<TKey, Tp>::TmpUnset
  //
  /** Temporarily unset a value. */
  template<class TKey, class Tp>
    class ExplicitMap<TKey, Tp>::TmpUnset
    {
    public:
      TmpUnset( ExplicitMap & map_r, const key_type & key_r )
      : _map( map_r )
      , _key( key_r )
      {
        const_iterator it = _map.find( _key );
        if ( it == _map.end() )
          {
            _wasDefault = true;
          }
        else
          {
            _wasDefault = false;
            _value = it->second;
            _map.unset( _key );
          }
      }

      ~TmpUnset()
      {
        if ( ! _wasDefault )
          {
            _map.set( _key, _value );
          }
      }

    private:
      ExplicitMap & _map;
      key_type      _key;
      param_type    _value;
      bool          _wasDefault;
    };
  ///////////////////////////////////////////////////////////////////

  ///////////////////////////////////////////////////////////////////
  //
  //    CLASS NAME : ExplicitMap<TKey, Tp>::TmpSetDefault
  //
  /** Temporarily change the default value. */
  template<class TKey, class Tp>
    class ExplicitMap<TKey, Tp>::TmpSetDefault
    {
    public:
      TmpSetDefault( ExplicitMap & map_r, param_type value_r )
      : _map( map_r )
      , _value( _map.getDefault() )
      {
        _map.setDefault( value_r );
      }

      ~TmpSetDefault()
      {
        _map.setDefault( _value );
      }

    private:
      ExplicitMap & _map;
      param_type    _value;
    };
  ///////////////////////////////////////////////////////////////////

  /////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
#endif // ZYPP_EXPLICITMAP_H