dune-common  2.6-git
Public Types | Public Member Functions | Public Attributes | Related Functions | List of all members
Dune::ToUniquePtr< T > Class Template Reference

An owning pointer wrapper that can be assigned to (smart) pointers. Cannot be copied. Transfers ownership by cast to any (smart) pointer type. Releases the stored pointer on transfer. NOTE: This is an intermediate solution to switch to std::unique_ptr in later releases smoothly. More...

#include <dune/common/to_unique_ptr.hh>

Inheritance diagram for Dune::ToUniquePtr< T >:
Inheritance graph

Public Types

using pointer = typename Super::pointer
 

Public Member Functions

 ToUniquePtr (pointer ptr=pointer()) noexcept
 Constructor, stores the pointer. More...
 
 ToUniquePtr (std::nullptr_t) noexcept
 Constructor, creates a nullptr More...
 
 DUNE_DEPRECATED_MSG ("Cast to raw pointer is deprecated. Use std::unique_ptr or std::shared_ptr instead.") operator pointer() noexcept
 Convert to underlying pointer, releases the stored pointer. NOTE: deprecated. More...
 
 operator std::unique_ptr< T > () noexcept
 Convert to unique_ptr, invalidates the stored pointer. More...
 
 operator std::shared_ptr< T > () noexcept
 Convert to shared_ptr, invalidates the stored pointer. More...
 
 operator bool () noexcept
 Checks whether *this owns an object. More...
 
 operator bool () const noexcept
 Checks whether *this owns an object. More...
 
T * operator-> ()
 STL member. More...
 

Public Attributes

ptr
 STL member. More...
 

Related Functions

(Note that these are not member functions.)

template<class T , class... Args>
ToUniquePtr< T > makeToUnique (Args &&... args)
 Constructs an object of type T and wraps it in a ToUniquePtr,. More...
 

Detailed Description

template<class T>
class Dune::ToUniquePtr< T >

An owning pointer wrapper that can be assigned to (smart) pointers. Cannot be copied. Transfers ownership by cast to any (smart) pointer type. Releases the stored pointer on transfer. NOTE: This is an intermediate solution to switch to std::unique_ptr in later releases smoothly.

Example of usage:

ToUniquePtr<int> f() { return new int(1); }
auto g() { return makeToUnique<int>(2); }
int* p1 = f(); // p1 gets ownership, must delete explicitly
delete p1;
std::unique_ptr<int> p2 = f();
std::shared_ptr<int> p3 = f();
auto p4 = f(); // ToUniquePtr has itself pointer semantic
std::cout << *p4 << '\n';
std::unique_ptr<int> p5( g() );

Member Typedef Documentation

◆ pointer

template<class T >
using Dune::ToUniquePtr< T >::pointer = typename Super::pointer

Constructor & Destructor Documentation

◆ ToUniquePtr() [1/2]

template<class T >
Dune::ToUniquePtr< T >::ToUniquePtr ( pointer  ptr = pointer())
inlinenoexcept

Constructor, stores the pointer.

◆ ToUniquePtr() [2/2]

template<class T >
Dune::ToUniquePtr< T >::ToUniquePtr ( std::nullptr_t  )
inlinenoexcept

Constructor, creates a nullptr

Member Function Documentation

◆ DUNE_DEPRECATED_MSG()

template<class T >
Dune::ToUniquePtr< T >::DUNE_DEPRECATED_MSG ( "Cast to raw pointer is deprecated. Use std::unique_ptr or std::shared_ptr instead."  )
inlinenoexcept

Convert to underlying pointer, releases the stored pointer. NOTE: deprecated.

◆ operator bool() [1/2]

template<class T >
Dune::ToUniquePtr< T >::operator bool ( ) const
inlineexplicitnoexcept

Checks whether *this owns an object.

◆ operator bool() [2/2]

template<class T >
Dune::ToUniquePtr< T >::operator bool ( )
inlineexplicitnoexcept

Checks whether *this owns an object.

◆ operator std::shared_ptr< T >()

template<class T >
Dune::ToUniquePtr< T >::operator std::shared_ptr< T > ( )
inlinenoexcept

Convert to shared_ptr, invalidates the stored pointer.

◆ operator std::unique_ptr< T >()

template<class T >
Dune::ToUniquePtr< T >::operator std::unique_ptr< T > ( )
inlinenoexcept

Convert to unique_ptr, invalidates the stored pointer.

◆ operator->()

T* std::unique_ptr< T >::operator->
inherited

STL member.

Friends And Related Function Documentation

◆ makeToUnique()

template<class T , class... Args>
ToUniquePtr< T > makeToUnique ( Args &&...  args)
related

Constructs an object of type T and wraps it in a ToUniquePtr,.

Member Data Documentation

◆ ptr

T std::unique_ptr< T >::ptr
inherited

STL member.


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