DCMTK Version 3.7.0 OFFIS DICOM Toolkit

Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
OFunique_ptr< T > Class Template Reference

OFunique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. More...

## Public Types
typedef T * | pointer
| typedef of pointer type T* for template programmers.
typedef T | element_type
| T, the type of the object managed by this unique_ptr.

## Public Member Functions
| OFunique_ptr ()
| Constructs an empty OFunique_ptr.
| OFunique_ptr (pointer p)
| Constructs a OFunique_ptr which owns p. More...
| OFunique_ptr (OFrvalue_ref(OFunique_ptr) rhs)
| Move constructor. More...
OFunique_ptr & | operator= (OFrvalue_ref(OFunique_ptr) rhs)
| Move assignment operator. More...
| ~OFunique_ptr ()
| If get() == OFnullptr there are no effects – otherwise, the owned object is destroyed.
void | reset (pointer p=OFnullptr)
| Replaces the managed object. More...
pointer | release ()
| Releases the ownership of the managed object if any. More...
pointer | get () const
| Returns a pointer to the managed object or OFnullptr if no object is owned. More...
| operator bool () const
| Checks whether *this owns an object, i.e. More...
bool | operator! () const
| Checks whether *this does NOT own an object, i.e. More...
T & | operator* () const
| Access the object owned by *this. More...
pointer | operator-> () const
| Access the object owned by *this. More...

## Private Member Functions
| OFunique_ptr (const OFunique_ptr &)
| Disable copy construction.
OFunique_ptr & | operator= (const OFunique_ptr &)
| Disable copy assignment.

## Private Attributes
pointer | m_pData
| The underlying (raw) pointer.

## Detailed Description

### template<typename T>
class OFunique_ptr< T >

OFunique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope.

No two unique_ptr instances can manage the same object.

The object is destroyed and its memory deallocated when either of the following happens:

- unique_ptr managing the object is destroyed
- unique_ptr managing the object is assigned another pointer via reset().

A unique_ptr may also own no objects, in which case it is called empty.

OFunique_ptr is NOT CopyConstructible or CopyAssignable.

Template Parameters

  T | the type of the managed object, e.g. int for an OFunique_ptr behaving like an int*.

Note
  this implementation is meant to be a subset of the c++11's std::unique_ptr that lacks the following features: swap support, custom deleters, specialized handling of pointers to arrays and some functions like comparing OFunique_ptrs or creating a hash key. see http://en.cppreference.com/w/cpp/memory/unique_ptr to compare OFunique_ptr against std::unique_ptr.

## Constructor & Destructor Documentation

## ◆ OFunique_ptr() [1/2]

template<typename T >

OFunique_ptr< T >::OFunique_ptr | ( | pointer | p | ) |
| inline explicit

Constructs a OFunique_ptr which owns p.

Parameters

  p | the pointer that's going to be owned by this unique_ptr.

## ◆ OFunique_ptr() [2/2]

template<typename T >

OFunique_ptr< T >::OFunique_ptr | ( | OFrvalue_ref(OFunique_ptr< T >) | rhs | ) |

Move constructor.

The move constructor moves the ownership of the managed object from its parameter to the newly created OFunique_ptr object, effectively emptying the former OFunique_ptr object.

Parameters

  rhs | an rvalue reference to another OFunique_ptr object, e.g. obtained via OFmove.

### Example

This example shows how to move the ownership of a managed object from one OFunique_ptr to another. Note that the ownership always remains unique in this example, even if an error occurred while reading the Dataset. Therefore no memory leaks are possible!

void consumeDataset( OFunique_ptr<DcmDataset> pDataset )
. . .
OFunique_ptr<DcmDataset> pDataset( new DcmDataset );
if( pDataset && pDataset->read( ... ).good() )
{
// Error: copy constructor not available, as it would
// compromise the unique ownership principle
// consumeDataset( pDataset );
// OFmove allows us to 'tell' OFunique_ptr we want
// to give away the ownership.
consumeDataset( OFmove( pDataset ) );
}
DcmDataset
a class handling the DICOM dataset format (files without meta header)
Definition: dcdatset.h:42
OFunique_ptr
OFunique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destro...
Definition: ofmem.h:285
OFmove
OFconstexpr xvalue OFmove(T< unspecified > t)
Obtains an rvalue reference to its argument and converts it to an xvalue.

References OFrvalue_access().

## Member Function Documentation

## ◆ get()

template<typename T >

pointer OFunique_ptr< T >::get | ( |  | ) | const
| inline

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

Returns
  Pointer to the managed object or OFnullptr if no object is owned.

References OFunique_ptr< T >::m_pData.

## ◆ operator bool()

template<typename T >

OFunique_ptr< T >::operator bool | ( |  | ) | const
| inline

Checks whether *this owns an object, i.e.

whether get() != OFnullptr.

Returns
  get() != OFnullptr.

References OFunique_ptr< T >::m_pData.

## ◆ operator!()

template<typename T >

bool OFunique_ptr< T >::operator! | ( |  | ) | const
| inline

Checks whether *this does NOT own an object, i.e.

whether get() == OFnullptr.

Returns
  get() == OFnullptr.

References OFunique_ptr< T >::m_pData.

## ◆ operator*()

template<typename T >

T& OFunique_ptr< T >::operator* | ( |  | ) | const
| inline

Access the object owned by *this.

Returns
  the object owned by *this, i.e. *get().

References OFunique_ptr< T >::m_pData.

## ◆ operator->()

template<typename T >

pointer OFunique_ptr< T >::operator-> | ( |  | ) | const
| inline

Access the object owned by *this.

Returns
  same as get().

References OFunique_ptr< T >::m_pData.

## ◆ operator=()

template<typename T >

OFunique_ptr& OFunique_ptr< T >::operator= | ( | OFrvalue_ref(OFunique_ptr< T >) | rhs | ) |

Move assignment operator.

The move assignment operator moves the ownership of the managed object from its parameter to itself, effectively emptying the other OFunique_ptr object. See OFunique_ptr's move constructor for more information.

Parameters

  rhs | an rvalue reference to another OFunique_ptr object, e.g. obtained via OFmove.

References OFunique_ptr< T >::m_pData, OFrvalue_access(), and OFunique_ptr< T >::reset().

## ◆ release()

template<typename T >

pointer OFunique_ptr< T >::release | ( |  | ) |
| inline

Releases the ownership of the managed object if any.

Retrieves the owned object (if any) and lets the unique_ptr become empty.

Returns
  same as get().

References OFunique_ptr< T >::m_pData.

## ◆ reset()

template<typename T >

void OFunique_ptr< T >::reset | ( | pointer | p = `OFnullptr` | ) |
| inline

Replaces the managed object.

The previously owned object is deleted if the unique_ptr was not empty.

Parameters

  p | the new pointer to be owned, defaults to OFnullptr.

References OFunique_ptr< T >::m_pData.

Referenced by OFunique_ptr< T >::operator=().

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

- ofstd/include/dcmtk/ofstd/ofmem.h

Generated on Mon Dec 15 2025 for DCMTK Version 3.7.0 by Doxygen 1.9.1