dune-common  2.2.1
propertymap.hh
Go to the documentation of this file.
1 // $Id: propertymap.hh 6003 2010-05-14 09:12:39Z sander $
2 #ifndef DUNE_PROPERTYMAP_HH
3 #define DUNE_PROPERTYMAP_HH
4 
5 #include <cstddef>
6 #include <iterator>
7 
8 #include"static_assert.hh"
9 #include"typetraits.hh"
10 
11 namespace Dune
12 {
13 
14  template<class PM>
16  {
20  typedef typename PM::KeyType KeyType;
24  typedef typename PM::ValueType ValueType;
28  typedef typename PM::Reference Reference;
32  typedef typename PM::Category Category;
33  };
34 
37  {};
38 
41  {};
42 
49  {};
50 
56  {};
57 
58  template<class T>
59  struct PropertyMapTraits<T*>
60  {
61  typedef T ValueType;
62  typedef ValueType& Reference;
63  typedef std::ptrdiff_t KeyType;
65  };
66 
67 
68  template<class T>
69  struct PropertyMapTraits<const T*>
70  {
71  typedef T ValueType;
72  typedef const ValueType& Reference;
73  typedef std::ptrdiff_t KeyType;
75  };
76 
77  template<class Reference, class PropertyMap>
79  {};
80 
81  template<class Reference, class PropertyMap, class Key>
82  inline Reference
84  const Key& key)
85  {
86  return static_cast<const PropertyMap&>(pmap)[key];
87  }
88 
89  template<class Reference, class PropertyMap, class Key, class Value>
90  inline void
92  const Key& key, const Value& value)
93  {
95  ::exists), "WritablePropertyMapTag required!");
96  static_cast<const PropertyMap&>(pmap)[key] = value;
97  }
98 
102  template<class RAI, class IM,
103  class T = typename std::iterator_traits<RAI>::value_type,
104  class R = typename std::iterator_traits<RAI>::reference>
106  : public RAPropertyMapHelper<R,IteratorPropertyMap<RAI,IM,T,R> >
107  {
108  public:
112  typedef RAI RandomAccessIterator;
113 
119  typedef IM IndexMap;
120 
124  typedef typename IndexMap::KeyType KeyType;
125 
129  typedef T ValueType;
130 
134  typedef R Reference;
135 
140 
149  const IndexMap& im=IndexMap())
150  :iter_(iter), indexMap_(im)
151  {}
152 
155  : iter_(), indexMap_()
156  {}
157 
159  inline Reference operator[](KeyType key) const
160  {
161  return *(iter_ + get(indexMap_, key));
162  }
163 
164  private:
166  RandomAccessIterator iter_;
168  IndexMap indexMap_;
169  };
170 
175  template<typename T>
177  : RAPropertyMapHelper<typename T::value_type::second_type&,
178  AssociativePropertyMap<T> >
179  {
183  typedef T UniqueAssociativeContainer;
184 
188  typedef typename UniqueAssociativeContainer::value_type::first_type
189  KeyType;
190 
194  typedef typename UniqueAssociativeContainer::value_type::second_type
195  ValueType;
196 
200  typedef ValueType& Reference;
201 
206 
208  inline AssociativePropertyMap()
209  : map_(0)
210  {}
211 
213  inline AssociativePropertyMap(UniqueAssociativeContainer& map)
214  : map_(&map)
215  {}
216 
221  inline Reference operator[](KeyType key)const
222  {
223  return map_->find(key)->second;
224  }
225  private:
226  UniqueAssociativeContainer* map_;
227  };
228 
233  template<typename T>
235  : RAPropertyMapHelper<const typename T::value_type::second_type&,
236  ConstAssociativePropertyMap<T> >
237  {
241  typedef T UniqueAssociativeContainer;
242 
246  typedef typename UniqueAssociativeContainer::value_type::first_type
247  KeyType;
248 
252  typedef typename UniqueAssociativeContainer::value_type::second_type
253  ValueType;
254 
258  typedef const ValueType& Reference;
259 
264 
267  : map_(0)
268  {}
269 
271  inline ConstAssociativePropertyMap(const UniqueAssociativeContainer& map)
272  : map_(&map)
273  {}
274 
279  inline Reference operator[](KeyType key)const
280  {
281  return map_->find(key)->second;
282  }
283  private:
284  const UniqueAssociativeContainer* map_;
285  };
286 
290  struct IdentityMap
291  : public RAPropertyMapHelper<std::size_t, IdentityMap>
292  {
294  typedef std::size_t KeyType;
295 
297  typedef std::size_t ValueType;
298 
300  typedef std::size_t Reference;
301 
304 
305  inline ValueType operator[](const KeyType& key) const
306  {
307  return key;
308  }
309  };
310 
311 
317  template<typename T, typename C>
319  {
323  typedef T Tag;
328  typedef C Container;
329  };
330 
331 }
332 
333 #endif