dune-geometry  2.5.1
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 
11 #include <dune/geometry/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  {
47  }
48 
50  template< class Topology >
51  static Object *create ( const Key &key )
52  {
53  return Factory::template createObject< Topology >( key );
54  }
55 
57  static void release( Object *object ) { delete object; }
58 
59  private:
60  // Internal maker class used in ifTopology helper
61  template< class Topology >
62  struct Maker
63  {
64  static Object *apply ( const Key &key )
65  {
66  return create< Topology >( key );
67  };
68  };
69  };
70 
71 
72 
77  template <class Factory>
79  {
80  static const unsigned int dimension = Factory::dimension;
81  typedef typename Factory::Key Key;
82  typedef const typename Factory::Object Object;
83 
85  static Object *create ( const Dune::GeometryType &gt, const Key &key )
86  {
87  assert( gt.id() < numTopologies );
88  return instance().getObject( gt, key );
89  }
91  template< class Topology >
92  static Object *create ( const Key &key )
93  {
94  static_assert((Topology::dimension == dimension),
95  "Topology with incompatible dimension used");
96  return instance().template getObject< Topology >( key );
97  }
99  static void release ( Object *object )
100  {}
101  private:
102  static TopologySingletonFactory &instance ()
103  {
104  static TopologySingletonFactory instance;
105  return instance;
106  }
107 
108  static const unsigned int numTopologies = (1 << dimension);
109  typedef std::array< Object *, numTopologies > Array;
110  typedef std::map< Key, Array > Storage;
111 
113  {}
115  {
116  const typename Storage::iterator end = storage_.end();
117  for( typename Storage::iterator it = storage_.begin(); it != end; ++it )
118  {
119  for( unsigned int topologyId = 0; topologyId < numTopologies; ++topologyId )
120  {
121  Object *&object = it->second[ topologyId ];
122  if( object != 0 )
123  Factory::release( object );
124  object = 0;
125  }
126  }
127  }
128 
129  Object *&find( const unsigned int topologyId, const Key &key )
130  {
131  typename Storage::iterator it = storage_.find( key );
132  if( it == storage_.end() )
133  it = storage_.insert( std::make_pair( key, fill_array<Object*, numTopologies>( nullptr ) ) ).first;
134  return it->second[ topologyId ];
135  }
136 
137  Object *getObject ( const Dune::GeometryType &gt, const Key &key )
138  {
139  Object *&object = find( gt.id(), key );
140  if( object == 0 )
141  object = Factory::create( gt, key );
142  return object;
143  }
144 
145  template< class Topology >
146  Object *getObject ( const Key &key )
147  {
148  Object *&object = find(Topology::id,key);
149  if( object == 0 )
150  object = Factory::template create< Topology >( key );
151  return object;
152  }
153  Storage storage_;
154  };
155 
156 }
157 
158 #endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:267
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:44
static auto apply(unsigned int topologyId, Args &&... args)
Definition: type.hh:233
const Factory::Object Object
Definition: topologyfactory.hh:82
Definition: affinegeometry.hh:18
static unsigned int numTopologies(int dim) noexcept
obtain the number of topologies of a given dimension
Definition: type.hh:93
A unique label for each type of element that can occur in a grid.
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:92
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:99
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:35
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:51
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:78
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:85
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:57
Traits::Factory Factory
Definition: topologyfactory.hh:41
static const unsigned int dimension
Definition: topologyfactory.hh:38
Factory::Key Key
Definition: topologyfactory.hh:81
unsigned int id() const
Return the topology id the type.
Definition: type.hh:570
Traits::Key Key
Definition: topologyfactory.hh:39
Traits::Object Object
Definition: topologyfactory.hh:40