dune-geometry 3.0-git
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 <vector>
7#include <map>
8
9#include <dune/common/array.hh>
10
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;
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
A unique label for each type of element that can occur in a grid.
Definition affinegeometry.hh:19
unsigned int numTopologies(int dim)
obtain the number of topologies of a given dimension
Definition topologytypes.hh:134
static void apply(const unsigned int topologyId)
Definition topologytypes.hh:320
Provide a factory over the generic topologies.
Definition topologyfactory.hh:37
Traits::Factory Factory
Definition topologyfactory.hh:42
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition topologyfactory.hh:45
static const unsigned int dimension
Definition topologyfactory.hh:39
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
Traits::Key Key
Definition topologyfactory.hh:40
Traits::Object Object
Definition topologyfactory.hh:41
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)
Definition topologyfactory.hh:87
static void release(Object *object)
release the object returned by the create methods
Definition topologyfactory.hh:101
const Factory::Object Object
Definition topologyfactory.hh:84
Factory::Key Key
Definition topologyfactory.hh:83
static Object * create(const Key &key)
Definition topologyfactory.hh:94
static const unsigned int dimension
Definition topologyfactory.hh:82
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:25
unsigned int id() const
Return the topology id the type.
Definition type.hh:326