DUNE PDELab (git)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
6#define DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
7
8#include <cassert>
9
10#include <array>
11#include <map>
12#include <memory>
13#include <type_traits>
14#include <vector>
15
16#include <dune/geometry/type.hh>
18
19namespace Dune
20{
21
40 template <class Traits>
42 {
43 // extract types from Traits class
44 static const unsigned int dimension = Traits::dimension;
45 typedef typename Traits::Key Key;
46 typedef typename Traits::Object Object;
47 typedef typename Traits::Factory Factory;
48
50 static Object *create ( const Dune::GeometryType &gt, const Key &key )
51 {
52 return Impl::toGeometryTypeIdConstant<dimension>(gt, [&](auto id) {
53 return create<decltype(id)::value>(key);
54 });
55 }
57 template< GeometryType::Id geometryId >
58 static Object *create ( const Key &key )
59 {
60 return Factory::template createObject< geometryId >( key );
61 }
62
64 template< class Topology >
65 static Object *create ( const Key &key )
66 {
67 return Factory::template createObject< Topology >( key );
68 }
69
71 static void release( Object *object ) { delete object; }
72 };
73
74
75
80 template <class Factory>
82 {
83 static const unsigned int dimension = Factory::dimension;
84 typedef typename Factory::Key Key;
85 typedef const typename Factory::Object Object;
86
88 static Object *create ( const Dune::GeometryType &gt, const Key &key )
89 {
90 assert( gt.id() < numTopologies );
91 return instance().getObject( gt, key );
92 }
94 template< GeometryType::Id geometryId >
95 static auto create ( const Key &key )
96 -> std::enable_if_t< static_cast<GeometryType>(geometryId).dim() == dimension, Object * >
97 {
98 return instance().template getObject< geometryId >( key );
99 }
100
102 template< class Topology >
103 static auto create ( const Key &key )
104 -> std::enable_if_t< Topology::dimension == dimension, Object * >
105 {
106 return instance().template getObject< Topology >( key );
107 }
108
110 static void release ( Object *object )
111 {}
112
113 private:
114 struct ObjectDeleter
115 {
116 void operator() ( Object *ptr ) const { Factory::release( ptr ); }
117 };
118
119 static TopologySingletonFactory &instance ()
120 {
121 static TopologySingletonFactory instance;
122 return instance;
123 }
124
125 static const unsigned int numTopologies = (1 << dimension);
126 typedef std::array< std::unique_ptr< Object, ObjectDeleter >, numTopologies > Array;
127 typedef std::map< Key, Array > Storage;
128
129 TopologySingletonFactory () = default;
130
131 std::unique_ptr< Object, ObjectDeleter > &find ( const unsigned int topologyId, const Key &key )
132 {
133 return storage_[ key ][ topologyId ];
134 }
135
136 Object *getObject ( const Dune::GeometryType &gt, const Key &key )
137 {
138 auto &object = find( gt.id(), key );
139 if( !object )
140 object.reset( Factory::create( gt, key ) );
141 return object.get();
142 }
143
144 template< GeometryType::Id geometryId >
145 Object *getObject ( const Key &key )
146 {
147 static constexpr GeometryType geometry = geometryId;
148 auto &object = find( geometry.id(), key );
149 if( !object )
150 object.reset( Factory::template create< geometry >( key ) );
151 return object.get();
152 }
153
154 template< class Topology >
155 Object *getObject ( const Key &key )
156 {
157 auto &object = find( Topology::id, key );
158 if( !object )
159 object.reset( Factory::template create< Topology >( key ) );
160 return object.get();
161 }
162
163 Storage storage_;
164 };
165
166}
167
168#endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
GeometryType
Type representing VTK's entity geometry types.
Definition: common.hh:132
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
Dune namespace.
Definition: alignedallocator.hh:13
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:42
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:50
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:58
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:71
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:82
static auto create(const Key &key) -> std::enable_if_t< Topology::dimension==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:103
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:88
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:110
static auto create(const Key &key) -> std::enable_if_t< static_cast< GeometryType >(geometryId).dim()==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:95
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 (Nov 24, 23:30, 2024)