dune-fem  2.4.1-rc
objectstack.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_OBJECTSTACK_HH
2 #define DUNE_FEM_OBJECTSTACK_HH
3 
6 
7 namespace Dune
8 {
9 
10  namespace Fem
11  {
12 
13  template< class ObjectFactoryImp >
14  class ObjectStack;
15 
16  template< class ObjectFactoryImp >
18 
19 
20  template< class ObjectFactoryImp >
22  {
24 
25  typedef typename ObjectFactoryImp :: ObjectType ObjectType;
26  };
27 
28 
29  template< class ObjectFactoryImp >
30  class ObjectStackEntry
31  : public ReferenceCounterDefault< ObjectStackEntryTraits< ObjectFactoryImp > >
32  {
33  public:
34  typedef ObjectFactoryImp ObjectFactoryType;
35 
37 
38  private:
41 
42  template< class, class >
43  friend class Conversion;
44 
45  friend class ObjectStack< ObjectFactoryType >;
46 
47  protected:
49 
50  public:
51  typedef typename ObjectFactoryType :: ObjectType ObjectType;
52 
53  protected:
54  // reference to the stack
55  ObjectStackType &stack_;
56 
57  // pointer to the actual object
58  ObjectType *const object_;
59 
60  // next object on the stack
61  ThisType *next_;
62 
63  protected:
64  inline explicit ObjectStackEntry ( ObjectStackType &stack )
65  : BaseType( 0 ),
66  stack_( stack ),
67  object_( stack_.factory().newObject() )
68  {
69  }
70 
71  private:
72  // prohibit copying
73  ObjectStackEntry ( const ThisType & );
74 
75  public:
77  {
78  delete object_;
79  }
80 
81  private:
82  // prohivit assignment
83  ThisType &operator= ( const ThisType & );
84 
85  public:
86  inline operator const ObjectType& () const
87  {
88  return *object_;
89  }
90 
91  inline operator ObjectType& ()
92  {
93  return *object_;
94  }
95 
96  inline void deleteObject ()
97  {
98  stack_.push( this );
99  }
100 
101  inline const ObjectType &getObject () const
102  {
103  return *object_;
104  }
105 
106  inline ObjectType &getObject ()
107  {
108  return *object_;
109  }
110  };
111 
112 
113 
117  template< class ObjectFactoryImp >
118  class ObjectStack
119  {
120  typedef ObjectFactoryImp ObjectFactoryType;
121 
122  private:
124 
125  friend class ObjectStackEntry< ObjectFactoryType >;
126 
127  public:
129  typedef typename ObjectFactoryType :: ObjectType ObjectType;
130 
133 
136 
137  protected:
138  const ObjectFactoryType &factory_;
139 
140  // store stack such that entries are thread safe
141  typedef StackEntryType* StackEntryPtrType;
143 
144  // thread safe stack entries (in multi thread mode a vector)
145  ThreadSafeValuesType stackEntries_;
146  public:
148  ObjectStack ( const ObjectFactoryType &factory )
149  : factory_( factory ),
150  stackEntries_( StackEntryPtrType(0) )
151  {
152  }
153 
154  private:
155  // Disallow copying
156  ObjectStack ( const ThisType & );
157 
158  public:
161  {
162  // make sure this is only called in single thread mode
163  // because the master thread is taking care of all object delete
165  const size_t threadSize = stackEntries_.size();
166  for( size_t i=0; i<threadSize; ++i )
167  {
168  StackEntryPtrType& stackEntry = stackEntries_[ i ];
169  while ( stackEntry != 0 )
170  {
171  StackEntryType *obj = stackEntry;
172  stackEntry = obj->next_;
173  delete obj;
174  }
175  }
176  }
177 
178  private:
179  // Disallow copying
180  ThisType &operator= ( const ThisType & );
181 
182  public:
184  inline PointerType getObject ()
185  {
186  return PointerType( pop() );
187  }
188 
189  protected:
190  inline const ObjectFactoryType &factory() const
191  {
192  return factory_;
193  }
194 
195  // push storage object to the stack
196  inline void push ( StackEntryType *obj )
197  {
198  // get thread private value
199  push( obj, *stackEntries_ );
200  }
201 
202  // pop a storage object from the stack
203  inline StackEntryType *pop ()
204  {
205  // get thread private value
206  return pop( *stackEntries_ );
207  }
208 
209  private:
210  // push storage object to the stack
211  inline void push ( StackEntryType *obj, StackEntryPtrType& stackEntry )
212  {
213  obj->next_ = stackEntry;
214  stackEntry = obj;
215  }
216 
217  // pop a storage object from the stack
218  inline StackEntryType *pop ( StackEntryPtrType& stackEntry )
219  {
220  StackEntryType *ptr = stackEntry;
221  if( ptr != 0 )
222  stackEntry = stackEntry->next_;
223  else
224  ptr = new StackEntryType( *this );
225  return ptr;
226  }
227  };
228 
229  } // namespace Fem
230 
231 } // namespace Dune
232 
233 #endif // #ifndef DUNE_FEM_OBJECTSTACK_HH
default implementation of ReferenceCounterInterface
Definition: referencecounter.hh:154
ObjectStackEntry< ObjectFactoryImp > ReferenceCounterType
Definition: objectstack.hh:23
ObjectFactoryType::ObjectType ObjectType
type of the stored objects
Definition: objectstack.hh:129
void deleteObject()
Definition: objectstack.hh:96
ObjectType *const object_
Definition: objectstack.hh:58
ObjectStackEntryTraits< ObjectFactoryType > Traits
Definition: objectstack.hh:36
ObjectStackEntry(ObjectStackType &stack)
Definition: objectstack.hh:64
ObjectStackType & stack_
Definition: objectstack.hh:55
ThisType * next_
Definition: objectstack.hh:61
ObjectFactoryType::ObjectType ObjectType
Definition: objectstack.hh:51
Definition: objectstack.hh:14
ObjectStack(const ObjectFactoryType &factory)
constructor
Definition: objectstack.hh:148
ObjectStackEntry< ObjectFactoryType > StackEntryType
type of the storage objects
Definition: objectstack.hh:132
Definition: coordinate.hh:4
models a pointer to a reference countable object
Definition: referencecounter.hh:231
const ObjectFactoryType & factory_
Definition: objectstack.hh:138
ObjectPointer< StackEntryType > PointerType
type of object pointers
Definition: objectstack.hh:135
~ObjectStack()
delete all objects on stack
Definition: objectstack.hh:160
StackEntryType * StackEntryPtrType
Definition: objectstack.hh:141
void push(StackEntryType *obj)
Definition: objectstack.hh:196
Definition: objectstack.hh:21
size_t size() const
return number of threads
Definition: threadsafevalue.hh:47
ThreadSafeValuesType stackEntries_
Definition: objectstack.hh:145
~ObjectStackEntry()
Definition: objectstack.hh:76
const ObjectType & getObject() const
Definition: objectstack.hh:101
StackEntryType * pop()
Definition: objectstack.hh:203
ObjectType & getObject()
Definition: objectstack.hh:106
PointerType getObject()
get an object pointer to a storage object
Definition: objectstack.hh:184
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:217
ObjectStack< ObjectFactoryType > ObjectStackType
Definition: objectstack.hh:48
const ObjectFactoryType & factory() const
Definition: objectstack.hh:190
ObjectFactoryImp::ObjectType ObjectType
Definition: objectstack.hh:25
ThreadSafeValue< StackEntryPtrType > ThreadSafeValuesType
Definition: objectstack.hh:142
ObjectFactoryImp ObjectFactoryType
Definition: objectstack.hh:34
Definition: objectstack.hh:17