dune-geometry  2.2.0
mappingprovider.hh
Go to the documentation of this file.
1 #ifndef DUNE_GEOMETRY_GENERICGEOMETRY_MAPPINGPROVIDER_HH
2 #define DUNE_GEOMETRY_GENERICGEOMETRY_MAPPINGPROVIDER_HH
3 
4 #include <dune/common/typetraits.hh>
5 
10 
11 namespace Dune
12 {
13 
14  namespace GenericGeometry
15  {
16 
17  // NonHybridMappingFactory
18  // -----------------------
19 
20  template< class Topology, class GeometryTraits >
22  {
24 
25  public:
27 
28  static const unsigned int maxMappingSize = sizeof( Mapping );
29 
30  template< class CoordVector >
31  static Mapping*
32  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
33  {
34  assert( (topologyId >> 1) == (Topology::id >> 1) );
35  return new( mappingStorage ) Mapping( coords );
36  }
37 
38  static std::size_t mappingSize ( const unsigned int topologyId )
39  {
40  return sizeof( Mapping );
41  }
42  };
43 
44 
45 
46  // VirtualMappingFactory
47  // ---------------------
48 
49  template< unsigned int dim, class GeometryTraits >
51  {
53 
54  static const unsigned int numTopologies = (1 << dim);
55 
56  template< int topologyId >
57  struct MappingSize
58  {
60  static const int v = sizeof( VirtualMapping< Topology, GeometryTraits > );
61 
62  static void apply ( std::size_t (&mappingSize)[ numTopologies ] )
63  {
64  mappingSize[ topologyId ] = v;
65  }
66  };
67 
68  template< class CoordVector >
69  class ConstructorTable;
70 
71  struct MappingSizeCache;
72 
73  public:
75 
76  static const unsigned int maxMappingSize = Maximum< MappingSize, 0, numTopologies-1 >::v;
77 
78  template< class CoordVector >
79  static Mapping*
80  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
81  {
82  static ConstructorTable< CoordVector > construct;
83  return construct[ topologyId ]( coords, mappingStorage );
84  }
85 
86  static std::size_t mappingSize ( const unsigned int topologyId )
87  {
88  static MappingSizeCache mappingSize;
89  return mappingSize[ topologyId ];
90  }
91  };
92 
93 
94  // VirtualMappingFactory::ConstructorTable
95  // ---------------------------------------
96 
97  template< unsigned int dim, class GeometryTraits >
98  template< class CoordVector >
99  class VirtualMappingFactory< dim, GeometryTraits >::ConstructorTable
100  {
101  typedef Mapping* (*Construct) ( const CoordVector &coords, char *mappingStorage );
102 
103  template< int i >
104  struct Builder;
105 
106  public:
108  {
109  ForLoop< Builder, 0, numTopologies-1 >::apply( construct_ );
110  }
111 
112  Construct operator[] ( const unsigned int topologyId )
113  {
114  assert( topologyId < numTopologies );
115  return construct_[ topologyId ];
116  }
117 
118  private:
119  template< class Topology >
120  static Mapping*
121  construct ( const CoordVector &coords, char *mappingStorage )
122  {
124  return new( mappingStorage ) VMapping( coords );
125  }
126 
127  Construct construct_[ numTopologies ];
128  };
129 
130 
131  // VirtualMappingFactory::ConstructorTable::Builder
132  // ------------------------------------------------
133 
134  template< unsigned int dim, class GeometryTraits >
135  template< class CoordVector >
136  template< int topologyId >
137  struct VirtualMappingFactory< dim, GeometryTraits >::ConstructorTable< CoordVector >::Builder
138  {
139  static void apply ( Construct (&construct)[ numTopologies ] )
140  {
143  }
144  };
145 
146 
147 
148  // VirtualMappingFactory::MappingSizeCache
149  // ---------------------------------------
150 
151  template< unsigned int dim, class GeometryTraits >
152  struct VirtualMappingFactory< dim, GeometryTraits >::MappingSizeCache
153  {
155  {
156  ForLoop< MappingSize, 0, numTopologies-1 >::apply( size_ );
157  }
158 
159  std::size_t operator[] ( const unsigned int topologyId )
160  {
161  assert( topologyId < numTopologies );
162  return size_[ topologyId ];
163  }
164 
165  private:
166  std::size_t size_[ numTopologies ];
167  };
168 
169 
170 
171  // MappingProvider
172  // ---------------
173 
174  template< class ElementMapping, unsigned int codim >
175  class MappingProvider;
176 
177 
178  template< unsigned int dim, class GeometryTraits, unsigned int codim >
179  class MappingProvider< HybridMapping< dim, GeometryTraits >, codim >
180  {
181  typedef MappingProvider< HybridMapping< dim, GeometryTraits >, codim > This;
182 
183  public:
184  static const unsigned int dimension = dim;
185  static const unsigned int codimension = codim;
186  static const unsigned int mydimension = dimension - codimension;
187 
188  private:
190 
191  public:
192  // Maximal amount of memory required to store a mapping
193  static const unsigned int maxMappingSize = Factory::maxMappingSize;
194 
195  typedef typename Factory::Mapping Mapping;
196 
197  template< class CoordVector >
198  static Mapping*
199  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
200  {
201  return Factory::construct( topologyId, coords, mappingStorage );
202  }
203 
204  template< class CoordVector >
205  static Mapping *create ( const unsigned int topologyId, const CoordVector &coords )
206  {
207  char *mapping = new char[ mappingSize( topologyId ) ];
208  return construct( topologyId, coords, mapping );
209  }
210 
211  static std::size_t mappingSize ( const unsigned int topologyId )
212  {
213  return Factory::mappingSize( topologyId );
214  }
215  };
216 
217 
218  template< class Topology, class GeometryTraits, unsigned int codim >
219  class MappingProvider< NonHybridMapping< Topology, GeometryTraits >, codim >
220  {
221  typedef MappingProvider< NonHybridMapping< Topology, GeometryTraits >, codim > This;
222 
223  public:
224  static const unsigned int dimension = Topology::dimension;
225  static const unsigned int codimension = codim;
226  static const unsigned int mydimension = dimension - codimension;
227 
228  static const bool hybrid = IsCodimHybrid< Topology, codim >::value;
229 
230  private:
231  template< bool >
232  struct HybridFactory
233  : public VirtualMappingFactory< mydimension, GeometryTraits >
234  {};
235 
236  template< bool >
237  struct NonHybridFactory
238  : public NonHybridMappingFactory
239  < typename SubTopology< Topology, codim, 0 >::type, GeometryTraits >
240  {};
241 
242  typedef typename SelectType< hybrid, HybridFactory<true>, NonHybridFactory<false> >::Type Factory;
243 
244  public:
245  // Maximal amount of memory required to store a mapping
246  static const unsigned int maxMappingSize = Factory::maxMappingSize;
247 
248  typedef typename Factory::Mapping Mapping;
249 
250  template< class CoordVector >
251  static Mapping*
252  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
253  {
254  return Factory::construct( topologyId, coords, mappingStorage );
255  }
256 
257  template< class CoordVector >
258  static Mapping *create ( const unsigned int topologyId, const CoordVector &coords )
259  {
260  Mapping *mapping = static_cast< Mapping * >( operator new( mappingSize( topologyId ) ) );
261  construct( topologyId, coords, mapping );
262  return mapping;
263  }
264 
265  static std::size_t mappingSize ( const unsigned int topologyId )
266  {
267  return Factory::mappingSize( topologyId );
268  }
269  };
270 
271  } // namespace GenericGeometry
272 
273 } // namespace Dune
274 
275 #endif // #ifndef DUNE_GEOMETRY_GENERICGEOMETRY_MAPPINGPROVIDER_HH