Dune Core Modules (2.4.2)

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 <vector>
7#include <map>
8
11#include <dune/geometry/genericgeometry/topologytypes.hh>
12#include "type.hh"
13
14namespace Dune
15{
16
35 template <class Traits>
37 {
38 // extract types from Traits class
39 static const unsigned int dimension = Traits::dimension;
40 typedef typename Traits::Key Key;
41 typedef typename Traits::Object Object;
42 typedef typename Traits::Factory Factory;
43
45 static Object *create(const Dune::GeometryType &gt, const Key &key)
46 {
47 Object *object;
48 GenericGeometry::IfTopology< Maker, dimension >::apply( gt.id(), key, object );
49 return object;
50 }
52 template <class Topology>
53 static Object *create(const Key &key)
54 {
55 return Factory::template createObject<Topology> ( key );
56 }
58 static void release( Object *object)
59 {
60 delete object;
61 }
62 private:
63 // Internal maker class used in ifTopology helper
64 template< class Topology >
65 struct Maker
66 {
67 static void apply ( const Key &key, Object *&object )
68 {
69 object = create<Topology>( key );
70 };
71 };
72 };
73
74
79 template <class Factory>
81 {
82 static const unsigned int dimension = Factory::dimension;
83 typedef typename Factory::Key Key;
84 typedef const typename Factory::Object Object;
85
87 static Object *create ( const Dune::GeometryType &gt, const Key &key )
88 {
89 assert( gt.id() < numTopologies );
90 return instance().getObject( gt, key );
91 }
93 template< class Topology >
94 static Object *create ( const Key &key )
95 {
96 static_assert((Topology::dimension == dimension),
97 "Topology with incompatible dimension used");
98 return instance().template getObject< Topology >( key );
99 }
101 static void release ( Object *object )
102 {}
103 private:
104 static TopologySingletonFactory &instance ()
105 {
106 static TopologySingletonFactory instance;
107 return instance;
108 }
109
110 static const unsigned int numTopologies = (1 << dimension);
111 typedef std::array< Object *, numTopologies > Array;
112 typedef std::map< Key, Array > Storage;
113
114 TopologySingletonFactory ()
115 {}
116 ~TopologySingletonFactory ()
117 {
118 const typename Storage::iterator end = storage_.end();
119 for( typename Storage::iterator it = storage_.begin(); it != end; ++it )
120 {
121 for( unsigned int topologyId = 0; topologyId < numTopologies; ++topologyId )
122 {
123 Object *&object = it->second[ topologyId ];
124 if( object != 0 )
125 Factory::release( object );
126 object = 0;
127 }
128 }
129 }
130
131 Object *&find( const unsigned int topologyId, const Key &key )
132 {
133 typename Storage::iterator it = storage_.find( key );
134 if( it == storage_.end() )
135 it = storage_.insert( std::make_pair( key, fill_array<Object*, numTopologies>( nullptr ) ) ).first;
136 return it->second[ topologyId ];
137 }
138
139 Object *getObject ( const Dune::GeometryType &gt, const Key &key )
140 {
141 Object *&object = find( gt.id(), key );
142 if( object == 0 )
143 object = Factory::create( gt, key );
144 return object;
145 }
146
147 template< class Topology >
148 Object *getObject ( const Key &key )
149 {
150 Object *&object = find(Topology::id,key);
151 if( object == 0 )
152 object = Factory::template create< Topology >( key );
153 return object;
154 }
155 Storage storage_;
156 };
157
158}
159
160#endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
Fallback implementation of the std::array class (a static array)
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:25
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:132
Dune namespace.
Definition: alignment.hh:10
Fallback implementation of the nullptr object in C++0x.
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:37
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:45
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:53
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:58
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:81
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:87
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:101
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:94
A unique label for each type of element that can occur in a grid.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)