dune-geometry  2.3.0
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/fvector.hh>
11 #include "type.hh"
12 
13 namespace 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;
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
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:79
A unique label for each type of element that can occur in a grid.
Traits::Object Object
Definition: topologyfactory.hh:40
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:35
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:57
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:100
static const unsigned int dimension
Definition: topologyfactory.hh:38
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:52
static void apply(const unsigned int topologyId)
Definition: topologytypes.hh:321
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:44
Traits::Factory Factory
Definition: topologyfactory.hh:41
unsigned int id() const
Return the topology id the type.
Definition: type.hh:327
const Factory::Object Object
Definition: topologyfactory.hh:83
Factory::Key Key
Definition: topologyfactory.hh:82
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:93
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:24
Traits::Key Key
Definition: topologyfactory.hh:39
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:86
static const unsigned int dimension
Definition: topologyfactory.hh:81