Dune Core Modules (2.3.1)

propertymap.hh
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>
17  struct PropertyMapTraits
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;
66  typedef LvaluePropertyMapTag Category;
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;
76  typedef LvaluePropertyMapTag Category;
77  };
78 
79  template<class Reference, class PropertyMap>
80  struct RAPropertyMapHelper
81  {};
82 
83  template<class Reference, class PropertyMap, class Key>
84  inline Reference
85  get(const RAPropertyMapHelper<Reference,PropertyMap>& pmap,
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
93  put(const RAPropertyMapHelper<Reference,PropertyMap>& pmap,
94  const Key& key, const Value& value)
95  {
96  dune_static_assert((Conversion<typename PropertyMap::Category,WritablePropertyMapTag>
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
An adapter to turn an unique associative container into a property map.
Definition: propertymap.hh:181
An adaptor to turn an unique associative container into a property map.
Definition: propertymap.hh:239
Adapter to turn a random access iterator into a property map.
Definition: propertymap.hh:109
IndexMap::KeyType KeyType
The key type of the property map.
Definition: propertymap.hh:126
R Reference
The reference type of the property map.
Definition: propertymap.hh:136
T ValueType
The value type of the property map.
Definition: propertymap.hh:131
Reference operator[](KeyType key) const
Access the a value by reference.
Definition: propertymap.hh:161
IteratorPropertyMap(RandomAccessIterator iter, const IndexMap &im=IndexMap())
Constructor.
Definition: propertymap.hh:150
IteratorPropertyMap()
Constructor.
Definition: propertymap.hh:156
IM IndexMap
The type of the index map.
Definition: propertymap.hh:121
RAI RandomAccessIterator
The type of the random access iterator.
Definition: propertymap.hh:114
LvaluePropertyMapTag Category
The category of this property map.
Definition: propertymap.hh:141
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
Dune namespace.
Definition: alignment.hh:14
Fallback implementation of the C++0x static_assert feature.
A property map that applies the identity function to integers.
Definition: propertymap.hh:294
std::size_t ValueType
The value type of the map.
Definition: propertymap.hh:299
std::size_t KeyType
The key type of the map.
Definition: propertymap.hh:296
std::size_t Reference
The reference type of the map.
Definition: propertymap.hh:302
ReadablePropertyMapTag Category
The category of the map.
Definition: propertymap.hh:305
Tag for the category of lvalue property maps.
Definition: propertymap.hh:58
Selector for the property map type.
Definition: propertymap.hh:321
T Tag
the tag identifying the property.
Definition: propertymap.hh:325
C Container
The container type to whose entries the properties are attached.
Definition: propertymap.hh:330
Tag for the category of readable and writable property maps.
Definition: propertymap.hh:51
Tag for the category of readable property maps.
Definition: propertymap.hh:39
Tag for the category of writable property maps.
Definition: propertymap.hh:43
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)