File: multi_type_map.tpp

package info (click to toggle)
libclaw 1.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 13,287; sh: 227; makefile: 8
file content (339 lines) | stat: -rw-r--r-- 10,842 bytes parent folder | download | duplicates (6)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
  CLAW - a C++ Library Absolutely Wonderful

  CLAW is a free library without any particular aim but being useful to 
  anyone.

  Copyright (C) 2005-2011 Julien Jorge

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: julien.jorge@gamned.org
*/
/**
 * \file multi_type_map.tpp
 * \brief Implementation of the claw::multi_type_map class.
 * \author Julien Jorge
 */

#include <claw/assert.hpp>
#include <claw/meta/same_type.hpp>

namespace claw
{
  /*
   * Here is the implementation of multi_type_map_wrapper for the case where the
   * ValueType is the first type in the type list of the map.
   */
  template<typename Key, typename Head, typename Tail>
  class multi_type_map_wrapper
  < Head, multi_type_map<Key, meta::type_list<Head, Tail> > >
  {
    typedef Key key_type;
    typedef Head value_type;
    typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
    typedef typename map_type::iterator_type iterator;
    typedef typename map_type::const_iterator_type const_iterator;

  public:
    static void erase( map_type& self, iterator it )
    {
      self.m_data.erase(it);
    }

    static std::size_t erase( map_type& self, const key_type& k )
    {
      return self.m_data.erase(k);
    }

    static const value_type& get( const map_type& self, const key_type& k )
    {
      CLAW_PRECOND( exists(self, k) );
      return self.m_data.find(k)->second;
    }

    static value_type& get( map_type& self, const key_type& k )
    {
      CLAW_PRECOND( exists(self, k) );
      return self.m_data.find(k)->second;
    }

    static void set( map_type& self, const key_type& k, const value_type& v )
    {
      self.m_data[k] = v;
    }

    static bool exists( const map_type& self, const key_type& k )
    {
      return self.m_data.find(k) != self.m_data.end();
    }

    static iterator begin( map_type& self )
    {
      return self.m_data.begin();
    }

    static iterator end( map_type& self )
    {
      return self.m_data.end();
    }

    static const_iterator begin( const map_type& self )
    {
      return self.m_data.begin();
    }

    static const_iterator end( const map_type& self )
    {
      return self.m_data.end();
    }

  }; // class multi_type_map_wrapper

  /*
   * Here is the implementation of multi_type_map_wrapper for the case where the
   * ValueType is not the first type in the type list of the map.
   */
  template<typename ValueType, typename Key, typename Head, typename Tail>
  class multi_type_map_wrapper
  < ValueType, multi_type_map< Key, meta::type_list<Head, Tail> > >:
    public multi_type_map_wrapper< ValueType, multi_type_map<Key, Tail> >
  {

  }; // class multi_type_map_wrapper

  /*
   * Here is the implementation of multi_type_map_helper for the case where the
   * ValueType is the first type in the type list of the map.
   */
  template<typename Key, typename Head, typename Tail>
  class multi_type_map_helper
  < multi_type_map<Key, meta::type_list<Head, Tail> > >
  {
    typedef Key key_type;
    typedef Head value_type;
    typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
    typedef typename map_type::iterator_type iterator;
    typedef typename map_type::const_iterator_type const_iterator;

  public:
    static void set( map_type& self, const map_type& that )
    {
      for ( const_iterator it=that.template begin<value_type>();
	    it!=that.template end<value_type>(); ++it )
	self.template set<Head>( it->first, it->second );

      multi_type_map_helper< multi_type_map<Key, Tail> >::set( self, that );
    } // size()

    static std::size_t size( const map_type& self )
    {
      return self.m_data.size()
	+ multi_type_map_helper< multi_type_map<Key, Tail> >::size( self );
    } // size()

  }; // class multi_type_map_helper

  /*
   * Here is the implementation of multi_type_map_helper that stops the
   * recursivity.
   */
  template<typename Key>
  class multi_type_map_helper
  < multi_type_map< Key, claw::meta::no_type > >
  {
  private:
    typedef multi_type_map<Key, claw::meta::no_type> map_type;

  public:
    static void set( map_type& self, const map_type& that )
    {
      // nothing to do
    } // set()

    static std::size_t size( const map_type& self )
    {
      return 0;
    } // size()

  }; // class multi_type_map_helper
} // namespace claw




/*----------------------------------------------------------------------------*/
/**
 * \brief Erase a value from the map.
 * \param k The key of the value to erase.
 * \return The number of removed elements (zero or one).
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
void
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
( typename iterator<ValueType>::type it )
{
  multi_type_map_wrapper<ValueType, self_type>::erase(*this, it);
} // multi_type_map::erase()

/*----------------------------------------------------------------------------*/
/**
 * \brief Erase a value from the map.
 * \param k The key of the value to erase.
 * \return The number of removed elements (zero or one).
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
std::size_t
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
( const key_type& k )
{
  return multi_type_map_wrapper<ValueType, self_type>::erase(*this, k);
} // multi_type_map::erase()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get a value from the map.
 * \param k The key of the value to get.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
const ValueType&
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
( const key_type& k ) const
{
  return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
} // multi_type_map::get()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get a value from the map.
 * \param k The key of the value to get.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
ValueType& claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
( const key_type& k )
{
  return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
} // multi_type_map::get()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set a value in the map.
 * \param k The key of the value to set.
 * \param v The value to set.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
( const key_type& k, const ValueType& v )
{
  return multi_type_map_wrapper<ValueType, self_type>::set(*this, k, v);
} // multi_type_map::set()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set a value in the map.
 * \param k The key of the value to set.
 * \param v The value to set.
 */
template<typename Key, typename Head, typename Tail>
void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
( const self_type& m )
{
  multi_type_map_helper<self_type>::set(*this, m);
} // multi_type_map::set()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the map contains a value of a given type with a given key.
 * \param k The key of the value to get.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
bool claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::exists
( const key_type& k ) const
{
  return multi_type_map_wrapper<ValueType, self_type>::exists(*this, k);
} // multi_type_map::exists()

/*----------------------------------------------------------------------------*/
/**
 * \brief Return the number of elements in the map.
 */
template<typename Key, typename Head, typename Tail>
std::size_t
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::size() const
{
  return multi_type_map_helper<self_type>::size(*this);
} // multi_type_map::size()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the beginning of the map for a given type.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
typename claw::multi_type_map
< Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin()
{
  return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
} // multi_type_map::begin()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the end of the map for a given type.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
typename claw::multi_type_map
< Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end()
{
  return multi_type_map_wrapper<ValueType, self_type>::end(*this);
} // multi_type_map::end()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the beginning of the map for a given type.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
typename
claw::multi_type_map
< Key, claw::meta::type_list<Head, Tail> >
::template iterator<ValueType>::const_type
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin() const
{
  return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
} // multi_type_map::begin()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get an iterator on the end of the map for a given type.
 */
template<typename Key, typename Head, typename Tail>
template<typename ValueType>
typename
claw::multi_type_map
< Key, claw::meta::type_list<Head, Tail> >
::template iterator<ValueType>::const_type
claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end() const
{
  return multi_type_map_wrapper<ValueType, self_type>::end(*this);
} // multi_type_map::end()