dune-fem  2.4.1-rc
singletonlist.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SINGLETONLIST_HH
2 #define DUNE_FEM_SINGLETONLIST_HH
3 
4 //- System includes
5 #include <cassert>
6 #include <vector>
7 #include <string>
8 #include <list>
9 #include <iostream>
10 
11 //- dune-fem includes
13 
14 namespace Dune
15 {
16 
17  namespace Fem
18  {
19 
20  template< class Key, class Object >
22  {
23  static Object *createObject ( const Key &key )
24  {
25  return new Object( key );
26  }
27 
28  static void deleteObject ( Object *object )
29  {
30  delete object;
31  }
32  };
33 
34 
47  template< class Key, class Object,
50  {
52 
53  public:
54  typedef Key KeyType;
55  typedef Object ObjectType;
56  typedef Factory FactoryType;
57 
58  typedef std :: pair< ObjectType * , unsigned int * > ValueType;
59  typedef std :: pair< KeyType, ValueType > ListObjType;
60 
61  private:
62  typedef std :: list< ListObjType > ListType;
63  typedef typename ListType :: iterator ListIteratorType;
64 
66 
67  private:
68  // prohibit creation
69  SingletonList ();
70 
71  // prohibit copying
72  SingletonList ( const ThisType & );
73 
74  public:
77  inline static ListType & singletonList()
78  {
79  static SingletonListStorage s;
81  return s.singletonList();
82  }
83 
84  public:
88  //inline static ObjectType & getObject(KeyType key)
89  inline static ObjectType &getObject( const KeyType &key )
90  {
91  // make sure this method is only called in single thread mode
93 
94  ValueType objValue = getObjFromList( key );
95 
96  // if object exists, increase reference count and return it
97  if( objValue.first )
98  {
99  ++( *(objValue.second) );
100  return *(objValue.first);
101  }
102 
103  // object does not exist. Create it with reference count of 1
104  ObjectType *object = FactoryType :: createObject( key );
105  assert( object );
106  ValueType value( object, new unsigned int( 1 ) );
107  ListObjType tmp( key, value );
108  singletonList().push_back( tmp );
109  return *object;
110  }
111 
114  inline static void removeObject ( const ObjectType &object )
115  {
116  // make sure this method is only called in single thread mode
118 
119  ListIteratorType end = singletonList().end();
120  for( ListIteratorType it = singletonList().begin(); it != end; ++it )
121  {
122  if( (*it).second.first == &object )
123  {
124  eraseItem( it );
125  return;
126  }
127  }
128 
129  std :: cerr << "Object could not be deleted, "
130  << "because it is not in the list anymore!" << std :: endl;
131  }
132 
133  // return pair < Object * , refCounter *>
134  inline static ValueType getObjFromList( const KeyType &key )
135  {
136  ListIteratorType endit = singletonList().end();
137  for(ListIteratorType it = singletonList().begin(); it!=endit; ++it)
138  {
139  if( (*it).first == key )
140  {
141  return (*it).second;
142  }
143  }
144  return ValueType( (ObjectType *)0, (unsigned int *)0 );
145  }
146 
147  protected:
148  static void eraseItem( ListIteratorType &it )
149  {
150  ValueType value = (*it).second;
151  unsigned int &refCount = *(value.second);
152 
153  assert( refCount > 0 );
154  if( (--refCount) == 0 )
155  deleteItem( it );
156  }
157 
158  private:
159  static void deleteItem(ListIteratorType & it)
160  {
161  ValueType val = (*it).second;
162  // remove from list
163  singletonList().erase( it );
164  // delete objects
165  FactoryType :: deleteObject( val.first );
166  delete val.second;
167  }
168  }; // end SingletonList
169 
170 
171  template< class Key, class Object, class Factory >
172  class SingletonList< Key, Object, Factory > :: SingletonListStorage
173  {
175 
176  protected:
177  ListType singletonList_;
178 
179  public:
181  : singletonList_()
182  {}
183 
185  {
186  while( !singletonList().empty() )
187  deleteItem( singletonList().begin() );
188  }
189 
190  ListType &singletonList ()
191  {
192  return singletonList_;
193  }
194 
195  void deleteItem ( const ListIteratorType &it )
196  {
197  ValueType val = (*it).second;
198  // remove from list
199  singletonList().erase( it );
200  // delete objects
201  FactoryType :: deleteObject( val.first );
202  delete val.second;
203  }
204  };
205 
206  } // namespace Fem
207 
208 } // namespace Dune
209 
210 #endif // #ifndef DUNE_FEM_SINGLETONLIST_HH
std::pair< KeyType, ValueType > ListObjType
Definition: singletonlist.hh:59
Key KeyType
Definition: singletonlist.hh:54
std::pair< ObjectType *, unsigned int * > ValueType
Definition: singletonlist.hh:58
Factory FactoryType
Definition: singletonlist.hh:56
Singleton list for key/object pairs.
Definition: singletonlist.hh:49
void deleteItem(const ListIteratorType &it)
Definition: singletonlist.hh:195
static Object * createObject(const Key &key)
Definition: singletonlist.hh:23
static ListType & singletonList()
Definition: singletonlist.hh:77
Definition: coordinate.hh:4
static void deleteObject(Object *object)
Definition: singletonlist.hh:28
static void eraseItem(ListIteratorType &it)
Definition: singletonlist.hh:148
static void removeObject(const ObjectType &object)
Definition: singletonlist.hh:114
static ObjectType & getObject(const KeyType &key)
Definition: singletonlist.hh:89
SingletonListStorage()
Definition: singletonlist.hh:180
ListType & singletonList()
Definition: singletonlist.hh:190
static ValueType getObjFromList(const KeyType &key)
Definition: singletonlist.hh:134
Definition: singletonlist.hh:172
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:217
~SingletonListStorage()
Definition: singletonlist.hh:184
Definition: singletonlist.hh:21
Object ObjectType
Definition: singletonlist.hh:55
ListType singletonList_
Definition: singletonlist.hh:177