Dune Core Modules (2.3.1)

shared_ptr.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// $Id: smartpointer.hh 5504 2009-04-08 13:35:31Z christi $
4
5#ifndef DUNE_SHARED_PTR_HH
6#define DUNE_SHARED_PTR_HH
7
8#if defined SHARED_PTR_HEADER
9# include SHARED_PTR_HEADER
10#endif
11#if defined HAVE_BOOST_SHARED_PTR_HPP
12#if defined HAVE_BOOST_MAKE_SHARED_HPP
13# include <boost/make_shared.hpp>
14#endif
15#endif
16
25namespace Dune
26{
27 // A shared_ptr implementation has been found if SHARED_PTR_NAMESPACE is set at all
28#ifdef SHARED_PTR_NAMESPACE
29 using SHARED_PTR_NAMESPACE :: shared_ptr;
30#else
31
39 {
40 template<class T1>
41 friend class shared_ptr;
42 protected:
44 int count_;
49 : count_(rep.count_) {}
50
52 virtual ~SharedCount() {};
53
54 };
62 template<class T>
64 {
65 template<class T1> friend class shared_ptr;
66
67 public:
73 typedef T element_type;
74
78 inline shared_ptr();
79
80 inline shared_ptr(nullptr_t null);
81
90 template<class T1>
91 inline shared_ptr(T1 * pointer);
92
93
106 template<class T1, class Deleter>
107 inline shared_ptr(T1 * pointer, Deleter deleter);
108
113 template<class T1>
114 inline shared_ptr(const shared_ptr<T1>& pointer);
115
120 inline shared_ptr(const shared_ptr& pointer);
121
125 inline ~shared_ptr();
126
128 template<class T1>
129 inline shared_ptr& operator=(const shared_ptr<T1>& pointer);
130
132 inline shared_ptr& operator=(const shared_ptr& pointer);
133
136
139
141 inline const element_type& operator*() const;
142
144 inline const element_type* operator->() const;
145
147 element_type* get() const {
148 return rep_;
149 }
150
152 operator bool() const {
153 return count_ != 0 && rep_ != 0;
154 }
155
157 inline void swap(shared_ptr& other);
158
162 inline void reset();
163
165 template<class T1>
166 inline void reset(T1* pointer);
167
168 //** \brief Same as shared_ptr(pointer,deleter).swap(*this)
169 template<class T1, class Deleter>
170 inline void reset(T1* pointer, Deleter deleter);
171
173 int use_count() const;
174
175 private:
177 template<class T1>
178 inline shared_ptr& assign(const shared_ptr<T1>& pointer);
180 template<class Deleter>
181 class SharedCountImpl :
182 public SharedCount
183 {
184 template<class T1>
185 friend class shared_ptr;
187 SharedCountImpl(T* elem,const Deleter& deleter) :
188 SharedCount(),
189 deleter_(deleter),
190 rep_(elem)
191 {}
193 SharedCountImpl(const SharedCountImpl& rep)
194 : SharedCount(rep), deleter_(rep.deleter_), rep_(rep.rep_) {}
196 ~SharedCountImpl()
197 { deleter_(rep_); }
198
199 // store a copy of the deleter
200 Deleter deleter_;
201 T* rep_;
202 };
203
205 struct DefaultDeleter
206 {
207 void operator() (element_type* p) const
208 { delete p; }
209 };
210
211
212 SharedCount *count_;
213 T *rep_;
214
215 // Needed for the implicit conversion to "bool"
216 typedef T* *__unspecified_bool_type;
217
218 public:
220 operator __unspecified_bool_type() const // never throws
221 {
222 return rep_ == 0 ? 0 : &shared_ptr::rep_;
223 }
224
225 };
226
227 template<class T>
228 template<class T1>
230 {
231 rep_ = p;
232 count_ = new SharedCountImpl<DefaultDeleter>(p, DefaultDeleter());
233 }
234
235 template<class T>
237 {
238 rep_ = 0;
239 count_ = 0;
240 }
241
242 template<class T>
243 template<class T1, class Deleter>
244 inline shared_ptr<T>::shared_ptr(T1 * p, Deleter deleter)
245 {
246 rep_ = p;
247 count_ = new SharedCountImpl<Deleter>(p, deleter);
248 }
249
250 template<class T>
252 {
253 rep_ = 0;
254 count_=0;
255 }
256
257 template<class T>
258 template<class T1>
260 : count_(other.count_), rep_(other.rep_)
261 {
262 if (rep_)
263 ++(count_->count_);
264 }
265
266 template<class T>
268 : count_(other.count_), rep_(other.rep_)
269 {
270 if (rep_)
271 ++(count_->count_);
272 }
273
274 template<class T>
275 template<class T1>
277 {
278 return assign(other);
279 }
280
281 template<class T>
283 {
284 return assign(other);
285 }
286
287 template<class T>
288 template<class T1>
290 {
291 if (other.count_)
292 (other.count_->count_)++;
293
294 if(rep_!=0 && --(count_->count_)<=0) {
295 delete count_;
296 }
297
298 rep_ = other.rep_;
299 count_ = other.count_;
300 return *this;
301 }
302
303 template<class T>
305 {
306 if(rep_!=0 && --(count_->count_)==0) {
307 delete count_;
308 rep_=0;
309 }
310 }
311
312 template<class T>
314 {
315 return *(rep_);
316 }
317
318 template<class T>
320 {
321 return rep_;
322 }
323
324 template<class T>
325 inline const T& shared_ptr<T>::operator*() const
326 {
327 return *(rep_);
328 }
329
330 template<class T>
331 inline const T *shared_ptr<T>::operator->() const
332 {
333 return rep_;
334 }
335
336 template<class T>
337 inline int shared_ptr<T>::use_count() const
338 {
339 return count_->count_;
340 }
341
342 template<class T>
344 {
345 SharedCount* dummy = count_;
346 count_=other.count_;
347 other.count_ = dummy;
348 T* tdummy=rep_;
349 rep_ = other.rep_;
350 other.rep_ = tdummy;
351 }
352
353 template<class T>
355 {
356 shared_ptr<T>().swap(*this);
357 }
358
359 template<class T>
360 template<class T1>
361 inline void shared_ptr<T>::reset(T1* pointer)
362 {
363 shared_ptr<T>(pointer).swap(*this);
364 }
365
366 template<class T>
367 template<class T1, class Deleter>
368 inline void shared_ptr<T>::reset(T1* pointer, Deleter deleter)
369 {
370 shared_ptr<T>(pointer, deleter).swap(*this);
371 }
372
374#endif // #ifdef SHARED_PTR_NAMESPACE
375
376
377 // C++0x and Boost have a make_shared implementation, TR1 does not.
378 // Unfortunately, TR1 gets picked over Boost if present.
379 // Moreover, boost::make_shared() only exists for (remotely) recent versions of Boost.
380#if HAVE_MAKE_SHARED
381#ifdef SHARED_PTR_NAMESPACE
382 using SHARED_PTR_NAMESPACE :: make_shared;
383#endif
384#else
385
386 template<typename T>
387 shared_ptr<T> make_shared()
388 {
389 return shared_ptr<T>(new T());
390 }
391
392 template<typename T, typename Arg1>
393 shared_ptr<T> make_shared(const Arg1& arg1)
394 {
395 return shared_ptr<T>(new T(arg1));
396 }
397
398 template<typename T, typename Arg1, typename Arg2>
399 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2)
400 {
401 return shared_ptr<T>(new T(arg1,arg2));
402 }
403
404 template<typename T, typename Arg1, typename Arg2, typename Arg3>
405 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
406 {
407 return shared_ptr<T>(new T(arg1,arg2,arg3));
408 }
409
410 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
411 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4)
412 {
413 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4));
414 }
415
416 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
417 typename Arg5>
418 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
419 const Arg5& arg5)
420 {
421 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5));
422 }
423
424 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
425 typename Arg5, typename Arg6>
426 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
427 const Arg5& arg5, const Arg6& arg6)
428 {
429 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6));
430 }
431
432 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
433 typename Arg5, typename Arg6, typename Arg7>
434 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
435 const Arg5& arg5, const Arg6& arg6, const Arg7& arg7)
436 {
437 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7));
438 }
439
440 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
441 typename Arg5, typename Arg6, typename Arg7, typename Arg8>
442 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
443 const Arg5& arg5, const Arg6& arg6, const Arg7& arg7, const Arg8& arg8)
444 {
445 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8));
446 }
447
448 template<typename T, typename Arg1, typename Arg2, typename Arg3, typename Arg4,
449 typename Arg5, typename Arg6, typename Arg7, typename Arg8, typename Arg9>
450 shared_ptr<T> make_shared(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4,
451 const Arg5& arg5, const Arg6& arg6, const Arg7& arg7, const Arg8& arg8,
452 const Arg9& arg9)
453 {
454 return shared_ptr<T>(new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9));
455 }
456
457#endif // custom make_shared
458
487 template<class T>
489 {
490 void operator() (T*) const {}
491 };
492
501 template<typename T>
503 {
504 return shared_ptr<T>(&t, null_deleter<T>());
505 }
506
520 template<typename T, typename T2>
522 {
523 return shared_ptr<T2>(dynamic_cast<T2*>(&t), null_deleter<T2>());
524 }
525
526}
527#endif
The object we reference.
Definition: shared_ptr.hh:39
int count_
The number of references.
Definition: shared_ptr.hh:44
SharedCount(const SharedCount &rep)
Copy constructor with type conversion.
Definition: shared_ptr.hh:48
SharedCount()
Constructor from existing Pointer.
Definition: shared_ptr.hh:46
virtual ~SharedCount()
Destructor, deletes element_type* rep_.
Definition: shared_ptr.hh:52
A reference counting smart pointer.
Definition: shared_ptr.hh:64
shared_ptr & operator=(const shared_ptr< T1 > &pointer)
Assignment operator.
T element_type
The data type we are a pointer for.
Definition: shared_ptr.hh:73
element_type * get() const
Access to the raw pointer, if you really want it.
Definition: shared_ptr.hh:147
shared_ptr< T2 > stackobject_to_shared_ptr(T &t)
Convert a stack object to a shared_ptr of a base class.
Definition: shared_ptr.hh:521
shared_ptr< T > stackobject_to_shared_ptr(T &t)
Convert a stack-allocated object to a shared_ptr:
Definition: shared_ptr.hh:502
Fallback implementation of nullptr.
Definition: nullptr.hh:19
const element_type * operator->() const
Dereference as const pointer.
Definition: shared_ptr.hh:331
void reset()
Decrease the reference count by one and free the memory if the reference count has reached 0.
Definition: shared_ptr.hh:354
element_type & operator*()
Dereference as object.
Definition: shared_ptr.hh:313
shared_ptr(const shared_ptr< T1 > &pointer)
Copy constructor.
Definition: shared_ptr.hh:259
shared_ptr(T1 *pointer)
Constructs a new smart pointer from a preallocated Object.
Definition: shared_ptr.hh:229
shared_ptr(const shared_ptr &pointer)
Copy constructor.
Definition: shared_ptr.hh:267
int use_count() const
The number of shared_ptrs pointing to the object we point to.
Definition: shared_ptr.hh:337
element_type * operator->()
Dereference as pointer.
Definition: shared_ptr.hh:319
void swap(shared_ptr &other)
Swap content of this shared_ptr and another.
Definition: shared_ptr.hh:343
void reset(T1 *pointer)
Detach shared pointer and set it anew for the given pointer.
Definition: shared_ptr.hh:361
const element_type & operator*() const
Dereference as const object.
Definition: shared_ptr.hh:325
shared_ptr()
Constructs a new smart pointer and allocates the referenced Object.
Definition: shared_ptr.hh:251
shared_ptr(T1 *pointer, Deleter deleter)
Constructs a new smart pointer from a preallocated Object.
Definition: shared_ptr.hh:244
~shared_ptr()
Destructor.
Definition: shared_ptr.hh:304
shared_ptr & operator=(const shared_ptr &pointer)
Assignment operator.
Definition: shared_ptr.hh:282
Dune namespace.
Definition: alignment.hh:14
Fallback implementation of the nullptr object in C++0x.
implements the Deleter concept of shared_ptr without deleting anything
Definition: shared_ptr.hh:489
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)