geometrygrid/entitypointer.hh

Go to the documentation of this file.
00001 #ifndef DUNE_GEOGRID_ENTITYPOINTER_HH
00002 #define DUNE_GEOGRID_ENTITYPOINTER_HH
00003 
00004 #include <dune/grid/common/grid.hh>
00005 
00006 #include <dune/grid/geometrygrid/capabilities.hh>
00007 #include <dune/grid/geometrygrid/entityseed.hh>
00008 
00009 namespace Dune
00010 {
00011 
00012   // External Forward Declarations
00013   // -----------------------------
00014 
00015   template< class HostGrid, class CoordFunction, class Allocator >
00016   class GeometryGrid;
00017 
00018 
00019 
00020   namespace GeoGrid
00021   {
00022 
00023     // External Forward Declarations
00024     // -----------------------------
00025 
00026     template< int, int, class >
00027     class Entity;
00028 
00029     template< class HostGrid, class CoordFunction >
00030     struct ExportParams;
00031     
00032 
00033 
00034 
00035     // Internal Forward Declarations
00036     // -----------------------------
00037 
00038     template< int codim, class Grid >
00039     struct EntityPointerTraits;
00040 
00041     template< class Traits, bool fake = Traits::fake >
00042     class EntityPointer;
00043 
00044 
00045 
00046     // EntityPointerTraits
00047     // -------------------
00048     
00049     template< int codim, class Grid >
00050     struct EntityPointerTraits;
00051 
00053     template< int codim, class Grid >
00054     struct EntityPointerTraits< codim, const Grid >
00055     : public EntityPointerTraits< codim, Grid >
00056     {};
00059     template< int codim, class HostGrid, class CoordFunction, class Allocator >
00060     struct EntityPointerTraits< codim, GeometryGrid< HostGrid, CoordFunction, Allocator > >
00061     : public ExportParams< HostGrid, CoordFunction >
00062     {
00063       typedef Dune::GeometryGrid< HostGrid, CoordFunction, Allocator > Grid;
00064 
00065       static const bool fake = !Capabilities::hasHostEntity< Grid, codim >::v;
00066 
00067       typedef typename HostGrid::ctype ctype;
00068 
00069       static const int dimension = HostGrid::dimension;
00070       static const int codimension = codim;
00071 
00072       typedef Dune::Entity< codimension, dimension, const Grid, GeoGrid::Entity > Entity;
00073       typedef GeoGrid::EntitySeed< codimension, const Grid > EntitySeed;
00074               
00075       typedef typename HostGrid::template Codim< codim >::Entity HostEntity;
00076       typedef typename HostGrid::template Codim< codim >::EntityPointer HostEntityPointer;
00077       typedef HostEntityPointer HostEntityIterator;
00078       
00079       typedef typename HostGrid::template Codim< 0 >::Entity HostElement;
00080       typedef typename HostGrid::template Codim< 0 >::EntityPointer HostElementIterator;
00081     };
00082 
00083 
00084 
00085     // EntityPointer (real)
00086     // --------------------
00087 
00088     template< class Traits >
00089     class EntityPointer< Traits, false >
00090     {
00091       typedef EntityPointer< Traits, false > This;
00092 
00093       typedef typename Traits::Grid Grid;
00094     
00095       typedef EntityPointerTraits< Traits::codimension, const Grid > BaseTraits;
00096       friend class EntityPointer< BaseTraits, false >;
00097 
00098     public:
00099       static const int dimension = Traits::dimension;
00100       static const int codimension = Traits::codimension;
00101    
00102       typedef typename Traits::Entity Entity;
00103 
00104       static const bool fake = Traits::fake;
00105      
00106       typedef EntityPointer< BaseTraits, fake > EntityPointerImp;
00107 
00108     protected:
00109       typedef typename Traits::HostEntityPointer HostEntityPointer;
00110       typedef typename Traits::HostEntityIterator HostEntityIterator;
00111       typedef typename Traits::HostElement HostElement;
00112 
00113       typedef typename Traits::EntitySeed EntitySeed;
00114 
00115     private:
00116       typedef MakeableInterfaceObject< Entity > MakeableEntity;
00117       typedef typename MakeableEntity::ImplementationType EntityImpl;
00118 
00119     public:
00120       EntityPointer ( const Grid &grid, const HostEntityIterator &hostEntityIterator )
00121       : grid_( &grid ),
00122         entity_( 0 ),
00123         hostEntityIterator_( hostEntityIterator )
00124       {}
00125 
00126       EntityPointer ( const Grid &grid, const HostElement &hostElement, int subEntity )
00127       : grid_( &grid ),
00128         entity_( 0 ),
00129         hostEntityIterator_( hostElement.template subEntity< codimension >( subEntity ) )
00130       {}
00131 
00132       EntityPointer ( const Grid &grid, const EntitySeed &seed )
00133       : grid_( &grid ),
00134         entity_( 0 ),
00135         hostEntityIterator_( grid.hostGrid().entityPointer( seed.hostEntitySeed() ) )
00136       {}
00137 
00138       EntityPointer ( const EntityImpl &entity )
00139       : grid_( &entity.grid() ),
00140         entity_( 0 ),
00141         hostEntityIterator_( entity.hostEntity() )
00142       {}
00143 
00144       EntityPointer ( const This &other )
00145       : grid_( other.grid_ ),
00146         entity_( 0 ),
00147         hostEntityIterator_( other.hostEntityIterator_ )
00148       {}
00149 
00150       template< class T >
00151       explicit EntityPointer ( const EntityPointer< T, fake > &other )
00152       : grid_( other.grid_ ),
00153         entity_( 0 ),
00154         hostEntityIterator_( other.hostEntityIterator_ )
00155       {}
00156       
00157       ~EntityPointer ()
00158       {
00159         if( entity_ )
00160           entityAllocator().deallocate( entity_ );
00161       }
00162 
00163       This &operator= ( const This &other )
00164       {
00165         grid_ = other.grid_;
00166         hostEntityIterator_ = other.hostEntityIterator_;
00167         releaseEntity();
00168         return *this;
00169       }
00170 
00171       operator const EntityPointerImp & () const
00172       {
00173         return reinterpret_cast< const EntityPointerImp & >( *this );
00174       }
00175 
00176       template< class T >
00177       bool equals ( const EntityPointer< T, fake > &other ) const
00178       {
00179         return (hostIterator() == other.hostIterator());
00180       }
00181       
00182       Entity &dereference () const
00183       {
00184         if( !entity_ )
00185           entity_ = entityAllocator().allocate( EntityImpl( grid(), *hostIterator() ) );
00186         return *entity_;
00187       }
00188       
00189       int level () const
00190       {
00191         return hostIterator().level();
00192       }
00193 
00194       void compactify ()
00195       {
00196         hostEntityIterator_.compactify();
00197         releaseEntity();
00198       }
00199 
00200       const HostEntityIterator &hostIterator() const
00201       {
00202         return hostEntityIterator_;
00203       }
00204 
00205       const Grid &grid () const
00206       {
00207         return *grid_;
00208       }
00209 
00210     protected:
00211       void releaseEntity ()
00212       {
00213         if( entity_ )
00214         {
00215           entityAllocator().deallocate( entity_ );
00216           entity_ = 0;
00217         }
00218       }
00219 
00220       typename Grid::Traits::template EntityAllocator< codimension > &
00221       entityAllocator () const
00222       {
00223         return grid().template entityAllocator< codimension >();
00224       }
00225 
00226     private:
00227       const Grid *grid_;
00228       mutable MakeableEntity *entity_;
00229 
00230     protected:
00231       HostEntityIterator hostEntityIterator_;
00232     };
00233 
00234 
00235 
00236     // EntityPointer (fake)
00237     // --------------------
00238 
00239     template< class Traits >
00240     class EntityPointer< Traits, true >
00241     {
00242       typedef EntityPointer< Traits, true > This;
00243       
00244       typedef typename Traits::Grid Grid;
00245 
00246       typedef EntityPointerTraits< Traits::codimension, const Grid > BaseTraits;
00247       friend class EntityPointer< BaseTraits, true >;
00248 
00249     public:
00250       static const int dimension = Traits::dimension;
00251       static const int codimension = Traits::codimension;
00252    
00253       typedef typename Traits::Entity Entity;
00254 
00255       static const bool fake = Traits::fake;
00256 
00257       typedef EntityPointer< BaseTraits, fake > EntityPointerImp;
00258 
00259     protected:
00260       typedef typename Traits::HostEntityPointer HostEntityPointer;
00261       typedef typename Traits::HostElementIterator HostElementIterator;
00262       typedef typename Traits::HostElement HostElement;
00263 
00264       typedef typename Traits::EntitySeed EntitySeed;
00265 
00266     private:
00267       typedef MakeableInterfaceObject< Entity > MakeableEntity;
00268       typedef typename MakeableEntity::ImplementationType EntityImpl;
00269 
00270     public:
00271       EntityPointer ( const Grid &grid, const HostElementIterator &hostElementIterator, int subEntity )
00272       : grid_( &grid ),
00273         entity_( 0 ),
00274         subEntity_( subEntity ),
00275         hostElementIterator_( hostElementIterator )
00276       {}
00277 
00278       EntityPointer ( const Grid &grid, const HostElement &hostElement, int subEntity )
00279       : grid_( &grid ),
00280         entity_( 0 ),
00281         subEntity_( subEntity ),
00282         hostElementIterator_( hostElement )
00283       {}
00284 
00285       EntityPointer ( const Grid &grid, const EntitySeed &seed )
00286       : grid_( &grid ),
00287         entity_( 0 ),
00288         subEntity_( seed.subEntity() ),
00289         hostElementIterator_( grid.hostGrid().entityPointer( seed.hostElementSeed() ) )
00290       {}
00291 
00292       EntityPointer ( const EntityImpl &entity )
00293       : grid_( &entity.grid() ),
00294         entity_( 0 ),
00295         subEntity_( entity.subEntity() ),
00296         hostElementIterator_( entity.hostElement() )
00297       {}
00298 
00299       EntityPointer ( const This &other )
00300       : grid_( other.grid_ ),
00301         entity_( 0 ),
00302         subEntity_( other.subEntity_ ),
00303         hostElementIterator_( other.hostElementIterator_ )
00304       {}
00305 
00306       template< class T >
00307       explicit EntityPointer ( const EntityPointer< T, fake > &other )
00308       : grid_( other.grid_ ),
00309         entity_( 0 ),
00310         subEntity_( other.subEntity_ ),
00311         hostElementIterator_( other.hostElementIterator_ )
00312       {}
00313 
00314       ~EntityPointer ()
00315       {
00316         if( entity_ )
00317           entityAllocator().deallocate( entity_ );
00318       }
00319 
00320       This &operator= ( const This &other )
00321       {
00322         grid_ = other.grid_;
00323         subEntity_ = other.subEntity_;
00324         hostElementIterator_ = other.hostElementIterator_;
00325         releaseEntity();
00326         return *this;
00327       }
00328 
00329       operator const EntityPointerImp & () const
00330       {
00331         return reinterpret_cast< const EntityPointerImp & >( *this );
00332       }
00333 
00334       operator EntityPointerImp & ()
00335       {
00336         return reinterpret_cast< EntityPointerImp & >( *this );
00337       }
00338 
00339       template< class T >
00340       bool equals ( const EntityPointer< T, fake > &other ) const
00341       {
00342         const bool thisEnd = (subEntity_ < 0);
00343         const bool otherEnd = (other.subEntity_ < 0);
00344         if( thisEnd || otherEnd )
00345           return thisEnd && otherEnd;
00346 
00347         const int lvl = level();
00348         if( lvl != other.level() )
00349           return false;
00350 
00351         const typename Traits::HostGrid::Traits::LevelIndexSet &indexSet
00352           = grid().hostGrid().levelIndexSet( lvl );
00353 
00354         const HostElement &thisElement = *hostElementIterator();
00355         assert( indexSet.contains( thisElement ) );
00356         const HostElement &otherElement = *(other.hostElementIterator());
00357         assert( indexSet.contains( otherElement ) );
00358 
00359         const int thisIndex = indexSet.subIndex( thisElement, subEntity_, codimension );
00360         const int otherIndex = indexSet.subIndex( otherElement, other.subEntity_, codimension );
00361         return (thisIndex == otherIndex);
00362       }
00363       
00364       Entity &dereference () const
00365       {
00366         if( !entity_ )
00367           entity_ = entityAllocator().allocate( EntityImpl( grid(), *hostElementIterator(), subEntity_ ) );
00368         return *entity_;
00369       }
00370       
00371       int level () const
00372       {
00373         return hostElementIterator().level();
00374       }
00375 
00376       void compactify ()
00377       {
00378         hostElementIterator_.compactify();
00379         releaseEntity();
00380       }
00381 
00382       const Grid &grid () const
00383       {
00384         return *grid_;
00385       }
00386 
00387     protected:
00388       void releaseEntity ()
00389       {
00390         if( entity_ )
00391         {
00392           entityAllocator().deallocate( entity_ );
00393           entity_ = 0;
00394         }
00395       }
00396 
00397       const HostElementIterator &hostElementIterator () const
00398       {
00399         return hostElementIterator_;
00400       }
00401 
00402       typename Grid::Traits::template EntityAllocator< codimension > &
00403       entityAllocator () const
00404       {
00405         return grid().template entityAllocator< codimension >();
00406       }
00407 
00408     private:
00409       const Grid *grid_;
00410       mutable MakeableEntity *entity_;
00411 
00412     protected:
00413       int subEntity_;
00414       HostElementIterator hostElementIterator_;
00415     };
00416 
00417   }
00418 
00419 }
00420 
00421 #endif // #ifndef DUNE_GEOGRID_ENTITYPOINTER_HH

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].