dune-geometry  2.2.1
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 > 0) ? (numTopologies-1) : 0) >::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  dune_static_assert(dim>=codim, "Codim exceeds dimension");
184  public:
185  static const unsigned int dimension = dim;
186  static const unsigned int codimension = codim;
187  static const unsigned int mydimension = dimension - codimension;
188 
189  private:
191 
192  public:
193  // Maximal amount of memory required to store a mapping
194  static const unsigned int maxMappingSize = Factory::maxMappingSize;
195 
196  typedef typename Factory::Mapping Mapping;
197 
198  template< class CoordVector >
199  static Mapping*
200  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
201  {
202  return Factory::construct( topologyId, coords, mappingStorage );
203  }
204 
205  template< class CoordVector >
206  static Mapping *create ( const unsigned int topologyId, const CoordVector &coords )
207  {
208  char *mapping = new char[ mappingSize( topologyId ) ];
209  return construct( topologyId, coords, mapping );
210  }
211 
212  static std::size_t mappingSize ( const unsigned int topologyId )
213  {
214  return Factory::mappingSize( topologyId );
215  }
216  };
217 
218 
219  template< class Topology, class GeometryTraits, unsigned int codim >
220  class MappingProvider< NonHybridMapping< Topology, GeometryTraits >, codim >
221  {
222  typedef MappingProvider< NonHybridMapping< Topology, GeometryTraits >, codim > This;
223 
224  public:
225  static const unsigned int dimension = Topology::dimension;
226  static const unsigned int codimension = codim;
227  static const unsigned int mydimension = dimension - codimension;
228 
229  static const bool hybrid = IsCodimHybrid< Topology, codim >::value;
230 
231  private:
232  template< bool >
233  struct HybridFactory
234  : public VirtualMappingFactory< mydimension, GeometryTraits >
235  {};
236 
237  template< bool >
238  struct NonHybridFactory
239  : public NonHybridMappingFactory
240  < typename SubTopology< Topology, codim, 0 >::type, GeometryTraits >
241  {};
242 
243  typedef typename SelectType< hybrid, HybridFactory<true>, NonHybridFactory<false> >::Type Factory;
244 
245  public:
246  // Maximal amount of memory required to store a mapping
247  static const unsigned int maxMappingSize = Factory::maxMappingSize;
248 
249  typedef typename Factory::Mapping Mapping;
250 
251  template< class CoordVector >
252  static Mapping*
253  construct ( const unsigned int topologyId, const CoordVector &coords, char *mappingStorage )
254  {
255  return Factory::construct( topologyId, coords, mappingStorage );
256  }
257 
258  template< class CoordVector >
259  static Mapping *create ( const unsigned int topologyId, const CoordVector &coords )
260  {
261  Mapping *mapping = static_cast< Mapping * >( operator new( mappingSize( topologyId ) ) );
262  construct( topologyId, coords, mapping );
263  return mapping;
264  }
265 
266  static std::size_t mappingSize ( const unsigned int topologyId )
267  {
268  return Factory::mappingSize( topologyId );
269  }
270  };
271 
272  } // namespace GenericGeometry
273 
274 } // namespace Dune
275 
276 #endif // #ifndef DUNE_GEOMETRY_GENERICGEOMETRY_MAPPINGPROVIDER_HH