5#ifndef DUNE_SHARED_PTR_HH
6#define DUNE_SHARED_PTR_HH
8#if defined SHARED_PTR_HEADER
9# include SHARED_PTR_HEADER
11#if defined HAVE_BOOST_SHARED_PTR_HPP
12#if defined HAVE_BOOST_MAKE_SHARED_HPP
13# include <boost/make_shared.hpp>
28#ifdef SHARED_PTR_NAMESPACE
29 using SHARED_PTR_NAMESPACE :: shared_ptr;
106 template<
class T1,
class Deleter>
152 operator bool()
const {
153 return count_ != 0 && rep_ != 0;
169 template<
class T1,
class Deleter>
170 inline void reset(T1* pointer, Deleter deleter);
180 template<
class Deleter>
181 class SharedCountImpl :
187 SharedCountImpl(T* elem,
const Deleter& deleter) :
193 SharedCountImpl(
const SharedCountImpl& rep)
194 :
SharedCount(rep), deleter_(rep.deleter_), rep_(rep.rep_) {}
205 struct DefaultDeleter
216 typedef T* *__unspecified_bool_type;
220 operator __unspecified_bool_type() const
222 return rep_ == 0 ? 0 : &shared_ptr::rep_;
232 count_ =
new SharedCountImpl<DefaultDeleter>(p, DefaultDeleter());
243 template<
class T1,
class Deleter>
247 count_ =
new SharedCountImpl<Deleter>(p, deleter);
260 : count_(other.count_), rep_(other.rep_)
268 : count_(other.count_), rep_(other.rep_)
278 return assign(other);
284 return assign(other);
294 if(rep_!=0 && --(count_->count_)<=0) {
299 count_ = other.count_;
306 if(rep_!=0 && --(count_->count_)==0) {
339 return count_->count_;
347 other.count_ = dummy;
367 template<
class T1,
class Deleter>
381#ifdef SHARED_PTR_NAMESPACE
382 using SHARED_PTR_NAMESPACE :: make_shared;
387 shared_ptr<T> make_shared()
389 return shared_ptr<T>(
new T());
392 template<
typename T,
typename Arg1>
393 shared_ptr<T> make_shared(
const Arg1& arg1)
395 return shared_ptr<T>(
new T(arg1));
398 template<
typename T,
typename Arg1,
typename Arg2>
399 shared_ptr<T> make_shared(
const Arg1& arg1,
const Arg2& arg2)
401 return shared_ptr<T>(
new T(arg1,arg2));
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)
407 return shared_ptr<T>(
new T(arg1,arg2,arg3));
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)
413 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4));
416 template<
typename T,
typename Arg1,
typename Arg2,
typename Arg3,
typename Arg4,
418 shared_ptr<T> make_shared(
const Arg1& arg1,
const Arg2& arg2,
const Arg3& arg3,
const Arg4& arg4,
421 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4,arg5));
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)
429 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4,arg5,arg6));
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)
437 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7));
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)
445 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8));
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,
454 return shared_ptr<T>(
new T(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9));
490 void operator() (T*)
const {}
520 template<
typename T,
typename T2>
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.