1#ifndef DUNE_FEM_SINGLETONLIST_HH
2#define DUNE_FEM_SINGLETONLIST_HH
14#include <dune/fem/misc/mpimanager.hh>
15#include <dune/fem/storage/singleton.hh>
23 template<
class Key,
class Object >
24 struct DefaultSingletonFactory
26 static Object *createObject (
const Key &key )
28 return new Object( key );
31 static void deleteObject ( Object *
object )
50 template<
class Key,
class Object,
51 class Factory = DefaultSingletonFactory< Key, Object > >
58 typedef Object ObjectType;
59 typedef Factory FactoryType;
61 typedef std :: pair< ObjectType * , unsigned int * > ValueType;
62 typedef std :: pair< KeyType, ValueType > ListObjType;
70 typedef std :: list< ListObjType > ListType;
71 typedef typename ListType :: iterator ListIteratorType;
73 class SingletonListStorage;
87 return s.singletonList();
93 template<
class... Args >
94 static auto getObject(
const KeyType &key, Args &&... args )
95 -> std::enable_if_t< std::is_same< decltype( FactoryType::createObject( key, std::forward< Args >( args )... ) ), ObjectType * >::value, ObjectType & >
97 ValueType objValue = getObjFromList( key );
102 ++( *(objValue.second) );
103 return *(objValue.first);
107 if( ! Fem :: MPIManager :: singleThreadMode() )
113 ObjectType *
object = FactoryType::createObject( key, std::forward< Args >( args )... );
115 ValueType value(
object,
new unsigned int( 1 ) );
116 ListObjType tmp( key, value );
126 if( ! Fem :: MPIManager :: singleThreadMode() )
132 for( ListIteratorType it =
singletonList().begin(); it != end; ++it )
134 if( (*it).second.first == &
object )
141 std :: cerr <<
"Object could not be deleted, "
142 <<
"because it is not in the list anymore!" << std :: endl;
146 inline static ValueType getObjFromList(
const KeyType &key )
149 for(ListIteratorType it =
singletonList().begin(); it!=endit; ++it)
151 if( (*it).first == key )
156 return ValueType( (ObjectType *)0, (
unsigned int *)0 );
160 static void eraseItem( ListIteratorType &it )
162 ValueType value = (*it).second;
163 unsigned int &refCount = *(value.second);
165 assert( refCount > 0 );
166 if( (--refCount) == 0 )
171 static void deleteItem(ListIteratorType & it)
173 ValueType val = (*it).second;
177 FactoryType :: deleteObject( val.first );
183 template<
class Key,
class Object,
class Factory >
184 class SingletonList< Key, Object, Factory > :: SingletonListStorage
186 typedef SingletonListStorage ThisType;
189 ListType singletonList_;
192 inline SingletonListStorage ()
196 inline ~SingletonListStorage ()
204 return singletonList_;
207 void deleteItem (
const ListIteratorType &it )
209 ValueType val = (*it).second;
213 FactoryType :: deleteObject( val.first );
Exception thrown when a code segment that is supposed to be only accessed in single thread mode is ac...
Definition: mpimanager.hh:43
Singleton list for key/object pairs.
Definition: singletonlist.hh:53
static void removeObject(const ObjectType &object)
Definition: singletonlist.hh:123
static ListType & singletonList()
Definition: singletonlist.hh:81
static auto getObject(const KeyType &key, Args &&... args) -> std::enable_if_t< std::is_same< decltype(FactoryType::createObject(key, std::forward< Args >(args)...)), ObjectType * >::value, ObjectType & >
Definition: singletonlist.hh:94
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::bool_constant<(sizeof...(II)==0)> empty(std::integer_sequence< T, II... >)
Checks whether the sequence is empty.
Definition: integersequence.hh:80