dune-geometry  2.8.0
topologyfactory.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_GEOMETRY_TOPOLOGYFACTORY_HH
4 #define DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
5 
6 #include <cassert>
7 
8 #include <array>
9 #include <map>
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
14 #include <dune/geometry/type.hh>
16 
17 namespace Dune
18 {
19 
38  template <class Traits>
40  {
41  // extract types from Traits class
42  static const unsigned int dimension = Traits::dimension;
43  typedef typename Traits::Key Key;
44  typedef typename Traits::Object Object;
45  typedef typename Traits::Factory Factory;
46 
48  static Object *create ( const Dune::GeometryType &gt, const Key &key )
49  {
50  return Impl::toGeometryTypeIdConstant<dimension>(gt, [&](auto id) {
51  return create<decltype(id)::value>(key);
52  });
53  }
55  template< GeometryType::Id geometryId >
56  static Object *create ( const Key &key )
57  {
58  return Factory::template createObject< geometryId >( key );
59  }
60 
62  template< class Topology >
63  static Object *create ( const Key &key )
64  {
65  return Factory::template createObject< Topology >( key );
66  }
67 
69  static void release( Object *object ) { delete object; }
70  };
71 
72 
73 
78  template <class Factory>
80  {
81  static const unsigned int dimension = Factory::dimension;
82  typedef typename Factory::Key Key;
83  typedef const typename Factory::Object Object;
84 
86  static Object *create ( const Dune::GeometryType &gt, const Key &key )
87  {
88  assert( gt.id() < numTopologies );
89  return instance().getObject( gt, key );
90  }
92  template< GeometryType::Id geometryId >
93  static auto create ( const Key &key )
94  -> std::enable_if_t< static_cast<GeometryType>(geometryId).dim() == dimension, Object * >
95  {
96  return instance().template getObject< geometryId >( key );
97  }
98 
100  template< class Topology >
101  static auto create ( const Key &key )
102  -> std::enable_if_t< Topology::dimension == dimension, Object * >
103  {
104  return instance().template getObject< Topology >( key );
105  }
106 
108  static void release ( Object *object )
109  {}
110 
111  private:
112  struct ObjectDeleter
113  {
114  void operator() ( Object *ptr ) const { Factory::release( ptr ); }
115  };
116 
117  static TopologySingletonFactory &instance ()
118  {
119  static TopologySingletonFactory instance;
120  return instance;
121  }
122 
123  static const unsigned int numTopologies = (1 << dimension);
124  typedef std::array< std::unique_ptr< Object, ObjectDeleter >, numTopologies > Array;
125  typedef std::map< Key, Array > Storage;
126 
127  TopologySingletonFactory () = default;
128 
129  std::unique_ptr< Object, ObjectDeleter > &find ( const unsigned int topologyId, const Key &key )
130  {
131  return storage_[ key ][ topologyId ];
132  }
133 
134  Object *getObject ( const Dune::GeometryType &gt, const Key &key )
135  {
136  auto &object = find( gt.id(), key );
137  if( !object )
138  object.reset( Factory::create( gt, key ) );
139  return object.get();
140  }
141 
142  template< GeometryType::Id geometryId >
143  Object *getObject ( const Key &key )
144  {
145  static constexpr GeometryType geometry = geometryId;
146  auto &object = find( geometry.id(), key );
147  if( !object )
148  object.reset( Factory::template create< geometry >( key ) );
149  return object.get();
150  }
151 
152  template< class Topology >
153  Object *getObject ( const Key &key )
154  {
155  auto &object = find( Topology::id, key );
156  if( !object )
157  object.reset( Factory::template create< Topology >( key ) );
158  return object.get();
159  }
160 
161  Storage storage_;
162  };
163 
164 }
165 
166 #endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
A unique label for each type of element that can occur in a grid.
Helper classes to provide indices for geometrytypes for use in a vector.
Definition: affinegeometry.hh:19
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:40
Traits::Factory Factory
Definition: topologyfactory.hh:45
static const unsigned int dimension
Definition: topologyfactory.hh:42
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:69
Traits::Key Key
Definition: topologyfactory.hh:43
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:48
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:56
Traits::Object Object
Definition: topologyfactory.hh:44
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:80
static auto create(const Key &key) -> std::enable_if_t< Topology::dimension==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:101
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:108
const Factory::Object Object
Definition: topologyfactory.hh:83
Factory::Key Key
Definition: topologyfactory.hh:82
static auto create(const Key &key) -> std::enable_if_t< static_cast< GeometryType >(geometryId).dim()==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:93
static const unsigned int dimension
Definition: topologyfactory.hh:81
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:86
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
constexpr unsigned int id() const
Return the topology id of the type.
Definition: type.hh:374