1#ifndef DUNE_FEM_SINGLETON_HH
2#define DUNE_FEM_SINGLETON_HH
10#include <unordered_map>
15#ifdef USING_DUNE_PYTHON
16#include <dune/python/pybind11/pybind11.h>
25 class SingletonStorage
29 struct Item {
virtual ~Item() {} };
31 typedef std::shared_ptr< Item > WeakPointerType;
32 typedef std::unique_ptr< Item > PointerType;
33 typedef std::type_index KeyType;
38 public std::pair< std::unordered_map< KeyType, std::shared_ptr< Item > >, std::vector<PointerType> >
41 std::pair<
std::unordered_map< KeyType,
std::shared_ptr< Item > >,
std::vector<PointerType> > (),
46 typedef std::recursive_mutex mutex_t;
50 typedef Storage StorageType;
54 struct SingletonDeleter
56 void operator()(StorageType* storage)
const
59 std::for_each(storage->second.rbegin(), storage->second.rend(), [](PointerType& item) { item.reset(); });
61 storage->second.clear();
62 storage->first.clear();
66 typedef std::shared_ptr<StorageType> StoragePointer;
68 static StoragePointer storage_;
76#ifndef USING_DUNE_PYTHON
77 storage_.reset(
new StorageType(), SingletonDeleter() );
79 storage_ = pybind11::cast< StoragePointer >(
80 pybind11::module::import(
"dune.fem._fem" ).attr(
"_singleton" ) );
91 template<
class Object >
94 typedef detail::SingletonStorage BaseType;
95 typedef typename BaseType::StorageType StorageType;
96 typedef typename BaseType::Item Item;
97 typedef typename BaseType::PointerType PointerType;
98 typedef typename BaseType::WeakPointerType WeakPointerType;
100 using BaseType::getStorage;
103 struct ItemWrapper :
public Item
106 template <
class... Args>
107 ItemWrapper(Args &&... args) : obj_(std::forward< Args >( args )...)
110 typedef ItemWrapper ItemWrapperType;
115 void operator()(Item *p)
const {}
122 template <
class... Args>
127 static thread_local Object& inst =
getObject(std::forward< Args >( args )...);
133 static const bool placeStaticVariableInline = false ;
137 template <
class... Args>
141 if constexpr ( placeStaticVariableInline )
143 static Object obj( std::forward< Args >( args )...);
151 StorageType& storage = getStorage();
155 std::lock_guard< StorageType::mutex_t > guard( storage.mutex_ );
158 auto& ptr = storage.first[ std::type_index(
typeid(Object)) ];
163 storage.second.emplace_back( PointerType(
new ItemWrapperType(std::forward< Args >( args )...) ) );
166 ptr = WeakPointerType( storage.second.back().operator->(), NullDeleter() );
170 assert(
dynamic_cast< ItemWrapperType*
> (ptr.operator->()) );
171 return static_cast< ItemWrapperType&
> (*ptr).obj_;
return singleton instance of given Object type.
Definition: singleton.hh:93
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
static DUNE_EXPORT Object & getObject(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:138
Dune namespace.
Definition: alignedallocator.hh:13
Definition of macros controlling symbol visibility at the ABI level.
#define DUNE_EXPORT
Export a symbol as part of the public ABI.
Definition: visibility.hh:20