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