Ookii.CommandLine for C++  1.0.0
Public Types | Public Member Functions | List of all members
ookii::owned_or_borrowed_ptr< T > Class Template Reference

Smart pointer that may or may not own the contained pointer. More...

#include <ookii/owned_or_borrowed_ptr.h>

Public Types

using element_type = T
 The type of object referred to by the pointer.
 
using pointer = T *
 A pointer to T.
 
using reference = std::add_lvalue_reference_t< T >
 A reference to T.
 

Public Member Functions

 owned_or_borrowed_ptr () noexcept=default
 Initializes a new instance of the owned_or_borrowed_ptr class, which does not contain any pointer.
 
 owned_or_borrowed_ptr (const owned_or_borrowed_ptr &)=delete
 
 owned_or_borrowed_ptr (owned_or_borrowed_ptr &&other) noexcept
 Move constructor. More...
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2*, pointer>>>
 owned_or_borrowed_ptr (owned_or_borrowed_ptr< T2 > &&other) noexcept
 Move constructor from a different owned_or_borrowed_ptr if the pointer types can be implicitly converted. More...
 
 owned_or_borrowed_ptr (pointer ptr, bool owned=true) noexcept
 Initializes a new instance of the owned_or_borrowed_ptr class, which contains the specified pointer. More...
 
 owned_or_borrowed_ptr (std::nullptr_t) noexcept
 Initializes a new instance of the owned_or_borrowed_ptr class, which does not contain any pointer.
 
 ~owned_or_borrowed_ptr ()
 Deletes the contained pointer if it is not NULL and if it's owned.
 
owned_or_borrowed_ptr as_borrowed () const noexcept
 Creates a borrowed pointer that refers to the same pointer as this instance.
 
pointer get () const noexcept
 Gets the contained pointer.
 
bool is_owned () const noexcept
 Gets a value that indicates whether the contained pointer is owned. More...
 
 operator bool () const noexcept
 Gets a value that indicates if the contained pointer is not NULL. More...
 
reference operator* () const noexcept
 Dererences the contained pointer. More...
 
pointer operator-> () const noexcept
 Member access operator.
 
owned_or_borrowed_ptroperator= (const owned_or_borrowed_ptr &)=delete
 
owned_or_borrowed_ptroperator= (owned_or_borrowed_ptr &&other) noexcept
 Move assignment operator. More...
 
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2*, pointer>>>
owned_or_borrowed_ptroperator= (owned_or_borrowed_ptr< T2 > &&other) noexcept
 Move assignment operator from a different owned_or_borrowed_ptr if the pointer types can be implicitly converted. More...
 
owned_or_borrowed_ptroperator= (std::nullptr_t) noexcept
 Assigns a NULL value to this instance.
 
pointer release () noexcept
 Releases the contained pointer without deleting it, even if it was owned. More...
 
void reset (pointer ptr=nullptr, bool owned=true) noexcept
 Resets this instance with a new pointer value. More...
 
void reset (std::nullptr_t) noexcept
 Resets this instance with a NULL pointer value. More...
 
void swap (owned_or_borrowed_ptr &other) noexcept
 Swaps this owned_or_borrowed_ptr with another. More...
 

Detailed Description

template<typename T>
class ookii::owned_or_borrowed_ptr< T >

Smart pointer that may or may not own the contained pointer.

This type is a smart pointer that will free the contained pointer on destruction if it's owned, or will do nothing if it's borrowed. Essentially, it acts like a std::unique_ptr<T> if the pointer is owned, and like a raw pointer if not.

This type is used to store both owned and duplicate references to the same objects in a single list.

Warning
The borrowed pointers don't have any influence on the lifetime of their owned counterparts, so it's up to the user to assure that the borrowed pointer doesn't outlive the underlying pointer.
Template Parameters
TThe type of object referred to by the pointer. Should not be an array type.

Constructor & Destructor Documentation

◆ owned_or_borrowed_ptr() [1/3]

template<typename T >
ookii::owned_or_borrowed_ptr< T >::owned_or_borrowed_ptr ( pointer  ptr,
bool  owned = true 
)
inlinenoexcept

Initializes a new instance of the owned_or_borrowed_ptr class, which contains the specified pointer.

Parameters
ptrThe pointer the object should hold.
ownedtrue if the instance should delete the pointer when destructed; otherwise, false. The default is true.

◆ owned_or_borrowed_ptr() [2/3]

template<typename T >
ookii::owned_or_borrowed_ptr< T >::owned_or_borrowed_ptr ( owned_or_borrowed_ptr< T > &&  other)
inlinenoexcept

Move constructor.

Parameters
otherThe owned_or_borrowed_ptr to move from.

◆ owned_or_borrowed_ptr() [3/3]

template<typename T >
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2*, pointer>>>
ookii::owned_or_borrowed_ptr< T >::owned_or_borrowed_ptr ( owned_or_borrowed_ptr< T2 > &&  other)
inlinenoexcept

Move constructor from a different owned_or_borrowed_ptr if the pointer types can be implicitly converted.

Parameters
otherThe owned_or_borrowed_ptr to move from.

Member Function Documentation

◆ is_owned()

template<typename T >
bool ookii::owned_or_borrowed_ptr< T >::is_owned ( ) const
inlinenoexcept

Gets a value that indicates whether the contained pointer is owned.

If this method returns true, the reset() method and the destructor will delete the contained pointer. If it's false, they will do nothing to that pointer.

This method may return true if the contained pointer is NULL, in which case it has no significance.

Returns
true if the contained pointer is owned; otherwise, false.

◆ operator bool()

template<typename T >
ookii::owned_or_borrowed_ptr< T >::operator bool ( ) const
inlineexplicitnoexcept

Gets a value that indicates if the contained pointer is not NULL.

Returns
true if the contained pointer is not NULL; otherwise, false.

◆ operator*()

template<typename T >
reference ookii::owned_or_borrowed_ptr< T >::operator* ( ) const
inlinenoexcept

Dererences the contained pointer.

Warning
This method does not check if the contained pointer is NULL.
Returns
A reference to the object refered to by the pointer.

◆ operator=() [1/2]

template<typename T >
owned_or_borrowed_ptr& ookii::owned_or_borrowed_ptr< T >::operator= ( owned_or_borrowed_ptr< T > &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe owned_or_borrowed_ptr to move from.

◆ operator=() [2/2]

template<typename T >
template<typename T2 , typename = std::enable_if_t<std::is_convertible_v<T2*, pointer>>>
owned_or_borrowed_ptr& ookii::owned_or_borrowed_ptr< T >::operator= ( owned_or_borrowed_ptr< T2 > &&  other)
inlinenoexcept

Move assignment operator from a different owned_or_borrowed_ptr if the pointer types can be implicitly converted.

Parameters
otherThe owned_or_borrowed_ptr to move from.

◆ release()

template<typename T >
pointer ookii::owned_or_borrowed_ptr< T >::release ( )
inlinenoexcept

Releases the contained pointer without deleting it, even if it was owned.

After calling this method, this instance will refer to a NULL pointer. The value of is_owned() will not change.

Returns
The previously contained pointer.

◆ reset() [1/2]

template<typename T >
void ookii::owned_or_borrowed_ptr< T >::reset ( pointer  ptr = nullptr,
bool  owned = true 
)
inlinenoexcept

Resets this instance with a new pointer value.

If this instance currently contains a non-NULL owned pointer, it is deleted.

Parameters
ptrThe pointer the object should hold.
ownedtrue if the instance should delete the pointer when destructed; otherwise, false. The default is true.

◆ reset() [2/2]

template<typename T >
void ookii::owned_or_borrowed_ptr< T >::reset ( std::nullptr_t  )
inlinenoexcept

Resets this instance with a NULL pointer value.

If this instance currently contains a non-NULL owned pointer, it is deleted.

◆ swap()

template<typename T >
void ookii::owned_or_borrowed_ptr< T >::swap ( owned_or_borrowed_ptr< T > &  other)
inlinenoexcept

Swaps this owned_or_borrowed_ptr with another.

Parameters
otherThe owned_or_borrowed_ptr to swap with.

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