dune-grid  2.5.1
common/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_GRID_ENTITYPOINTER_HH
4 #define DUNE_GRID_ENTITYPOINTER_HH
5 
6 #include <utility>
7 
8 #include <dune/common/proxymemberaccess.hh>
9 #include <dune/common/iteratorfacades.hh>
10 #include <dune/common/deprecated.hh>
11 #include <dune/geometry/type.hh>
14 
19 #define DUNE_ENTITYPOINTER_DEPRECATED_MSG DUNE_DEPRECATED_MSG("EntityPointer is deprecated and will be removed after the release of dune-grid-2.4. Instead, you can copy and store entities directly now. Note, this might lead to a decreased performance until all grid implementations properly addressed this interface change.")
20 namespace Dune
21 {
22 
23  // External forward declaration
24  // ----------------------------
25 
26  template< int, int, class, template< int, int, class > class >
27  class Entity;
28 
29 
30 
113  template<class GridImp, class IteratorImp>
115  {
116  // need to make copy constructor of EntityPointer work for any iterator
117  //friend class EntityPointer<GridImp,typename IteratorImp::EntityPointerImp>;
118  template< class, class > friend class EntityPointer;
119 
120 #if DUNE_GRID_EXPERIMENTAL_GRID_EXTENSIONS
121  public:
122 #else
123  protected:
124  // give the GridDefaultImplementation class access to the realImp
125  friend class GridDefaultImplementation<
126  GridImp::dimension, GridImp::dimensionworld,
127  typename GridImp::ctype,
128  typename GridImp::GridFamily> ;
129 #endif
130  // type of underlying implementation, for internal use only
131  typedef IteratorImp Implementation;
132 
136  const Implementation &impl () const { return realIterator; }
137 
138  protected:
140 
141  public:
143  enum { codimension = IteratorImp::codimension };
144 
146  typedef typename IteratorImp::Entity Entity;
147 
149  typedef typename std::conditional<
150  std::is_lvalue_reference<
151  decltype(realIterator.dereference())
152  >::value,
153  const Entity&,
154  Entity
156 
157  //===========================================================
161  //===========================================================
162 
169  template< class ItImp >
171  : realIterator( ep.realIterator )
172  {}
173 
176  {}
177 
185  EntityPointer(const Entity& entity)
186  : realIterator( entity.impl() )
187  {}
188 
194  EntityPointer ( const typename Entity::Implementation &entityImp )
195  : realIterator( entityImp )
196  {}
197 
198  template< class ItImp >
201  {
202  realIterator = ep.realIterator;
203  return *this;
204  }
205 
207 
208  //===========================================================
212  //===========================================================
213 
214  // The behavior when dereferencing the EntityPointer facade depends on
215  // the way the grid implementation handles returning entities. The implementation
216  // may either return a reference to an entity stored inside the EntityPointer
217  // implementation or a temporary Entity object. This object has to be forwarded through
218  // the facade to the user, which requires a little trickery, especially for operator->().
219  //
220  // In order to avoid confusing users reading the Doxygen documentation, we provide "clean"
221  // function signatures to Doxygen and hide the actual implementations.
222 
223 #ifdef DOXYGEN
224 
226  Entity operator*() const
228 
229 
231  const Entity* operator->() const
233 
234 #else // DOXYGEN
235 
237  Reference
238  operator*() const
239  DUNE_ENTITYPOINTER_DEPRECATED_MSG
240  {
241  return realIterator.dereference();
242  }
243 
245  decltype(handle_proxy_member_access(realIterator.dereference()))
246  operator->() const
247  DUNE_ENTITYPOINTER_DEPRECATED_MSG
248  {
249  return handle_proxy_member_access(realIterator.dereference());
250  }
251 
252  template<typename T>
253  // this construction, where the deprecation warning is triggered by a separate function,
254  // is slightly convoluted, but I could not get the warning to trigger reliably when attached
255  // directly to the cast operator.
256  DUNE_DEPRECATED_MSG("The implicit cast from EntityPointer to an Entity reference is DANGEROUS. It's mainly there for writing backwards compatible code that doesn't trigger a deprecation warning for ported grids and must ONLY be used if the returned reference is used in an rvalue-like setting!")
257  void trigger_entity_cast_warning() const
258  {}
259 
260  template<typename T, typename std::enable_if<std::is_same<T,Entity>::value,int>::type = 0>
261  operator const T&() const
262  {
263  static_assert(std::is_same<T,Entity>::value,"invalid cast");
264  trigger_entity_cast_warning<T>();
265  return realIterator.dereference();
266  }
267 
268 #endif // DOXYGEN
269 
271 
272  //===========================================================
276  //===========================================================
277 
283  template< class ItImp >
285  {
286  return equals( rhs );
287  }
288 
294  template< class ItImp >
296  {
297  return !equals( rhs );
298  }
300 
306  DUNE_ENTITYPOINTER_DEPRECATED_MSG
307  bool operator==(const Entity& rhs) const
308  {
309  return (**this) == rhs;
310  }
311 
317  DUNE_ENTITYPOINTER_DEPRECATED_MSG
318  bool operator!=(const Entity& rhs) const
319  {
320  return (**this) != rhs;
321  }
322 
323 
324  //===========================================================
328  //===========================================================
329 
339  int level () const
340  DUNE_ENTITYPOINTER_DEPRECATED_MSG
341  {
342  return realIterator.level();
343  }
344 
346 
347 
348  //===========================================================
352  //===========================================================
353 
359  EntityPointer(const IteratorImp & i) :
360  realIterator(i) {}
361 
363  template< class ItImp >
364  bool equals ( const EntityPointer< GridImp, ItImp > &rhs ) const
365  {
366  return realIterator.equals( rhs.realIterator );
367  }
369 
370  //===========================================================
374  //===========================================================
375 
377  typedef typename GridImp::template Codim<codimension>::Geometry Geometry;
378 
380  typedef typename GridImp::template Codim<codimension>::EntitySeed EntitySeed;
381 
389  typedef typename GridImp::template Codim<codimension>::LocalGeometry LocalGeometry;
390 
392  template <int cd>
393  struct Codim
394  {
395  typedef typename GridImp::template Codim<cd>::EntityPointer EntityPointer;
396  typedef typename GridImp::template Codim<cd>::Entity Entity;
397  };
398 
400  // typedef typename Entity::EntityPointer EntityPointer;
401 
403  typedef typename GridImp::HierarchicIterator HierarchicIterator;
404 
405  enum {
408  };
409  enum {
412  };
413 
415  PartitionType partitionType () const { return realIterator.dereference().partitionType(); }
416 
429  Geometry geometry () const { return realIterator.dereference().geometry(); }
430 
434  GeometryType type () const { return realIterator.dereference().type(); }
435 
439  EntitySeed seed () const { return realIterator.dereference().seed(); }
440 
441 #define CHECK_CODIM0 int ecodim = codimension, typename std::enable_if<ecodim == 0,int>::type = 0
442 #define ONLY_CODIM0 template<int ecodim = codimension, typename std::enable_if<ecodim == 0,int>::type = 0>
443 
444  template< int codim, CHECK_CODIM0 >
445  typename Codim<codim>::Entity
446  subEntity ( int i ) const
447  {
448  return realIterator.dereference().template subEntity< codim >(i);
449  }
450 
455  bool hasFather () const
456  {
457  return realIterator.dereference().hasFather();
458  }
459 
462  bool isLeaf () const
463  {
464  return realIterator.dereference().isLeaf();
465  }
466 
471  bool isRegular() const { return realIterator.dereference().isRegular(); }
472 
499  LocalGeometry geometryInFather () const { return realIterator.dereference().geometryInFather(); }
500 
513  HierarchicIterator hbegin (int maxLevel) const
514  {
515  return realIterator.dereference().hbegin(maxLevel);
516  }
517 
526  HierarchicIterator hend (int maxLevel) const
527  {
528  return realIterator.dereference().hend(maxLevel);
529  }
530 
534  bool isNew () const { return realIterator.dereference().isNew(); }
535 
541  bool mightVanish () const { return realIterator.dereference().mightVanish(); }
542 
546  bool hasBoundaryIntersections () const { return realIterator.dereference().hasBoundaryIntersections(); }
548 
549  };
550 
551 
552 
553 #ifndef DOXYEN
554 
555  // DefaultEntityPointer
556  // --------------------
557 
558  /* The EntityPointer class defined above has been deprecated. Unitil its
559  final removal valid Dune grids still have to provide at least a suitable
560  EntityPointer typedef. This class provides a default implementation of an
561  entity pointer from a given Dune::Entity type:
562  \code
563  struct GridFamily
564  {
565  ...
566  typedef ImplementationDefined Entity;
567  typedef DefaultEntityPointer<Entity> EntityPointer;
568  ...
569  };
570  \endcode
571 
572  This class will retain a possible compatibility support with the
573  deprecated interface behavior if the iterator classes in the grid
574  implementation provide the following two additional methods:
575  \code
576  class Iterator
577  {
578  // dereference() method required by Dune::EntityIterator
579  typedef ImplemenatationDefined Entity;
580  Entity dereference () const;
581 
582  // allow for (deprecated) construction/assignment of EntityPointer from a given iterator
583  operator Dune::DefaultEntityPointer<Entity>() const
584  {
585  return Dune::DefaultEntityPointer<Entity>(dereference());
586  }
587 
588  // allow for (deprecated) comparison of an iterator with an entity pointer
589  bool equals(const Dune::DefaultEntityPointer<Entity> &rhs) const
590  {
591  return dereference() == rhs.dereference();
592  }
593  };
594  \endcode
595  */
596  template< class E >
598 
599  template< int codim, int dim, class Grid, template< int, int, class > class EntityImp >
600  class DefaultEntityPointer< Dune::Entity< codim, dim, Grid, EntityImp > >
601  {
602  public:
603  static const int codimension = codim;
604 
606 
608 
609  explicit DefaultEntityPointer ( Entity entity )
610  : entity_( std::move( entity ) )
611  {}
612 
613  explicit DefaultEntityPointer ( EntityImp< codim, dim, Grid > entity )
614  : entity_( std::move( entity ) )
615  {}
616 
617  const Entity &dereference () const { return entity_; }
618 
619  bool equals ( const DefaultEntityPointer &rhs ) const
620  {
621  return entity_ == rhs.entity_;
622  }
623 
624  int level () const { return entity_.level(); }
625 
626  private:
627  Entity entity_;
628  };
629 
630 #endif // #ifndef DOXYEN
631 
632 }
633 #undef DUNE_ENTITYPOINTER_DEPRECATED_MSG
634 
635 #endif // DUNE_GRID_ENTITYPOINTER_HH
Implementation realIterator
Definition: common/entitypointer.hh:139
ONLY_CODIM0 bool isRegular() const
Returns true if element is of regular type in red/green type refinement. In bisection or hanging node...
Definition: common/entitypointer.hh:471
#define ONLY_CODIM0
Definition: common/entitypointer.hh:442
ONLY_CODIM0 HierarchicIterator hend(int maxLevel) const
Returns iterator to one past the last son element.
Definition: common/entitypointer.hh:526
GridImp::template Codim< cd >::EntityPointer EntityPointer
Definition: common/entitypointer.hh:395
Implementation & impl()
return reference to the real implementation
Definition: common/entitypointer.hh:134
Definition: common/entitypointer.hh:597
ONLY_CODIM0 bool hasFather() const
Return true if entity has a father entity which can be accessed using the father() method...
Definition: common/entitypointer.hh:455
GeometryType
Type representing VTK&#39;s entity geometry types.
Definition: common.hh:178
PartitionType
Attributes used in the generic overlap model.
Definition: gridenums.hh:28
DUNE_ENTITYPOINTER_DEPRECATED_MSG bool operator==(const Entity &rhs) const
Compares an EntityPointer with an Entity for equality.
Definition: common/entitypointer.hh:307
#define DUNE_ENTITYPOINTER_DEPRECATED_MSG
Definition: common/entitypointer.hh:19
ONLY_CODIM0 bool isNew() const
Returns true, if the entity has been created during the last call to adapt()
Definition: common/entitypointer.hh:534
Definition: common/geometry.hh:24
GridImp::template Codim< codimension >::Geometry Geometry
The geometry type of this entity.
Definition: common/entitypointer.hh:377
bool equals(const DefaultEntityPointer &rhs) const
Definition: common/entitypointer.hh:619
DUNE_ENTITYPOINTER_DEPRECATED_MSG EntityPointer(const Entity &entity)
Templatized constructor from type of entity that this entity pointer points to. This constructor can ...
Definition: common/entitypointer.hh:185
ONLY_CODIM0 bool mightVanish() const
Returns true, if entity might disappear during the next call to adapt(). If the method returns false...
Definition: common/entitypointer.hh:541
bool operator!=(const EntityPointer< GridImp, ItImp > &rhs) const
Checks for inequality. Only works for EntityPointers and iterators on the same grid. Due to the conversion operators one can compare all kinds of iterators and EntityPointer.
Definition: common/entitypointer.hh:295
std::conditional< std::is_lvalue_reference< decltype(realIterator.dereference()) >::value, const Entity &, Entity >::type Reference
Tpy of the reference used when derefencing the Ptr.
Definition: common/entitypointer.hh:155
ONLY_CODIM0 HierarchicIterator hbegin(int maxLevel) const
Inter-level access to elements that resulted from (recursive) subdivision of this element...
Definition: common/entitypointer.hh:513
const Entity * operator->() const DUNE_ENTITYPOINTER_DEPRECATED_MSG
Pointer operator.
ONLY_CODIM0 bool isLeaf() const
Returns true if the entity is contained in the leaf grid.
Definition: common/entitypointer.hh:462
bool equals(const EntityPointer< GridImp, ItImp > &rhs) const
Forward equality check to realIterator.
Definition: common/entitypointer.hh:364
GridImp::template Codim< codimension >::LocalGeometry LocalGeometry
The geometry type of this entity when the geometry is expressed embedded in the father element...
Definition: common/entitypointer.hh:389
const Implementation & impl() const
return reference to the real implementation
Definition: common/entitypointer.hh:136
EntityPointer(const typename Entity::Implementation &entityImp)
Constructor from type of entity implementation that this entity pointer points to. This constructor is only used in the EntityDefaultImplementation to implement the method seed() by default when the type of EntitySeed and EntityPointer coniside.
Definition: common/entitypointer.hh:194
Dune::Entity< codim, dim, Grid, EntityImp > Entity
Definition: common/entitypointer.hh:605
Know the grid&#39;s dimension.
Definition: common/entitypointer.hh:407
DUNE_ENTITYPOINTER_DEPRECATED_MSG EntityPointer & operator=(const EntityPointer< GridImp, ItImp > &ep)
Definition: common/entitypointer.hh:200
Know the grid dimension.
Definition: common/entity.hh:111
int level() const
Definition: common/entitypointer.hh:624
Know dimension of the entity.
Definition: common/entitypointer.hh:411
GridImp::template Codim< codimension >::EntitySeed EntitySeed
The corresponding entity seed (for storage of entities)
Definition: common/entitypointer.hh:380
IteratorImp::Entity Entity
The Entity that this EntityPointer can point to.
Definition: common/entitypointer.hh:146
GridImp::HierarchicIterator HierarchicIterator
The codim==0 EntityPointer type.
Definition: common/entitypointer.hh:403
IteratorImp Implementation
Definition: common/entitypointer.hh:131
Codim< codim >::Entity subEntity(int i) const
Definition: common/entitypointer.hh:446
const Entity & dereference() const
Definition: common/entitypointer.hh:617
EntitySeed seed() const
Return the entity seed which contains sufficient information to generate the entity again and uses as...
Definition: common/entitypointer.hh:439
Include standard header files.
Definition: agrid.hh:59
EntityPointer(const IteratorImp &i)
Copy Constructor from an Iterator implementation.
Definition: common/entitypointer.hh:359
DefaultEntityPointer(EntityImp< codim, dim, Grid > entity)
Definition: common/entitypointer.hh:613
EntityPointer types of the different codimensions.
Definition: common/entitypointer.hh:393
GridImp::template Codim< cd >::Entity Entity
Definition: common/entitypointer.hh:396
Definition: common/entitypointer.hh:143
Wrapper and interface classes for element geometries.
EntityPointer()
Default constructor of an empty (undefined) EntityPointer.
Definition: common/entitypointer.hh:175
Entity operator*() const DUNE_ENTITYPOINTER_DEPRECATED_MSG
Dereferencing operator.
bool operator==(const EntityPointer< GridImp, ItImp > &rhs) const
Checks for equality. Only works for EntityPointers and iterators on the same grid. Due to the conversion operators one can compare all kinds of iterators and EntityPointer.
Definition: common/entitypointer.hh:284
int level() const DUNE_ENTITYPOINTER_DEPRECATED_MSG
Ask for level of entity.
Definition: common/entitypointer.hh:339
DUNE_ENTITYPOINTER_DEPRECATED_MSG bool operator!=(const Entity &rhs) const
Compares an EntityPointer with an Entity for inequality.
Definition: common/entitypointer.hh:318
ONLY_CODIM0 LocalGeometry geometryInFather() const
Provides information how this element has been subdivided from its father element.
Definition: common/entitypointer.hh:499
PartitionType partitionType() const
Partition type of this entity.
Definition: common/entitypointer.hh:415
STL namespace.
GeometryType type() const
Return the name of the reference element. The type can be used to access the Dune::ReferenceElement.
Definition: common/entitypointer.hh:434
DefaultEntityPointer(Entity entity)
Definition: common/entitypointer.hh:609
EntityImp< cd, dim, GridImp > Implementation
Definition: common/entity.hh:81
ONLY_CODIM0 bool hasBoundaryIntersections() const
Returns true, if entity has intersections with boundary.
Definition: common/entitypointer.hh:546
EntityPointer(const EntityPointer< GridImp, ItImp > &ep)
Templatized copy constructor from arbitrary IteratorImp. This enables that an EntityPointer can be co...
Definition: common/entitypointer.hh:170
Wrapper class for pointers to entities.
Definition: common/entitypointer.hh:114
Geometry geometry() const
obtain geometric realization of the entity
Definition: common/entitypointer.hh:429