Loading...
Searching...
No Matches
topologyfactory.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
#define DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
#include <cassert>
#include <array>
#include <map>
#include <memory>
#include <type_traits>
#include <vector>
#include <dune/geometry/type.hh>
#include <dune/geometry/typeindex.hh>
namespace Dune
{
template <class Traits>
struct TopologyFactory
{
// extract types from Traits class
static const unsigned int dimension = Traits::dimension;
typedef typename Traits::Key Key;
typedef typename Traits::Object Object;
typedef typename Traits::Factory Factory;
static Object *create ( const Dune::GeometryType &gt, const Key &key )
{
return Impl::toGeometryTypeIdConstant<dimension>(gt, [&](auto id) {
return create<decltype(id)::value>(key);
});
}
template< GeometryType::Id geometryId >
static Object *create ( const Key &key )
{
return Factory::template createObject< geometryId >( key );
}
template< class Topology >
static Object *create ( const Key &key )
{
return Factory::template createObject< Topology >( key );
}
static void release( Object *object ) { delete object; }
};
template <class Factory>
struct TopologySingletonFactory
{
static const unsigned int dimension = Factory::dimension;
typedef typename Factory::Key Key;
typedef const typename Factory::Object Object;
static Object *create ( const Dune::GeometryType &gt, const Key &key )
{
assert( gt.id() < numTopologies );
return instance().getObject( gt, key );
}
template< GeometryType::Id geometryId >
static auto create ( const Key &key )
-> std::enable_if_t< static_cast<GeometryType>(geometryId).dim() == dimension, Object * >
{
return instance().template getObject< geometryId >( key );
}
template< class Topology >
static auto create ( const Key &key )
-> std::enable_if_t< Topology::dimension == dimension, Object * >
{
return instance().template getObject< Topology >( key );
}
static void release ( Object *object )
{}
private:
struct ObjectDeleter
{
void operator() ( Object *ptr ) const { Factory::release( ptr ); }
};
static TopologySingletonFactory &instance ()
{
static TopologySingletonFactory instance;
return instance;
}
static const unsigned int numTopologies = (1 << dimension);
typedef std::array< std::unique_ptr< Object, ObjectDeleter >, numTopologies > Array;
typedef std::map< Key, Array > Storage;
TopologySingletonFactory () = default;
std::unique_ptr< Object, ObjectDeleter > &find ( const unsigned int topologyId, const Key &key )
{
return storage_[ key ][ topologyId ];
}
Object *getObject ( const Dune::GeometryType &gt, const Key &key )
{
auto &object = find( gt.id(), key );
if( !object )
object.reset( Factory::create( gt, key ) );
return object.get();
}
template< GeometryType::Id geometryId >
Object *getObject ( const Key &key )
{
static constexpr GeometryType geometry = geometryId;
auto &object = find( geometry.id(), key );
if( !object )
object.reset( Factory::template create< geometry >( key ) );
return object.get();
}
template< class Topology >
Object *getObject ( const Key &key )
{
auto &object = find( Topology::id, key );
if( !object )
object.reset( Factory::template create< Topology >( key ) );
return object.get();
}
Storage storage_;
};
}
#endif // #ifndef DUNE_GEOMETRY_TOPOLOGYFACTORY_HH
typeindex.hh
Helper classes to provide indices for geometrytypes for use in a vector.
type.hh
A unique label for each type of element that can occur in a grid.
Dune
Definition affinegeometry.hh:22
Dune::TopologyFactory
Provide a factory over the generic topologies.
Definition topologyfactory.hh:42
Dune::TopologyFactory::Factory
Traits::Factory Factory
Definition topologyfactory.hh:47
Dune::TopologyFactory::create
static Object * create(const Dune::GeometryType &gt, const Key &key)
dynamically create objects
Definition topologyfactory.hh:50
Dune::TopologyFactory::dimension
static const unsigned int dimension
Definition topologyfactory.hh:44
Dune::TopologyFactory::create
static Object * create(const Key &key)
statically create objects
Definition topologyfactory.hh:58
Dune::TopologyFactory::release
static void release(Object *object)
release the object returned by the create methods
Definition topologyfactory.hh:71
Dune::TopologyFactory::Key
Traits::Key Key
Definition topologyfactory.hh:45
Dune::TopologyFactory::Object
Traits::Object Object
Definition topologyfactory.hh:46
Dune::TopologySingletonFactory::create
static auto create(const Key &key) -> std::enable_if_t< Topology::dimension==dimension, Object * >
Definition topologyfactory.hh:103
Dune::TopologySingletonFactory::create
static Object * create(const Dune::GeometryType &gt, const Key &key)
Definition topologyfactory.hh:88
Dune::TopologySingletonFactory::release
static void release(Object *object)
release the object returned by the create methods
Definition topologyfactory.hh:110
Dune::TopologySingletonFactory::Object
const Factory::Object Object
Definition topologyfactory.hh:85
Dune::TopologySingletonFactory::Key
Factory::Key Key
Definition topologyfactory.hh:84
Dune::TopologySingletonFactory::create
static auto create(const Key &key) -> std::enable_if_t< static_cast< GeometryType >(geometryId).dim()==dimension, Object * >
Definition topologyfactory.hh:95
Dune::TopologySingletonFactory::dimension
static const unsigned int dimension
Definition topologyfactory.hh:83
Dune::GeometryType
Unique label for each type of entities that can occur in DUNE grids.
Definition type.hh:114
Dune::GeometryType::id
constexpr unsigned int id() const
Return the topology id of the type.
Definition type.hh:365