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