throwing_ptr
Smart pointers that throw on dereference if null
Public Types | Public Member Functions | Friends | List of all members
throwing::unique_ptr< T, Deleter > Class Template Reference

unique_ptr that manages a single object More...

Public Types

typedef std::unique_ptr< T, Deleter > std_unique_ptr_type
 type of the wrapped std::unique_ptr. More...
 
typedef std::unique_ptr< T, Deleter >::pointer pointer
 type of the pointer to the pointed object. More...
 
typedef std::unique_ptr< T, Deleter >::element_type element_type
 type of the pointed object. More...
 
typedef std::unique_ptr< T, Deleter >::deleter_type deleter_type
 type of the deleter. More...
 

Public Member Functions

TSP_CONSTEXPR unique_ptr () TSP_NOEXCEPT=default
 Constructs a unique_ptr that owns nothing. More...
 
TSP_CONSTEXPR unique_ptr (std::nullptr_t) TSP_NOEXCEPT
 Constructs a unique_ptr that owns nothing. More...
 
 unique_ptr (pointer ptr) TSP_NOEXCEPT
 Constructs a unique_ptr which owns p. More...
 
 unique_ptr (pointer ptr, typename std::conditional< std::is_reference< Deleter >::value, Deleter, const Deleter &>::type d1) TSP_NOEXCEPT
 Constructs a std::unique_ptr object which owns p. More...
 
 unique_ptr (pointer ptr, typename std::remove_reference< Deleter >::type &&d2) TSP_NOEXCEPT
 Constructs a std::unique_ptr object which owns p. More...
 
 unique_ptr (unique_ptr &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from u to *this. More...
 
template<class U , class E >
 unique_ptr (unique_ptr< U, E > &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E). More...
 
template<class U , class E >
 unique_ptr (std::unique_ptr< U, E > &&u) TSP_NOEXCEPT
 Constructs a unique_ptr by transferring ownership from a std::unique_ptr u to *this, where u is constructed with a specified deleter (E). More...
 
 ~unique_ptr ()=default
 Destructor If get() == nullptr there are no effects. Otherwise, the owned object is destroyed via get_deleter()(get()) on the underlying unique_ptr. More...
 
unique_ptroperator= (unique_ptr &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class U , class E >
unique_ptroperator= (unique_ptr< U, E > &&r) TSP_NOEXCEPT
 Assignment operator. More...
 
template<class U , class E >
unique_ptroperator= (std::unique_ptr< U, E > &&r) TSP_NOEXCEPT
 Assignment from std::unique_ptr operator. More...
 
unique_ptroperator= (std::nullptr_t) TSP_NOEXCEPT
 Assignment operator. More...
 
pointer release () TSP_NOEXCEPT
 Releases the ownership of the managed object if any. More...
 
void reset (pointer ptr=pointer()) TSP_NOEXCEPT
 Replaces the managed object. More...
 
void swap (unique_ptr &other) TSP_NOEXCEPT
 Swaps the managed objects and associated deleters of *this and another unique_ptr object other. More...
 
pointer get () const TSP_NOEXCEPT
 Returns a pointer to the managed object or nullptr if no object is owned. More...
 
Deleter & get_deleter () TSP_NOEXCEPT
 Returns the deleter object which would be used for destruction of the managed object. More...
 
const Deleter & get_deleter () const TSP_NOEXCEPT
 Returns the deleter object which would be used for destruction of the managed object. More...
 
 operator bool () const TSP_NOEXCEPT
 Checks whether *this owns an object, i.e. whether get() != nullptr. More...
 
std::add_lvalue_reference< T >::type operator* () const
 Dereferences the stored pointer. More...
 
pointer operator-> () const
 Dereferences the stored pointer. More...
 
std_unique_ptr_typeget_std_unique_ptr () TSP_NOEXCEPT
 Returns reference to the wrapped std::unique_ptr. More...
 
const std_unique_ptr_typeget_std_unique_ptr () const TSP_NOEXCEPT
 Returns const reference to the wrapped std::unique_ptr. More...
 

Friends

template<typename OtherT , typename OtherDeleter >
class unique_ptr
 

Detailed Description

template<typename T, typename Deleter = std::default_delete<T>>
class throwing::unique_ptr< T, Deleter >

unique_ptr that manages a single object

throwing::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.

The object is disposed of using the associated deleter when either of the following happens:

The object is disposed of using a potentially user-supplied deleter by calling get_deleter()(ptr). The default deleter uses the delete operator, which destroys the object and deallocates the memory.

A unique_ptr may alternatively own no object, in which case it is called empty.

Definition at line 38 of file unique_ptr.hpp.

Member Typedef Documentation

◆ deleter_type

template<typename T, typename Deleter = std::default_delete<T>>
typedef std::unique_ptr<T, Deleter>::deleter_type throwing::unique_ptr< T, Deleter >::deleter_type

type of the deleter.

Definition at line 47 of file unique_ptr.hpp.

◆ element_type

template<typename T, typename Deleter = std::default_delete<T>>
typedef std::unique_ptr<T, Deleter>::element_type throwing::unique_ptr< T, Deleter >::element_type

type of the pointed object.

Definition at line 45 of file unique_ptr.hpp.

◆ pointer

template<typename T, typename Deleter = std::default_delete<T>>
typedef std::unique_ptr<T, Deleter>::pointer throwing::unique_ptr< T, Deleter >::pointer

type of the pointer to the pointed object.

Definition at line 43 of file unique_ptr.hpp.

◆ std_unique_ptr_type

template<typename T, typename Deleter = std::default_delete<T>>
typedef std::unique_ptr<T, Deleter> throwing::unique_ptr< T, Deleter >::std_unique_ptr_type

type of the wrapped std::unique_ptr.

Definition at line 41 of file unique_ptr.hpp.

Constructor & Destructor Documentation

◆ unique_ptr() [1/8]

template<typename T, typename Deleter = std::default_delete<T>>
TSP_CONSTEXPR throwing::unique_ptr< T, Deleter >::unique_ptr ( )
default

Constructs a unique_ptr that owns nothing.

Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

◆ unique_ptr() [2/8]

template<typename T, typename Deleter = std::default_delete<T>>
TSP_CONSTEXPR throwing::unique_ptr< T, Deleter >::unique_ptr ( std::nullptr_t  )
inline

Constructs a unique_ptr that owns nothing.

Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

Definition at line 66 of file unique_ptr.hpp.

66 {}

◆ unique_ptr() [3/8]

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::unique_ptr ( pointer  ptr)
inlineexplicit

Constructs a unique_ptr which owns p.

Initialises the stored pointer with ptr and value-initialises the stored deleter.

Requires that Deleter is DefaultConstructible and that construction does not throw an exception.

Definition at line 76 of file unique_ptr.hpp.

76 : p(ptr) {}

◆ unique_ptr() [4/8]

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::unique_ptr ( pointer  ptr,
typename std::conditional< std::is_reference< Deleter >::value, Deleter, const Deleter &>::type  d1 
)
inline

Constructs a std::unique_ptr object which owns p.

Initialises the stored pointer with ptr and initialises the stored deleter with d1

Requires that Deleter is nothrow-CopyConstructible

Definition at line 87 of file unique_ptr.hpp.

90  : p(ptr, std::forward<decltype(d1)>(d1)) {}

◆ unique_ptr() [5/8]

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::unique_ptr ( pointer  ptr,
typename std::remove_reference< Deleter >::type &&  d2 
)
inline

Constructs a std::unique_ptr object which owns p.

Initialises the stored pointer with ptr. Moves d2 into stored_deleter.

Definition at line 97 of file unique_ptr.hpp.

99  : p(ptr, std::forward<decltype(d2)>(d2)) {}

◆ unique_ptr() [6/8]

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::unique_ptr ( unique_ptr< T, Deleter > &&  u)
inline

Constructs a unique_ptr by transferring ownership from u to *this.

If Deleter is not a reference type, requires that it is nothrow-MoveConstructible (if Deleter is a reference, get_deleter() and u.get_deleter() after move construction reference the same value)

Definition at line 108 of file unique_ptr.hpp.

108 : p(std::move(u.p)) {}

◆ unique_ptr() [7/8]

template<typename T, typename Deleter = std::default_delete<T>>
template<class U , class E >
throwing::unique_ptr< T, Deleter >::unique_ptr ( unique_ptr< U, E > &&  u)
inline

Constructs a unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E).

It depends upon whether E is a reference type, as following: a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw) b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)

This constructor only participates in overload resolution if all of the following is true: a) unique_ptr<U, E>::pointer is implicitly convertible to pointer b) U is not an array type c) Either Deleter is a reference type and E is the same type as D, or Deleter is not a reference type and E is implicitly convertible to D

Definition at line 127 of file unique_ptr.hpp.

128  : p(std::move(u.get_std_unique_ptr())) {}

◆ unique_ptr() [8/8]

template<typename T, typename Deleter = std::default_delete<T>>
template<class U , class E >
throwing::unique_ptr< T, Deleter >::unique_ptr ( std::unique_ptr< U, E > &&  u)
inline

Constructs a unique_ptr by transferring ownership from a std::unique_ptr u to *this, where u is constructed with a specified deleter (E).

It depends upon whether E is a reference type, as following: a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw) b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)

This constructor only participates in overload resolution if all of the following is true: a) std::unique_ptr<U, E>::pointer is implicitly convertible to pointer b) U is not an array type c) Either Deleter is a reference type and E is the same type as D, or Deleter is not a reference type and E is implicitly convertible to D

Definition at line 148 of file unique_ptr.hpp.

148 : p(std::move(u)) {}

◆ ~unique_ptr()

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::~unique_ptr ( )
default

Destructor If get() == nullptr there are no effects. Otherwise, the owned object is destroyed via get_deleter()(get()) on the underlying unique_ptr.

Requires that get_deleter()(get()) does not throw exceptions.

Member Function Documentation

◆ get()

template<typename T, typename Deleter = std::default_delete<T>>
pointer throwing::unique_ptr< T, Deleter >::get ( ) const
inline

Returns a pointer to the managed object or nullptr if no object is owned.

Definition at line 257 of file unique_ptr.hpp.

257 { return p.get(); }

◆ get_deleter() [1/2]

template<typename T, typename Deleter = std::default_delete<T>>
Deleter& throwing::unique_ptr< T, Deleter >::get_deleter ( )
inline

Returns the deleter object which would be used for destruction of the managed object.

Definition at line 262 of file unique_ptr.hpp.

262 { return p.get_deleter(); }

◆ get_deleter() [2/2]

template<typename T, typename Deleter = std::default_delete<T>>
const Deleter& throwing::unique_ptr< T, Deleter >::get_deleter ( ) const
inline

Returns the deleter object which would be used for destruction of the managed object.

Definition at line 267 of file unique_ptr.hpp.

267 { return p.get_deleter(); }

◆ get_std_unique_ptr() [1/2]

template<typename T, typename Deleter = std::default_delete<T>>
std_unique_ptr_type& throwing::unique_ptr< T, Deleter >::get_std_unique_ptr ( )
inline

Returns reference to the wrapped std::unique_ptr.

Definition at line 297 of file unique_ptr.hpp.

297 { return p; }

◆ get_std_unique_ptr() [2/2]

template<typename T, typename Deleter = std::default_delete<T>>
const std_unique_ptr_type& throwing::unique_ptr< T, Deleter >::get_std_unique_ptr ( ) const
inline

Returns const reference to the wrapped std::unique_ptr.

Definition at line 301 of file unique_ptr.hpp.

301  {
302  return p;
303  }

◆ operator bool()

template<typename T, typename Deleter = std::default_delete<T>>
throwing::unique_ptr< T, Deleter >::operator bool ( ) const
inlineexplicit

Checks whether *this owns an object, i.e. whether get() != nullptr.

Definition at line 272 of file unique_ptr.hpp.

272 { return p.operator bool(); }

◆ operator*()

template<typename T, typename Deleter = std::default_delete<T>>
std::add_lvalue_reference<T>::type throwing::unique_ptr< T, Deleter >::operator* ( ) const
inline

Dereferences the stored pointer.

Exceptions
null_ptr_exception<T>if the pointer is null

Definition at line 278 of file unique_ptr.hpp.

278  {
279  if (nullptr == get())
280  throw null_ptr_exception<T>();
281  return *p;
282  }

◆ operator->()

template<typename T, typename Deleter = std::default_delete<T>>
pointer throwing::unique_ptr< T, Deleter >::operator-> ( ) const
inline

Dereferences the stored pointer.

Exceptions
null_ptr_exception<T>if the pointer is null

Definition at line 288 of file unique_ptr.hpp.

288  {
289  const auto ptr = get();
290  if (nullptr == ptr)
291  throw null_ptr_exception<T>();
292  return ptr;
293  }

◆ operator=() [1/4]

template<typename T, typename Deleter = std::default_delete<T>>
unique_ptr& throwing::unique_ptr< T, Deleter >::operator= ( unique_ptr< T, Deleter > &&  r)
inline

Assignment operator.

Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Definition at line 170 of file unique_ptr.hpp.

170  {
171  p = std::move(r.p);
172  return *this;
173  }

◆ operator=() [2/4]

template<typename T, typename Deleter = std::default_delete<T>>
template<class U , class E >
unique_ptr& throwing::unique_ptr< T, Deleter >::operator= ( unique_ptr< U, E > &&  r)
inline

Assignment operator.

Transfers ownership from r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Only participates in overload resolution if U is not an array type and unique_ptr<U,E>::pointer is implicitly convertible to pointer and std::is_assignable<Deleter&, E&&>::value is true (since C++17).

Definition at line 192 of file unique_ptr.hpp.

192  {
193  p = std::move(r.p);
194  return *this;
195  }

◆ operator=() [3/4]

template<typename T, typename Deleter = std::default_delete<T>>
template<class U , class E >
unique_ptr& throwing::unique_ptr< T, Deleter >::operator= ( std::unique_ptr< U, E > &&  r)
inline

Assignment from std::unique_ptr operator.

Transfers ownership from a std::unique_ptr r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).

If Deleter is not a reference type, requires that it is nothrow-MoveAssignable.

If Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.

Only participates in overload resolution if U is not an array type and unique_ptr<U,E>::pointer is implicitly convertible to pointer and std::is_assignable<Deleter&, E&&>::value is true (since C++17).

Definition at line 214 of file unique_ptr.hpp.

214  {
215  p = std::move(r);
216  return *this;
217  }

◆ operator=() [4/4]

template<typename T, typename Deleter = std::default_delete<T>>
unique_ptr& throwing::unique_ptr< T, Deleter >::operator= ( std::nullptr_t  )
inline

Assignment operator.

Effectively the same as calling reset().

Definition at line 223 of file unique_ptr.hpp.

223  {
224  p = nullptr;
225  return *this;
226  }

◆ release()

template<typename T, typename Deleter = std::default_delete<T>>
pointer throwing::unique_ptr< T, Deleter >::release ( )
inline

Releases the ownership of the managed object if any.

get() returns nullptr after the call.

Returns
Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by get() before the call.

Definition at line 235 of file unique_ptr.hpp.

235 { return p.release(); }

◆ reset()

template<typename T, typename Deleter = std::default_delete<T>>
void throwing::unique_ptr< T, Deleter >::reset ( pointer  ptr = pointer())
inline

Replaces the managed object.

Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:

  • Saves a copy of the current pointer old_ptr = current_ptr
  • Overwrites the current pointer with the argument current_ptr = ptr
  • If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).

Definition at line 247 of file unique_ptr.hpp.

247 { p.reset(ptr); }

◆ swap()

template<typename T, typename Deleter = std::default_delete<T>>
void throwing::unique_ptr< T, Deleter >::swap ( unique_ptr< T, Deleter > &  other)
inline

Swaps the managed objects and associated deleters of *this and another unique_ptr object other.

Definition at line 252 of file unique_ptr.hpp.

252 { p.swap(other.p); }

Friends And Related Function Documentation

◆ unique_ptr

template<typename T, typename Deleter = std::default_delete<T>>
template<typename OtherT , typename OtherDeleter >
friend class unique_ptr
friend

Definition at line 50 of file unique_ptr.hpp.


The documentation for this class was generated from the following file: