dune-common  2.3.1
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
std::ptrdiff_t KeyType
Definition: propertymap.hh:65
IndexMap::KeyType KeyType
The key type of the property map.
Definition: propertymap.hh:126
Tag for the category of readable and writable property maps.
Definition: propertymap.hh:49
C Container
The container type to whose entries the properties are attached.
Definition: propertymap.hh:330
std::size_t Reference
The reference type of the map.
Definition: propertymap.hh:302
PM::Reference Reference
The type of the reference to the values.
Definition: propertymap.hh:30
ValueType & Reference
Definition: propertymap.hh:64
Checks wether a type is convertible to another.
Definition: typetraits.hh:212
PM::Category Category
The category the property map belongs to.
Definition: propertymap.hh:34
LvaluePropertyMapTag Category
The category of this property map.
Definition: propertymap.hh:141
ReadablePropertyMapTag Category
The category of the map.
Definition: propertymap.hh:305
A property map that applies the identity function to integers.
Definition: propertymap.hh:292
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
ValueType operator[](const KeyType &key) const
Definition: propertymap.hh:307
IteratorPropertyMap()
Constructor.
Definition: propertymap.hh:156
Tag for the category of readable property maps.
Definition: propertymap.hh:38
LvaluePropertyMapTag Category
Definition: propertymap.hh:76
RAI RandomAccessIterator
The type of the random access iterator.
Definition: propertymap.hh:114
PM::KeyType KeyType
The type of the key of the property map.
Definition: propertymap.hh:22
std::ptrdiff_t KeyType
Definition: propertymap.hh:75
Traits for type conversions and type information.
Definition: propertymap.hh:17
Tag for the category of writable property maps.
Definition: propertymap.hh:42
IteratorPropertyMap(RandomAccessIterator iter, const IndexMap &im=IndexMap())
Constructor.
Definition: propertymap.hh:150
Fallback implementation of the C++0x static_assert feature.
LvaluePropertyMapTag Category
Definition: propertymap.hh:66
Selector for the property map type.
Definition: propertymap.hh:320
An adapter to turn an unique associative container into a property map.
Definition: propertymap.hh:178
Definition: propertymap.hh:80
IM IndexMap
The type of the index map.
Definition: propertymap.hh:121
PM::ValueType ValueType
The type of the values of the property map.
Definition: propertymap.hh:26
R Reference
The reference type of the property map.
Definition: propertymap.hh:136
T ValueType
Definition: propertymap.hh:73
An adaptor to turn an unique associative container into a property map.
Definition: propertymap.hh:236
std::size_t ValueType
The value type of the map.
Definition: propertymap.hh:299
Reference operator[](KeyType key) const
Access the a value by reference.
Definition: propertymap.hh:161
Tag for the category of lvalue property maps.
Definition: propertymap.hh:56
T ValueType
Definition: propertymap.hh:63
void put(const RAPropertyMapHelper< Reference, PropertyMap > &pmap, const Key &key, const Value &value)
Definition: propertymap.hh:93
T ValueType
The value type of the property map.
Definition: propertymap.hh:131
Adapter to turn a random access iterator into a property map.
Definition: propertymap.hh:107
std::size_t KeyType
The key type of the map.
Definition: propertymap.hh:296
const ValueType & Reference
Definition: propertymap.hh:74
T Tag
the tag identifying the property.
Definition: propertymap.hh:325