Claw  1.7.3
multi_type_map.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
31 #include <claw/assert.hpp>
32 #include <claw/meta/same_type.hpp>
33 
34 namespace claw
35 {
36  /*
37  * Here is the implementation of multi_type_map_wrapper for the case where the
38  * ValueType is the first type in the type list of the map.
39  */
40  template<typename Key, typename Head, typename Tail>
41  class multi_type_map_wrapper
42  < Head, multi_type_map<Key, meta::type_list<Head, Tail> > >
43  {
44  typedef Key key_type;
45  typedef Head value_type;
46  typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
47  typedef typename map_type::iterator_type iterator;
48  typedef typename map_type::const_iterator_type const_iterator;
49 
50  public:
51  static void erase( map_type& self, iterator it )
52  {
53  self.m_data.erase(it);
54  }
55 
56  static std::size_t erase( map_type& self, const key_type& k )
57  {
58  return self.m_data.erase(k);
59  }
60 
61  static const value_type& get( const map_type& self, const key_type& k )
62  {
63  CLAW_PRECOND( exists(self, k) );
64  return self.m_data.find(k)->second;
65  }
66 
67  static value_type& get( map_type& self, const key_type& k )
68  {
69  CLAW_PRECOND( exists(self, k) );
70  return self.m_data.find(k)->second;
71  }
72 
73  static void set( map_type& self, const key_type& k, const value_type& v )
74  {
75  self.m_data[k] = v;
76  }
77 
78  static bool exists( const map_type& self, const key_type& k )
79  {
80  return self.m_data.find(k) != self.m_data.end();
81  }
82 
83  static iterator begin( map_type& self )
84  {
85  return self.m_data.begin();
86  }
87 
88  static iterator end( map_type& self )
89  {
90  return self.m_data.end();
91  }
92 
93  static const_iterator begin( const map_type& self )
94  {
95  return self.m_data.begin();
96  }
97 
98  static const_iterator end( const map_type& self )
99  {
100  return self.m_data.end();
101  }
102 
103  }; // class multi_type_map_wrapper
104 
105  /*
106  * Here is the implementation of multi_type_map_wrapper for the case where the
107  * ValueType is not the first type in the type list of the map.
108  */
109  template<typename ValueType, typename Key, typename Head, typename Tail>
110  class multi_type_map_wrapper
111  < ValueType, multi_type_map< Key, meta::type_list<Head, Tail> > >:
112  public multi_type_map_wrapper< ValueType, multi_type_map<Key, Tail> >
113  {
114 
115  }; // class multi_type_map_wrapper
116 
117  /*
118  * Here is the implementation of multi_type_map_helper for the case where the
119  * ValueType is the first type in the type list of the map.
120  */
121  template<typename Key, typename Head, typename Tail>
122  class multi_type_map_helper
123  < multi_type_map<Key, meta::type_list<Head, Tail> > >
124  {
125  typedef Key key_type;
126  typedef Head value_type;
127  typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
128  typedef typename map_type::iterator_type iterator;
129  typedef typename map_type::const_iterator_type const_iterator;
130 
131  public:
132  static void set( map_type& self, const map_type& that )
133  {
134  for ( const_iterator it=that.template begin<value_type>();
135  it!=that.template end<value_type>(); ++it )
136  self.template set<Head>( it->first, it->second );
137 
138  multi_type_map_helper< multi_type_map<Key, Tail> >::set( self, that );
139  } // size()
140 
141  static std::size_t size( const map_type& self )
142  {
143  return self.m_data.size()
144  + multi_type_map_helper< multi_type_map<Key, Tail> >::size( self );
145  } // size()
146 
147  }; // class multi_type_map_helper
148 
149  /*
150  * Here is the implementation of multi_type_map_helper that stops the
151  * recursivity.
152  */
153  template<typename Key>
154  class multi_type_map_helper
155  < multi_type_map< Key, claw::meta::no_type > >
156  {
157  private:
158  typedef multi_type_map<Key, claw::meta::no_type> map_type;
159 
160  public:
161  static void set( map_type& self, const map_type& that )
162  {
163  // nothing to do
164  } // set()
165 
166  static std::size_t size( const map_type& self )
167  {
168  return 0;
169  } // size()
170 
171  }; // class multi_type_map_helper
172 } // namespace claw
173 
174 
175 
176 
177 /*----------------------------------------------------------------------------*/
183 template<typename Key, typename Head, typename Tail>
184 template<typename ValueType>
185 void
186 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
187 ( typename iterator<ValueType>::type it )
188 {
189  multi_type_map_wrapper<ValueType, self_type>::erase(*this, it);
190 } // multi_type_map::erase()
191 
192 /*----------------------------------------------------------------------------*/
198 template<typename Key, typename Head, typename Tail>
199 template<typename ValueType>
200 std::size_t
201 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
202 ( const key_type& k )
203 {
204  return multi_type_map_wrapper<ValueType, self_type>::erase(*this, k);
205 } // multi_type_map::erase()
206 
207 /*----------------------------------------------------------------------------*/
212 template<typename Key, typename Head, typename Tail>
213 template<typename ValueType>
214 const ValueType&
215 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
216 ( const key_type& k ) const
217 {
218  return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
219 } // multi_type_map::get()
220 
221 /*----------------------------------------------------------------------------*/
226 template<typename Key, typename Head, typename Tail>
227 template<typename ValueType>
228 ValueType& claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
229 ( const key_type& k )
230 {
231  return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
232 } // multi_type_map::get()
233 
234 /*----------------------------------------------------------------------------*/
240 template<typename Key, typename Head, typename Tail>
241 template<typename ValueType>
242 void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
243 ( const key_type& k, const ValueType& v )
244 {
245  return multi_type_map_wrapper<ValueType, self_type>::set(*this, k, v);
246 } // multi_type_map::set()
247 
248 /*----------------------------------------------------------------------------*/
254 template<typename Key, typename Head, typename Tail>
255 void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
256 ( const self_type& m )
257 {
258  multi_type_map_helper<self_type>::set(*this, m);
259 } // multi_type_map::set()
260 
261 /*----------------------------------------------------------------------------*/
266 template<typename Key, typename Head, typename Tail>
267 template<typename ValueType>
268 bool claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::exists
269 ( const key_type& k ) const
270 {
271  return multi_type_map_wrapper<ValueType, self_type>::exists(*this, k);
272 } // multi_type_map::exists()
273 
274 /*----------------------------------------------------------------------------*/
278 template<typename Key, typename Head, typename Tail>
279 std::size_t
280 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::size() const
281 {
282  return multi_type_map_helper<self_type>::size(*this);
283 } // multi_type_map::size()
284 
285 /*----------------------------------------------------------------------------*/
289 template<typename Key, typename Head, typename Tail>
290 template<typename ValueType>
291 typename claw::multi_type_map
292 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
293 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin()
294 {
295  return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
296 } // multi_type_map::begin()
297 
298 /*----------------------------------------------------------------------------*/
302 template<typename Key, typename Head, typename Tail>
303 template<typename ValueType>
304 typename claw::multi_type_map
305 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
306 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end()
307 {
308  return multi_type_map_wrapper<ValueType, self_type>::end(*this);
309 } // multi_type_map::end()
310 
311 /*----------------------------------------------------------------------------*/
315 template<typename Key, typename Head, typename Tail>
316 template<typename ValueType>
317 typename
318 claw::multi_type_map
320 ::template iterator<ValueType>::const_type
321 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin() const
322 {
323  return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
324 } // multi_type_map::begin()
325 
326 /*----------------------------------------------------------------------------*/
330 template<typename Key, typename Head, typename Tail>
331 template<typename ValueType>
332 typename
333 claw::multi_type_map
335 ::template iterator<ValueType>::const_type
336 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end() const
337 {
338  return multi_type_map_wrapper<ValueType, self_type>::end(*this);
339 } // multi_type_map::end()