Dune Core Modules (2.8.0)

topologyfactory.hh
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
17namespace 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
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:123
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:130
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:156
Dune namespace.
Definition: alignedallocator.hh:11
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:40
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
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:69
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 Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:86
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:108
static auto create(const Key &key) -> std::enable_if_t< static_cast< GeometryType >(geometryId).dim()==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:93
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.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)