DUNE-FEM (unstable)

referencecounter.hh
1#ifndef DUNE_FEM_REFERENCECOUNTER_HH
2#define DUNE_FEM_REFERENCECOUNTER_HH
3
4#include <cassert>
5#include <type_traits>
6
7#include <dune/fem/misc/bartonnackmaninterface.hh>
8
9namespace Dune
10{
11
12 namespace Fem
13 {
14
33 template< class RCT >
35 : public BartonNackmanInterface< ReferenceCounterInterface< RCT >, typename RCT::ReferenceCounterType >
36 {
37 typedef ReferenceCounterInterface< RCT > ThisType;
38 typedef BartonNackmanInterface< ThisType, typename RCT::ReferenceCounterType > BaseType;
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:
66 void addReference () const
67 {
69 }
70
82 {
84 }
85
94 const ObjectType &getObject () const
95 {
96 CHECK_INTERFACE_IMPLEMENTATION( asImp().getObject() );
97 return asImp().getObject();
98 }
99
109 {
110 CHECK_INTERFACE_IMPLEMENTATION( asImp().getObject() );
111 return asImp().getObject();
112 }
113
123 void removeReference () const
124 {
126 }
127 };
128
129
130
131 template< class ReferenceCounter >
132 struct SupportsReferenceCounterInterface
133 {
134 typedef ReferenceCounterInterface< typename ReferenceCounter::Traits > ReferenceCounterInterfaceType;
135 static const bool v = std::is_convertible< ReferenceCounter, ReferenceCounterInterfaceType >::value;
136 };
137
138
139
151 template< class RCT >
153 : public ReferenceCounterInterface< RCT >
154 {
155 typedef ReferenceCounterDefault< RCT > ThisType;
156 typedef ReferenceCounterInterface< RCT > BaseType;
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
181 ReferenceCounterDefault ( const ThisType& ) = delete;
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
294 ObjectType &operator* () const
295 {
296 assert( object_ != 0 );
297 return object_->getObject();
298 }
299
301 unsigned int referenceCounter () const { return object_->referenceCounter(); }
302
303 protected:
304 ReferenceCounterType *object_;
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__)
Definition: bartonnackmanifcheck.hh:61
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
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
ThisType & operator=(const ThisType &other)
assign another pointer to this one.
Definition: referencecounter.hh:274
default implementation of ReferenceCounterInterface
Definition: referencecounter.hh:154
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
ReferenceCounterDefault(unsigned int refcount=1)
constructor initializing the reference counter
Definition: referencecounter.hh:177
void removeReference() const
Dune namespace. :: ReferenceCounterInterface :: removeReference
Definition: referencecounter.hh:197
void deleteObject()
Dune namespace. :: ReferenceCounterInterface :: deleteObject
Definition: referencecounter.hh:191
void addReference() const
Dune namespace. :: ReferenceCounterInterface :: addReference
Definition: referencecounter.hh:185
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
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
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)