Dune Core Modules (2.3.1)

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
10#include <dune/geometry/genericgeometry/topologytypes.hh>
11#include "type.hh"
12
13namespace Dune
14{
15
34 template <class Traits>
36 {
37 // extract types from Traits class
38 static const unsigned int dimension = Traits::dimension;
39 typedef typename Traits::Key Key;
40 typedef typename Traits::Object Object;
41 typedef typename Traits::Factory Factory;
42
44 static Object *create(const Dune::GeometryType &gt, const Key &key)
45 {
46 Object *object;
47 GenericGeometry::IfTopology< Maker, dimension >::apply( gt.id(), key, object );
48 return object;
49 }
51 template <class Topology>
52 static Object *create(const Key &key)
53 {
54 return Factory::template createObject<Topology> ( key );
55 }
57 static void release( Object *object)
58 {
59 delete object;
60 }
61 private:
62 // Internal maker class used in ifTopology helper
63 template< class Topology >
64 struct Maker
65 {
66 static void apply ( const Key &key, Object *&object )
67 {
68 object = create<Topology>( key );
69 };
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< class Topology >
93 static Object *create ( const Key &key )
94 {
95 dune_static_assert( (Topology::dimension == dimension),
96 "Topology with incompatible dimension used" );
97 return instance().template getObject< Topology >( key );
98 }
100 static void release ( Object *object )
101 {}
102 private:
103 static TopologySingletonFactory &instance ()
104 {
105 static TopologySingletonFactory instance;
106 return instance;
107 }
108
109 static const unsigned int numTopologies = (1 << dimension);
110 typedef FieldVector< Object *, numTopologies > Array;
111 typedef std::map< Key, Array > Storage;
112
113 TopologySingletonFactory ()
114 {}
115 ~TopologySingletonFactory ()
116 {
117 const typename Storage::iterator end = storage_.end();
118 for( typename Storage::iterator it = storage_.begin(); it != end; ++it )
119 {
120 for( unsigned int topologyId = 0; topologyId < numTopologies; ++topologyId )
121 {
122 Object *&object = it->second[ topologyId ];
123 if( object != 0 )
124 Factory::release( object );
125 object = 0;
126 }
127 }
128 }
129
130 Object *&find( const unsigned int topologyId, const Key &key )
131 {
132 typename Storage::iterator it = storage_.find( key );
133 if( it == storage_.end() )
134 it = storage_.insert( std::make_pair( key, Array( 0 ) ) ).first;
135 return it->second[ topologyId ];
136 }
137
138 Object *getObject ( const Dune::GeometryType &gt, const Key &key )
139 {
140 Object *&object = find( gt.id(), key );
141 if( object == 0 )
142 object = Factory::create( gt, key );
143 return object;
144 }
145
146 template< class Topology >
147 Object *getObject ( const Key &key )
148 {
149 Object *&object = find(Topology::id,key);
150 if( object == 0 )
151 object = Factory::template create< Topology >( key );
152 return object;
153 }
154 Storage storage_;
155 };
156
157}
158
159#endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:25
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define dune_static_assert(COND, MSG)
Helper template so that compilation fails if condition is not true.
Definition: static_assert.hh:79
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:14
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:36
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:44
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:52
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:57
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:80
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:100
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:93
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 (Jul 15, 22:36, 2024)