dune-fem 2.12-git
Loading...
Searching...
No Matches
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#include <type_traits>
6
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
33 template< class RCT >
35 : public BartonNackmanInterface< ReferenceCounterInterface< RCT >, typename RCT::ReferenceCounterType >
36 {
39
40 public:
42 typedef RCT Traits;
43
45 typedef typename Traits::ReferenceCounterType ReferenceCounterType;
46
49
51 typedef typename Traits::ObjectType ObjectType;
52
53 protected:
54 using BaseType::asImp;
55
56 public:
70
85
94 const ObjectType &getObject () const
95 {
97 return asImp().getObject();
98 }
99
109 {
111 return asImp().getObject();
112 }
113
127 };
128
129
130
131 template< class ReferenceCounter >
137
138
139
151 template< class RCT >
153 : public ReferenceCounterInterface< RCT >
154 {
157
158 public:
161
162 protected:
163 using BaseType::asImp;
164
165 public:
177 explicit ReferenceCounterDefault ( unsigned int refcount = 1 )
178 : refcount_( refcount )
179 {}
180
182 ThisType& operator= ( const ThisType& ) = delete;
183
185 void addReference () const
186 {
187 ++refcount_;
188 }
189
192 {
193 delete this;
194 }
195
197 void removeReference () const
198 {
199 assert( refcount_ > 0 );
200 --refcount_;
201 if( refcount_ == 0 )
202 const_cast< ReferenceCounterType& >( asImp() ).deleteObject();
203 }
204
206 unsigned int referenceCounter () const { return refcount_; }
207
208 protected:
209 mutable unsigned int refcount_;
210 };
211
212
213
222 template< class ReferenceCounter >
224 {
226
227 static_assert( SupportsReferenceCounterInterface< ReferenceCounter >::v, "ObjectPointer can only point to reference counting types." );
228
229 public:
231 typedef ReferenceCounter ReferenceCounterType;
232
233 typedef typename ReferenceCounterType::ObjectType ObjectType;
234
240 explicit ObjectPointer ( ReferenceCounterType *const object = 0 )
241 : object_( object )
242 {
243 if( object_ != 0 )
244 object_->addReference();
245 }
246
254 ObjectPointer ( const ThisType &other )
255 : object_( other.object_ )
256 {
257 if( object_ != 0 )
258 object_->addReference();
259 }
260
267 {
268 if( object_ != 0 )
269 object_->removeReference();
270 }
271
274 ThisType &operator= ( const ThisType &other )
275 {
276 // Note that it is safe to remove the reference first. If other holds
277 // a reference to the same object, the reference counter cannot reach
278 // zero.
279 if( object_ != 0 )
280 object_->removeReference();
281 object_ = other.object_;
282 if( object_ != 0 )
283 object_->addReference();
284
285 return *this;
286 }
287
295 {
296 assert( object_ != 0 );
297 return object_->getObject();
298 }
299
301 unsigned int referenceCounter () const { return object_->referenceCounter(); }
302
303 protected:
305 };
306
307 } // namespace Fem
308
309} // namespace Dune
310
311#endif // #ifndef DUNE_FEM_REFERENCECOUNTER_HH
#define CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(__interface_method_to_call__)
#define CHECK_INTERFACE_IMPLEMENTATION(dummy)
Definition bartonnackmaninterface.hh:17
const Implementation & asImp() const
Definition bartonnackmaninterface.hh:37
interface for objects capable of reference counting
Definition referencecounter.hh:36
Traits::ObjectType ObjectType
type of the object, this is a reference counter for
Definition referencecounter.hh:51
void deleteObject()
delete to object
Definition referencecounter.hh:81
void removeReference() const
remove a reference to this object
Definition referencecounter.hh:123
RCT Traits
type of the traits
Definition referencecounter.hh:42
ObjectType & getObject()
access the real object (non-const version)
Definition referencecounter.hh:108
const Implementation & asImp() const
Definition bartonnackmaninterface.hh:37
void addReference() const
add a reference to this object
Definition referencecounter.hh:66
Traits::ReferenceCounterType ReferenceCounterType
type of the implementation (Barton-Nackman)
Definition referencecounter.hh:45
ThisType ReferenceCounterInterfaceType
type of the reference counter interface
Definition referencecounter.hh:48
const ObjectType & getObject() const
access the real object (const version)
Definition referencecounter.hh:94
Definition referencecounter.hh:133
ReferenceCounterInterface< typename ReferenceCounter::Traits > ReferenceCounterInterfaceType
Definition referencecounter.hh:134
static const bool v
Definition referencecounter.hh:135
default implementation of ReferenceCounterInterface
Definition referencecounter.hh:154
ReferenceCounterDefault(const ThisType &)=delete
ThisType & operator=(const ThisType &)=delete
unsigned int referenceCounter() const
return current reference count
Definition referencecounter.hh:206
BaseType::ReferenceCounterType ReferenceCounterType
type of the implementation (Barton-Nackman)
Definition referencecounter.hh:160
const Implementation & asImp() const
Definition bartonnackmaninterface.hh:37
unsigned int refcount_
Definition referencecounter.hh:209
ReferenceCounterDefault(unsigned int refcount=1)
constructor initializing the reference counter
Definition referencecounter.hh:177
void removeReference() const
:: ReferenceCounterInterface :: removeReference
Definition referencecounter.hh:197
void deleteObject()
:: ReferenceCounterInterface :: deleteObject
Definition referencecounter.hh:191
void addReference() const
:: ReferenceCounterInterface :: addReference
Definition referencecounter.hh:185
models a pointer to a reference countable object
Definition referencecounter.hh:224
ReferenceCounter ReferenceCounterType
type of the object, this pointer points to
Definition referencecounter.hh:231
ObjectType & operator*() const
dereference the ObjectPointer
Definition referencecounter.hh:294
ObjectPointer(const ThisType &other)
copy constructor
Definition referencecounter.hh:254
ReferenceCounterType::ObjectType ObjectType
Definition referencecounter.hh:233
unsigned int referenceCounter() const
return current reference count
Definition referencecounter.hh:301
~ObjectPointer()
destructor
Definition referencecounter.hh:266
ObjectPointer(ReferenceCounterType *const object=0)
initialize a pointer (with a standard C++ pointer)
Definition referencecounter.hh:240
ReferenceCounterType * object_
Definition referencecounter.hh:304
ThisType & operator=(const ThisType &other)
assign another pointer to this one.
Definition referencecounter.hh:274