dune-fem  2.4.1-rc
referencecounter.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_REFERENCECOUNTER_HH
2 #define DUNE_FEM_REFERENCECOUNTER_HH
3 
4 #include <cassert>
5 
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
32  template< class RCT >
34  : public BartonNackmanInterface< ReferenceCounterInterface< RCT >, typename RCT::ReferenceCounterType >
35  {
38 
39  template< class, class >
40  friend class Conversion;
41 
42  public:
44  typedef RCT Traits;
45 
48 
51 
53  typedef typename Traits::ObjectType ObjectType;
54 
55  protected:
56  using BaseType::asImp;
57 
58  public:
68  void addReference () const
69  {
70  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().addReference() );
71  }
72 
83  void deleteObject ()
84  {
85  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().deleteObject() );
86  }
87 
96  const ObjectType &getObject () const
97  {
98  CHECK_INTERFACE_IMPLEMENTATION( asImp().getObject() );
99  return asImp().getObject();
100  }
101 
110  ObjectType &getObject ()
111  {
112  CHECK_INTERFACE_IMPLEMENTATION( asImp().getObject() );
113  return asImp().getObject();
114  }
115 
125  void removeReference () const
126  {
127  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().removeReference() );
128  }
129  };
130 
131 
132 
133  template< class ReferenceCounter >
135  {
137  static const bool v = Conversion< ReferenceCounter, ReferenceCounterInterfaceType >::exists;
138  };
139 
140 
141 
153  template< class RCT >
155  : public ReferenceCounterInterface< RCT >
156  {
159 
160  template< class, class >
161  friend class Conversion;
162 
163  public:
165  typedef typename BaseType::ReferenceCounterType ReferenceCounterType;
166 
167  protected:
168  using BaseType::asImp;
169 
170  public:
182  explicit ReferenceCounterDefault ( unsigned int refcount = 1 )
183  : refcount_( refcount )
184  {}
185 
186  private:
187  // prohibit copying
188  ReferenceCounterDefault ( const ThisType & );
189  ThisType &operator= ( const ThisType &other );
190 
191  public:
193  void addReference () const
194  {
195  ++refcount_;
196  }
197 
199  void deleteObject ()
200  {
201  delete this;
202  }
203 
205  void removeReference () const
206  {
207  assert( refcount_ > 0 );
208  --refcount_;
209  if( refcount_ == 0 )
210  const_cast< ReferenceCounterType& >( asImp() ).deleteObject();
211  }
212 
214  unsigned int referenceCounter () const { return refcount_; }
215 
216  protected:
217  mutable unsigned int refcount_;
218  };
219 
220 
221 
230  template< class ReferenceCounter >
232  {
234 
235  static_assert( SupportsReferenceCounterInterface< ReferenceCounter >::v, "ObjectPointer can only point to reference counting types." );
236 
237  public:
239  typedef ReferenceCounter ReferenceCounterType;
240 
241  typedef typename ReferenceCounterType::ObjectType ObjectType;
242 
248  explicit ObjectPointer ( ReferenceCounterType *const object = 0 )
249  : object_( object )
250  {
251  if( object_ != 0 )
252  object_->addReference();
253  }
254 
262  ObjectPointer ( const ThisType &other )
263  : object_( other.object_ )
264  {
265  if( object_ != 0 )
266  object_->addReference();
267  }
268 
275  {
276  if( object_ != 0 )
277  object_->removeReference();
278  }
279 
282  ThisType &operator= ( const ThisType &other )
283  {
284  // Note that it is safe to remove the reference first. If other holds
285  // a reference to the same object, the reference counter cannot reach
286  // zero.
287  if( object_ != 0 )
288  object_->removeReference();
289  object_ = other.object_;
290  if( object_ != 0 )
291  object_->addReference();
292 
293  return *this;
294  }
295 
302  ObjectType &operator* () const
303  {
304  assert( object_ != 0 );
305  return object_->getObject();
306  }
307 
309  unsigned int referenceCounter () const { return object_->referenceCounter(); }
310 
311  protected:
312  ReferenceCounterType *object_;
313  };
314 
315  } // namespace Fem
316 
317 } // namespace Dune
318 
319 #endif // #ifndef DUNE_FEM_REFERENCECOUNTER_HH
default implementation of ReferenceCounterInterface
Definition: referencecounter.hh:154
ObjectType & getObject()
access the real object (non-const version)
Definition: referencecounter.hh:110
ReferenceCounterType * object_
Definition: referencecounter.hh:312
void addReference() const
:: ReferenceCounterInterface :: addReference
Definition: referencecounter.hh:193
Traits::ReferenceCounterType ReferenceCounterType
type of the implementation (Barton-Nackman)
Definition: referencecounter.hh:47
friend class Conversion
Definition: referencecounter.hh:40
interface for objects capable of reference counting
Definition: referencecounter.hh:33
ReferenceCounterType::ObjectType ObjectType
Definition: referencecounter.hh:241
Traits::ObjectType ObjectType
type of the object, this is a reference counter for
Definition: referencecounter.hh:53
ReferenceCounterDefault(unsigned int refcount=1)
constructor initializing the reference counter
Definition: referencecounter.hh:182
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
void addReference() const
add a reference to this object
Definition: referencecounter.hh:68
ThisType ReferenceCounterInterfaceType
type of the reference counter interface
Definition: referencecounter.hh:50
ObjectPointer(ReferenceCounterType *const object=0)
initialize a pointer (with a standard C++ pointer)
Definition: referencecounter.hh:248
unsigned int referenceCounter() const
return current reference count
Definition: referencecounter.hh:309
Definition: coordinate.hh:4
RCT Traits
type of the traits
Definition: referencecounter.hh:44
models a pointer to a reference countable object
Definition: referencecounter.hh:231
void deleteObject()
delete to object
Definition: referencecounter.hh:83
unsigned int refcount_
Definition: referencecounter.hh:217
Double operator*(const Double &a, const Double &b)
Definition: double.hh:495
const ObjectType & getObject() const
access the real object (const version)
Definition: referencecounter.hh:96
ReferenceCounter ReferenceCounterType
type of the object, this pointer points to
Definition: referencecounter.hh:235
ReferenceCounterInterface< typename ReferenceCounter::Traits > ReferenceCounterInterfaceType
Definition: referencecounter.hh:136
~ObjectPointer()
destructor
Definition: referencecounter.hh:274
void removeReference() const
:: ReferenceCounterInterface :: removeReference
Definition: referencecounter.hh:205
void removeReference() const
remove a reference to this object
Definition: referencecounter.hh:125
Definition: referencecounter.hh:134
BaseType::ReferenceCounterType ReferenceCounterType
type of the implementation (Barton-Nackman)
Definition: referencecounter.hh:165
void deleteObject()
:: ReferenceCounterInterface :: deleteObject
Definition: referencecounter.hh:199
ObjectFactoryImp::ObjectType ObjectType
Definition: objectstack.hh:25
Definition: objectstack.hh:17
ObjectPointer(const ThisType &other)
copy constructor
Definition: referencecounter.hh:262
unsigned int referenceCounter() const
return current reference count
Definition: referencecounter.hh:214
Definition: bartonnackmaninterface.hh:15