dune-grid  2.1.1
geometrygrid/datahandle.hh
Go to the documentation of this file.
00001 #ifndef DUNE_GEOGRID_DATAHANDLE_HH
00002 #define DUNE_GEOGRID_DATAHANDLE_HH
00003 
00004 #include <dune/common/typetraits.hh>
00005 #include <dune/grid/common/datahandleif.hh>
00006 #include <dune/grid/common/grid.hh>
00007 
00008 #include <dune/grid/geometrygrid/capabilities.hh>
00009 #include <dune/grid/geometrygrid/entity.hh>
00010 
00011 namespace Dune
00012 {
00013 
00014   namespace GeoGrid
00015   {
00016 
00017     // GeometryGridDataHandle
00018     // ----------------------
00019 
00020     template< class Grid, class WrappedHandle >
00021     class CommDataHandle
00022     : public CommDataHandleIF< CommDataHandle< Grid, WrappedHandle >, typename WrappedHandle::DataType >
00023     {
00024       typedef typename remove_const< Grid >::type::Traits Traits;
00025 
00026       template< class HostEntity >
00027       class EntityProxy;
00028 
00029     public:
00030       CommDataHandle ( const Grid &grid, WrappedHandle &handle )
00031       : grid_( grid ),
00032         wrappedHandle_( handle )
00033       {}
00034 
00035       bool contains ( int dim, int codim ) const
00036       {
00037         const bool contains = wrappedHandle_.contains( dim, codim );
00038         if( contains )
00039           assertHostEntity( dim, codim );
00040         return contains;
00041       }
00042 
00043       bool fixedsize ( int dim, int codim ) const
00044       {
00045         return wrappedHandle_.fixedsize( dim, codim );
00046       }
00047 
00048       template< class HostEntity >
00049       size_t size ( const HostEntity &hostEntity ) const
00050       {
00051         EntityProxy< HostEntity > proxy( grid_, hostEntity );
00052         return wrappedHandle_.size( *proxy );
00053       }
00054 
00055       template< class MessageBuffer, class HostEntity >
00056       void gather ( MessageBuffer &buffer, const HostEntity &hostEntity ) const
00057       {
00058         EntityProxy< HostEntity > proxy( grid_, hostEntity );
00059         wrappedHandle_.gather( buffer, *proxy );
00060       }
00061 
00062       template< class MessageBuffer, class HostEntity >
00063       void scatter ( MessageBuffer &buffer, const HostEntity &hostEntity, size_t size )
00064       {
00065         EntityProxy< HostEntity > proxy( grid_, hostEntity );
00066         wrappedHandle_.scatter( buffer, *proxy, size );
00067       }
00068 
00069     private:
00070       static void assertHostEntity ( int dim, int codim )
00071       {
00072         if( !Capabilities::CodimCache< Grid >::hasHostEntity( codim ) )
00073           noEntity( codim );
00074       }
00075 
00076       static void noEntity ( int codim )
00077       {
00078         DUNE_THROW( NotImplemented, "Host grid has no entities for codimension "
00079                                     << codim << "." );
00080       }
00081 
00082       const Grid &grid_;
00083       WrappedHandle &wrappedHandle_;
00084     };
00085 
00086 
00087 
00088     template< class Grid, class WrappedHandle >
00089     template< class HostEntity >
00090     class CommDataHandle< Grid, WrappedHandle >::EntityProxy
00091     {
00092       static const int codimension = HostEntity::codimension;
00093       typedef typename Traits::template Codim< codimension >::Entity Entity;
00094       typedef MakeableInterfaceObject< Entity > MakeableEntity;
00095       typedef typename MakeableEntity::ImplementationType EntityImpl;
00096 
00097       template< bool >
00098       struct CreateReal
00099       {
00100         static MakeableEntity
00101         apply ( const Grid &grid, const HostEntity &hostEntity )
00102         {
00103           return MakeableEntity( EntityImpl( grid, hostEntity ) );
00104         }
00105       };
00106 
00107       template< bool >
00108       struct CreateFake
00109       {
00110         static MakeableEntity
00111         apply ( const Grid &grid, const HostEntity &hostEntity )
00112         {
00113           DUNE_THROW( NotImplemented, "Host grid has no entities for codimension "
00114                                       << codimension << "." );
00115         }
00116       };
00117 
00118       static const bool hasHostEntity = Capabilities::hasHostEntity< Grid, codimension >::v;
00119       typedef typename SelectType< hasHostEntity, CreateReal< true >, CreateFake< false > >::Type Create;
00120 
00121     public:
00122       EntityProxy ( const Grid &grid, const HostEntity &hostEntity )
00123       : entity_(new MakeableEntity(Create::apply( grid, hostEntity ) ) )
00124       {}
00125 
00126       ~EntityProxy ()
00127       {
00128         delete entity_;
00129       }
00130 
00131       const Entity &operator* () const
00132       {
00133         return *entity_;
00134       }
00135 
00136     private:
00137       MakeableEntity *entity_;
00138     };
00139 
00140   }
00141 
00142 }
00143 
00144 #endif // #ifndef DUNE_GEOGRID_DATAHANDLE_HH