dune-grid  2.2.0
geometrygrid/entitypointer.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_ENTITYPOINTER_HH
2 #define DUNE_GEOGRID_ENTITYPOINTER_HH
3 
5 
9 
10 namespace Dune
11 {
12 
13  namespace GeoGrid
14  {
15 
16  // External Forward Declarations
17  // -----------------------------
18 
19  template< int, int, class >
20  class Entity;
21 
22  template< class, class >
23  struct ExportParams;
24 
25 
26 
27 
28  // Internal Forward Declarations
29  // -----------------------------
30 
31  template< int codim, class Grid >
32  struct EntityPointerTraits;
33 
34  template< class Traits, bool fake = Traits::fake >
35  class EntityPointer;
36 
37 
38 
39  // EntityPointerTraits
40  // -------------------
41 
42  template< int codim, class Grid >
43  struct EntityPointerTraits;
44 
46  template< int codim, class Grid >
47  struct EntityPointerTraits< codim, const Grid >
48  : public EntityPointerTraits< codim, Grid >
49  {};
52  template< int codim, class HostGrid, class CoordFunction, class Allocator >
53  struct EntityPointerTraits< codim, GeometryGrid< HostGrid, CoordFunction, Allocator > >
54  : public ExportParams< HostGrid, CoordFunction >
55  {
57 
58  static const bool fake = !Capabilities::hasHostEntity< Grid, codim >::v;
59 
60  typedef typename HostGrid::ctype ctype;
61 
62  static const int dimension = HostGrid::dimension;
63  static const int codimension = codim;
64 
66  typedef GeoGrid::EntitySeed< codimension, const Grid > EntitySeed;
67 
68  typedef typename HostGrid::template Codim< codim >::Entity HostEntity;
69  typedef typename HostGrid::template Codim< codim >::EntityPointer HostEntityPointer;
71 
72  typedef typename HostGrid::template Codim< 0 >::Entity HostElement;
73  typedef typename HostGrid::template Codim< 0 >::EntityPointer HostElementIterator;
74  };
75 
76 
77 
78  // EntityPointer (real)
79  // --------------------
80 
81  template< class Traits >
82  class EntityPointer< Traits, false >
83  {
85 
86  typedef typename Traits::Grid Grid;
87 
88  typedef EntityPointerTraits< Traits::codimension, const Grid > BaseTraits;
89  friend class EntityPointer< BaseTraits, false >;
90 
91  public:
92  static const int dimension = Traits::dimension;
93  static const int codimension = Traits::codimension;
94 
95  typedef typename Traits::Entity Entity;
96 
97  static const bool fake = Traits::fake;
98 
100 
101  protected:
102  typedef typename Traits::HostEntityPointer HostEntityPointer;
103  typedef typename Traits::HostEntityIterator HostEntityIterator;
104  typedef typename Traits::HostElement HostElement;
105 
106  typedef typename Traits::EntitySeed EntitySeed;
107 
110 
111  public:
112  EntityPointer ( const GeometryImpl &geo, const HostEntityIterator &hostEntityIterator )
113  : entity_( EntityImpl( geo ) ),
114  hostEntityIterator_( hostEntityIterator )
115  {}
116 
117  EntityPointer ( const Grid &grid, const HostEntityIterator &hostEntityIterator )
118  : entity_( EntityImpl( grid ) ),
119  hostEntityIterator_( hostEntityIterator )
120  {}
121 
122  EntityPointer ( const Grid &grid, const HostElement &hostElement, int subEntity )
123  : entity_( EntityImpl( grid ) ),
124  hostEntityIterator_( hostElement.template subEntity< codimension >( subEntity ) )
125  {}
126 
127  EntityPointer ( const Grid &grid, const EntitySeed &seed )
128  : entity_( EntityImpl( grid ) ),
129  hostEntityIterator_( grid.hostGrid().entityPointer( seed.hostEntitySeed() ) )
130  {}
131 
132  explicit EntityPointer ( const EntityImpl &entity )
133  : entity_( entity ),
134  hostEntityIterator_( entity.hostEntity() )
135  {}
136 
137  EntityPointer ( const This &other )
138  : entity_( other.entityImpl() ),
139  hostEntityIterator_( other.hostEntityIterator_ )
140  {}
141 
142  template< class T >
143  explicit EntityPointer ( const EntityPointer< T, fake > &other )
144  : entity_( other.entityImpl() ),
145  hostEntityIterator_( other.hostEntityIterator_ )
146  {}
147 
148  const This &operator= ( const This &other )
149  {
150  entityImpl() = other.entityImpl();
151  hostEntityIterator_ = other.hostEntityIterator_;
152  return *this;
153  }
154 
155  operator const EntityPointerImp & () const
156  {
157  return reinterpret_cast< const EntityPointerImp & >( *this );
158  }
159 
160  template< class T >
161  const This &operator= ( const EntityPointer< T, fake > &other )
162  {
163  entityImpl() = other.entityImpl();
164  hostEntityIterator_ = other.hostEntityIterator_;
165  return *this;
166  }
167 
168  template< class T >
169  bool equals ( const EntityPointer< T, fake > &other ) const
170  {
171  return (hostIterator() == other.hostIterator());
172  }
173 
174  Entity &dereference () const
175  {
176  if( !entityImpl() )
177  entityImpl().initialize( *hostIterator() );
178  return entity_;
179  }
180 
181  int level () const { return hostIterator().level(); }
182 
183  const HostEntityIterator &hostIterator() const { return hostEntityIterator_; }
184 
185  const Grid &grid () const { return entityImpl().grid(); }
186 
187  protected:
188  EntityImpl &entityImpl () const
189  {
190  return Grid::getRealImplementation( entity_ );
191  }
192 
193  private:
194  mutable Entity entity_;
195 
196  protected:
198  };
199 
200 
201 
202  // EntityPointer (fake)
203  // --------------------
204 
205  template< class Traits >
206  class EntityPointer< Traits, true >
207  {
209 
210  typedef typename Traits::Grid Grid;
211 
212  typedef EntityPointerTraits< Traits::codimension, const Grid > BaseTraits;
213  friend class EntityPointer< BaseTraits, true >;
214 
215  public:
216  static const int dimension = Traits::dimension;
217  static const int codimension = Traits::codimension;
218 
219  typedef typename Traits::Entity Entity;
220 
221  static const bool fake = Traits::fake;
222 
224 
225  protected:
226  typedef typename Traits::HostEntityPointer HostEntityPointer;
227  typedef typename Traits::HostElementIterator HostElementIterator;
228  typedef typename Traits::HostElement HostElement;
229 
230  typedef typename Traits::EntitySeed EntitySeed;
231 
234 
235  public:
236  EntityPointer ( const GeometryImpl &geo, const HostElementIterator &hostElementIterator, int subEntity )
237  : entity_( EntityImpl( geo, subEntity ) ),
238  hostElementIterator_( hostElementIterator )
239  {}
240 
241  EntityPointer ( const Grid &grid, const HostElementIterator &hostElementIterator, int subEntity )
242  : entity_( EntityImpl( grid, subEntity ) ),
243  hostElementIterator_( hostElementIterator )
244  {}
245 
246  EntityPointer ( const Grid &grid, const HostElement &hostElement, int subEntity )
247  : entity_( EntityImpl( grid, subEntity ) ),
248  hostElementIterator_( hostElement )
249  {}
250 
251  EntityPointer ( const Grid &grid, const EntitySeed &seed )
252  : entity_( EntityImpl( grid, seed.subEntity() ) ),
253  hostElementIterator_( grid.hostGrid().entityPointer( seed.hostElementSeed() ) )
254  {}
255 
256  explicit EntityPointer ( const EntityImpl &entity )
257  : entity_( entity ),
258  hostElementIterator_( entity.hostElement() )
259  {}
260 
261  EntityPointer ( const This &other )
262  : entity_( other.entityImpl() ),
263  hostElementIterator_( other.hostElementIterator_ )
264  {}
265 
266  template< class T >
267  explicit EntityPointer ( const EntityPointer< T, fake > &other )
268  : entity_( other.entityImpl() ),
269  hostElementIterator_( other.hostElementIterator_ )
270  {}
271 
272  const This &operator= ( const This &other )
273  {
274  entityImpl() = other.entityImpl();
275  hostElementIterator_ = other.hostElementIterator_;
276  return *this;
277  }
278 
279  operator const EntityPointerImp & () const
280  {
281  return reinterpret_cast< const EntityPointerImp & >( *this );
282  }
283 
284  template< class T >
285  const This &operator= ( const EntityPointer< T, fake > &other )
286  {
287  entityImpl() = other.entityImpl();
288  hostElementIterator_ = other.hostElementIterator_;
289  return *this;
290  }
291 
292  template< class T >
293  bool equals ( const EntityPointer< T, fake > &other ) const
294  {
295  const bool thisEnd = (subEntity() < 0);
296  const bool otherEnd = (other.subEntity() < 0);
297  if( thisEnd || otherEnd )
298  return thisEnd && otherEnd;
299 
300  const int lvl = level();
301  if( lvl != other.level() )
302  return false;
303 
304  const typename Traits::HostGrid::Traits::LevelIndexSet &indexSet
305  = grid().hostGrid().levelIndexSet( lvl );
306 
307  const HostElement &thisElement = *hostElementIterator();
308  assert( indexSet.contains( thisElement ) );
309  const HostElement &otherElement = *(other.hostElementIterator());
310  assert( indexSet.contains( otherElement ) );
311 
312  const int thisIndex = indexSet.subIndex( thisElement, subEntity(), codimension );
313  const int otherIndex = indexSet.subIndex( otherElement, other.subEntity(), codimension );
314  return (thisIndex == otherIndex);
315  }
316 
317  Entity &dereference () const
318  {
319  if( !entityImpl() )
320  entityImpl().initialize( *hostElementIterator() );
321  return entity_;
322  }
323 
324  int level () const { return hostElementIterator().level(); }
325 
326  const Grid &grid () const { return entityImpl().grid(); }
327  int subEntity () const { return entityImpl().subEntity(); }
328 
329  protected:
330  EntityImpl &entityImpl () const
331  {
332  return Grid::getRealImplementation( entity_ );
333  }
334 
335  const HostElementIterator &hostElementIterator () const
336  {
337  return hostElementIterator_;
338  }
339 
340  private:
341  mutable Entity entity_;
342 
343  protected:
345  };
346 
347  } // namespace GeoGrid
348 
349 } // namespace Dune
350 
351 #endif // #ifndef DUNE_GEOGRID_ENTITYPOINTER_HH