dune-grid  2.2.0
geometrygrid/datahandle.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOGRID_DATAHANDLE_HH
2 #define DUNE_GEOGRID_DATAHANDLE_HH
3 
4 #include <dune/common/typetraits.hh>
5 
10 
11 namespace Dune
12 {
13 
14  namespace GeoGrid
15  {
16 
17  template< int codim, class Grid >
19  {
20  typedef typename remove_const< Grid >::type::Traits Traits;
21 
22  typedef typename Traits::template Codim< codim >::Entity Entity;
24  typedef typename EntityImpl::HostEntity HostEntity;
25 
26  template< bool >
27  struct InitReal
28  {
29  static void apply ( EntityImpl &entityImpl, const HostEntity &hostEntity )
30  {
31  entityImpl.initialize( hostEntity );
32  }
33  };
34 
35  template< bool >
36  struct InitFake
37  {
38  static void apply ( EntityImpl &entityImpl, const HostEntity &hostEntity )
39  {
40  DUNE_THROW( NotImplemented, "Host grid has no entities for codimension " << codim << "." );
41  }
42  };
43 
44  static const bool hasHostEntity = Capabilities::hasHostEntity< Grid, codim >::v;
45  typedef typename SelectType< hasHostEntity, InitReal< true >, InitFake< false > >::Type Init;
46 
47  public:
48  EntityProxy ( const Grid &grid, const HostEntity &hostEntity )
49  : entity_( EntityImpl( grid ) )
50  {
51  Init::apply( Grid::getRealImplementation( entity_ ), hostEntity );
52  }
53 
54  const Entity &operator* () const
55  {
56  return entity_;
57  }
58 
59  private:
60  Entity entity_;
61  };
62 
63 
64 
65  // GeometryGridDataHandle
66  // ----------------------
67 
68  template< class Grid, class WrappedHandle >
70  : public CommDataHandleIF< CommDataHandle< Grid, WrappedHandle >, typename WrappedHandle::DataType >
71  {
72  typedef typename remove_const< Grid >::type::Traits Traits;
73 
74  public:
75  CommDataHandle ( const Grid &grid, WrappedHandle &handle )
76  : grid_( grid ),
77  wrappedHandle_( handle )
78  {}
79 
80  bool contains ( int dim, int codim ) const
81  {
82  const bool contains = wrappedHandle_.contains( dim, codim );
83  if( contains )
84  assertHostEntity( dim, codim );
85  return contains;
86  }
87 
88  bool fixedsize ( int dim, int codim ) const
89  {
90  return wrappedHandle_.fixedsize( dim, codim );
91  }
92 
93  template< class HostEntity >
94  size_t size ( const HostEntity &hostEntity ) const
95  {
96  EntityProxy< HostEntity::codimension, Grid > proxy( grid_, hostEntity );
97  return wrappedHandle_.size( *proxy );
98  }
99 
100  template< class MessageBuffer, class HostEntity >
101  void gather ( MessageBuffer &buffer, const HostEntity &hostEntity ) const
102  {
103  EntityProxy< HostEntity::codimension, Grid > proxy( grid_, hostEntity );
104  wrappedHandle_.gather( buffer, *proxy );
105  }
106 
107  template< class MessageBuffer, class HostEntity >
108  void scatter ( MessageBuffer &buffer, const HostEntity &hostEntity, size_t size )
109  {
110  EntityProxy< HostEntity::codimension, Grid > proxy( grid_, hostEntity );
111  wrappedHandle_.scatter( buffer, *proxy, size );
112  }
113 
114  private:
115  static void assertHostEntity ( int dim, int codim )
116  {
118  noEntity( codim );
119  }
120 
121  static void noEntity ( int codim )
122  {
123  DUNE_THROW( NotImplemented, "Host grid has no entities for codimension " << codim << "." );
124  }
125 
126  const Grid &grid_;
127  WrappedHandle &wrappedHandle_;
128  };
129 
130  } // namespace GeoGrid
131 
132 } // namespace Dune
133 
134 #endif // #ifndef DUNE_GEOGRID_DATAHANDLE_HH