Dune Core Modules (2.6.0)

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 <cassert>
7 
8 #include <array>
9 #include <map>
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
14 #include <dune/geometry/type.hh>
15 
16 namespace Dune
17 {
18 
37  template <class Traits>
39  {
40  // extract types from Traits class
41  static const unsigned int dimension = Traits::dimension;
42  typedef typename Traits::Key Key;
43  typedef typename Traits::Object Object;
44  typedef typename Traits::Factory Factory;
45 
47  static Object *create ( const Dune::GeometryType &gt, const Key &key )
48  {
50  }
51 
53  template< class Topology >
54  static Object *create ( const Key &key )
55  {
56  return Factory::template createObject< Topology >( key );
57  }
58 
60  static void release( Object *object ) { delete object; }
61 
62  private:
63  // Internal maker class used in ifTopology helper
64  template< class Topology >
65  struct Maker
66  {
67  static Object *apply ( const Key &key )
68  {
69  return create< Topology >( key );
70  };
71  };
72  };
73 
74 
75 
80  template <class Factory>
82  {
83  static const unsigned int dimension = Factory::dimension;
84  typedef typename Factory::Key Key;
85  typedef const typename Factory::Object Object;
86 
88  static Object *create ( const Dune::GeometryType &gt, const Key &key )
89  {
90  assert( gt.id() < numTopologies );
91  return instance().getObject( gt, key );
92  }
93 
95  template< class Topology >
96  static auto create ( const Key &key )
97  -> std::enable_if_t< Topology::dimension == dimension, Object * >
98  {
99  return instance().template getObject< Topology >( key );
100  }
101 
103  static void release ( Object *object )
104  {}
105 
106  private:
107  struct ObjectDeleter
108  {
109  void operator() ( Object *ptr ) const { Factory::release( ptr ); }
110  };
111 
112  static TopologySingletonFactory &instance ()
113  {
114  static TopologySingletonFactory instance;
115  return instance;
116  }
117 
118  static const unsigned int numTopologies = (1 << dimension);
119  typedef std::array< std::unique_ptr< Object, ObjectDeleter >, numTopologies > Array;
120  typedef std::map< Key, Array > Storage;
121 
122  TopologySingletonFactory () = default;
123 
124  std::unique_ptr< Object, ObjectDeleter > &find ( const unsigned int topologyId, const Key &key )
125  {
126  return storage_[ key ][ topologyId ];
127  }
128 
129  Object *getObject ( const Dune::GeometryType &gt, const Key &key )
130  {
131  auto &object = find( gt.id(), key );
132  if( !object )
133  object.reset( Factory::create( gt, key ) );
134  return object.get();
135  }
136 
137  template< class Topology >
138  Object *getObject ( const Key &key )
139  {
140  auto &object = find( Topology::id, key );
141  if( !object )
142  object.reset( Factory::template create< Topology >( key ) );
143  return object.get();
144  }
145 
146  Storage storage_;
147  };
148 
149 }
150 
151 #endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:277
decltype(auto) apply(F &&f, ArgTuple &&args)
Apply function with arguments given as tuple.
Definition: apply.hh:58
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:147
Dune namespace.
Definition: alignedallocator.hh:10
Provide a factory over the generic topologies.
Definition: topologyfactory.hh:39
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:60
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:47
static Object * create(const Key &key)
statically create objects
Definition: topologyfactory.hh:54
A wrapper for a TopologyFactory providing singleton storage. Same usage as TopologyFactory but with e...
Definition: topologyfactory.hh:82
static auto create(const Key &key) -> std::enable_if_t< Topology::dimension==dimension, Object * >
statically create objects
Definition: topologyfactory.hh:96
static void release(Object *object)
release the object returned by the create methods
Definition: topologyfactory.hh:103
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition: topologyfactory.hh:88
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.80.0 (May 3, 22:32, 2024)