dune-common  2.3.0
propertymap.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // $Id$
4 #ifndef DUNE_PROPERTYMAP_HH
5 #define DUNE_PROPERTYMAP_HH
6 
7 #include <cstddef>
8 #include <iterator>
9 
10 #include "static_assert.hh"
11 #include "typetraits.hh"
12 
13 namespace Dune
14 {
15 
16  template<class PM>
18  {
22  typedef typename PM::KeyType KeyType;
26  typedef typename PM::ValueType ValueType;
30  typedef typename PM::Reference Reference;
34  typedef typename PM::Category Category;
35  };
36 
39  {};
40 
43  {};
44 
51  {};
52 
58  {};
59 
60  template<class T>
61  struct PropertyMapTraits<T*>
62  {
63  typedef T ValueType;
64  typedef ValueType& Reference;
65  typedef std::ptrdiff_t KeyType;
67  };
68 
69 
70  template<class T>
71  struct PropertyMapTraits<const T*>
72  {
73  typedef T ValueType;
74  typedef const ValueType& Reference;
75  typedef std::ptrdiff_t KeyType;
77  };
78 
79  template<class Reference, class PropertyMap>
81  {};
82 
83  template<class Reference, class PropertyMap, class Key>
84  inline Reference
86  const Key& key)
87  {
88  return static_cast<const PropertyMap&>(pmap)[key];
89  }
90 
91  template<class Reference, class PropertyMap, class Key, class Value>
92  inline void
94  const Key& key, const Value& value)
95  {
97  ::exists), "WritablePropertyMapTag required!");
98  static_cast<const PropertyMap&>(pmap)[key] = value;
99  }
100 
104  template<class RAI, class IM,
105  class T = typename std::iterator_traits<RAI>::value_type,
106  class R = typename std::iterator_traits<RAI>::reference>
108  : public RAPropertyMapHelper<R,IteratorPropertyMap<RAI,IM,T,R> >
109  {
110  public:
114  typedef RAI RandomAccessIterator;
115 
121  typedef IM IndexMap;
122 
126  typedef typename IndexMap::KeyType KeyType;
127 
131  typedef T ValueType;
132 
136  typedef R Reference;
137 
142 
151  const IndexMap& im=IndexMap())
152  : iter_(iter), indexMap_(im)
153  {}
154 
157  : iter_(), indexMap_()
158  {}
159 
161  inline Reference operator[](KeyType key) const
162  {
163  return *(iter_ + get(indexMap_, key));
164  }
165 
166  private:
168  RandomAccessIterator iter_;
170  IndexMap indexMap_;
171  };
172 
177  template<typename T>
179  : RAPropertyMapHelper<typename T::value_type::second_type&,
180  AssociativePropertyMap<T> >
181  {
185  typedef T UniqueAssociativeContainer;
186 
190  typedef typename UniqueAssociativeContainer::value_type::first_type
191  KeyType;
192 
196  typedef typename UniqueAssociativeContainer::value_type::second_type
197  ValueType;
198 
202  typedef ValueType& Reference;
203 
208 
210  inline AssociativePropertyMap()
211  : map_(0)
212  {}
213 
215  inline AssociativePropertyMap(UniqueAssociativeContainer& map)
216  : map_(&map)
217  {}
218 
223  inline Reference operator[](KeyType key) const
224  {
225  return map_->find(key)->second;
226  }
227  private:
228  UniqueAssociativeContainer* map_;
229  };
230 
235  template<typename T>
237  : RAPropertyMapHelper<const typename T::value_type::second_type&,
238  ConstAssociativePropertyMap<T> >
239  {
243  typedef T UniqueAssociativeContainer;
244 
248  typedef typename UniqueAssociativeContainer::value_type::first_type
249  KeyType;
250 
254  typedef typename UniqueAssociativeContainer::value_type::second_type
255  ValueType;
256 
260  typedef const ValueType& Reference;
261 
266 
269  : map_(0)
270  {}
271 
273  inline ConstAssociativePropertyMap(const UniqueAssociativeContainer& map)
274  : map_(&map)
275  {}
276 
281  inline Reference operator[](KeyType key) const
282  {
283  return map_->find(key)->second;
284  }
285  private:
286  const UniqueAssociativeContainer* map_;
287  };
288 
292  struct IdentityMap
293  : public RAPropertyMapHelper<std::size_t, IdentityMap>
294  {
296  typedef std::size_t KeyType;
297 
299  typedef std::size_t ValueType;
300 
302  typedef std::size_t Reference;
303 
306 
307  inline ValueType operator[](const KeyType& key) const
308  {
309  return key;
310  }
311  };
312 
313 
319  template<typename T, typename C>
321  {
325  typedef T Tag;
330  typedef C Container;
331  };
332 
333 }
334 
335 #endif