Murl::AutoPointer< Type1 > Class Template Reference

A simple smart pointer providing strict ownership semantics. More...

#include "murl_auto_pointer.h"

Public Types

using ValueType = Type1
 The template parameter value type.
 

Public Member Functions

 AutoPointer ()
 The default constructor. More...
 
 AutoPointer (Type1 *pointer)
 Constructor taking ownership of a memory location. More...
 
 AutoPointer (const AutoPointer &autoPointer)
 Copy constructor. More...
 
template<class Type2 >
 AutoPointer (const AutoPointer< Type2 > &autoPointer)
 Constructor for automatic downcast. More...
 
AutoPointeroperator= (const AutoPointer &autoPointer)
 Assignment operator. More...
 
template<class Type2 >
AutoPointeroperator= (const AutoPointer< Type2 > &autoPointer)
 Assignment operator for an object of different type. More...
 
 ~AutoPointer ()
 The destructor. More...
 
Bool IsNull () const
 Check if the memory pointer is null. More...
 
Type1 & operator* () const
 Indirection operator. More...
 
Type1 * operator-> () const
 Dereference operator. More...
 
Type1 * Get () const
 Get the pointer to the memory. More...
 
Type1 * Release ()
 Release the memory pointer. More...
 
void Reset (Type1 *pointer=nullptr)
 Take ownership of a memory location. More...
 
template<class Type2 >
 operator AutoPointer< Type2 > ()
 Conversion operator. More...
 
 AutoPointer (AutoPointerRef< Type1 > autoPointerRef)
 Automatic conversions. More...
 

Detailed Description

template<class Type1>
class Murl::AutoPointer< Type1 >

A simple smart pointer providing strict ownership semantics.

An AutoPointer owns the object it holds a pointer to. Copying an AutoPointer copies the pointer and transfers ownership to the destination. If more than one AutoPointer owns the same object at the same time the behavior of the program is undefined.

The uses of AutoPointer include providing passing ownership of dynamically allocated memory to a function and returning dynamically allocated memory from a function.

Constructor & Destructor Documentation

◆ AutoPointer() [1/5]

template<class Type1 >
Murl::AutoPointer< Type1 >::AutoPointer ( )
inlineexplicit

The default constructor.

Set a null pointer.

◆ AutoPointer() [2/5]

template<class Type1 >
Murl::AutoPointer< Type1 >::AutoPointer ( Type1 *  pointer)
inlineexplicit

Constructor taking ownership of a memory location.

Parameters
pointerThe memory to take the ownership from.

◆ AutoPointer() [3/5]

template<class Type1 >
Murl::AutoPointer< Type1 >::AutoPointer ( const AutoPointer< Type1 > &  autoPointer)
inline

Copy constructor.

Release() the memory pointer in the const autoPointer.

Parameters
autoPointerThe AutoPointer object to transfer.

◆ AutoPointer() [4/5]

template<class Type1 >
template<class Type2 >
Murl::AutoPointer< Type1 >::AutoPointer ( const AutoPointer< Type2 > &  autoPointer)
inlineexplicit

Constructor for automatic downcast.

Release() the memory pointer in the const autoPointer.

Parameters
autoPointerThe AutoPointer object to transfer.

◆ ~AutoPointer()

template<class Type1 >
Murl::AutoPointer< Type1 >::~AutoPointer ( )
inline

The destructor.

Free (delete) the memory.

◆ AutoPointer() [5/5]

template<class Type1 >
Murl::AutoPointer< Type1 >::AutoPointer ( AutoPointerRef< Type1 >  autoPointerRef)
inline

Automatic conversions.

These operations convert an AutoPointer into and from an AutoPointerRef automatically as needed. This allows constructs such as:

AutoPointer<Derived> FuncReturningAutoPointer(...);
...
AutoPointer<Base> pointer = FuncReturningAutoPointer(...);
Parameters
autoPointerRefThe AutoPointer object to transfer.

Member Function Documentation

◆ operator=() [1/2]

template<class Type1 >
AutoPointer& Murl::AutoPointer< Type1 >::operator= ( const AutoPointer< Type1 > &  autoPointer)
inline

Assignment operator.

Release() the memory pointer in the const autoPointer.

Parameters
autoPointerThe AutoPointer object to transfer.
Returns
The object itself.

References Murl::AutoPointer< Type1 >::Release(), and Murl::AutoPointer< Type1 >::Reset().

◆ operator=() [2/2]

template<class Type1 >
template<class Type2 >
AutoPointer& Murl::AutoPointer< Type1 >::operator= ( const AutoPointer< Type2 > &  autoPointer)
inline

Assignment operator for an object of different type.

Release() the memory pointer in the const autoPointer.

Parameters
autoPointerThe AutoPointer object to transfer.
Returns
The object itself.

References Murl::AutoPointer< Type1 >::Release(), and Murl::AutoPointer< Type1 >::Reset().

◆ IsNull()

template<class Type1 >
Bool Murl::AutoPointer< Type1 >::IsNull ( ) const
inline

Check if the memory pointer is null.

Returns
True if pointer to memory is null.

◆ operator*()

template<class Type1 >
Type1& Murl::AutoPointer< Type1 >::operator* ( ) const
inline

Indirection operator.

Returns
The reference to the memory.

◆ operator->()

template<class Type1 >
Type1* Murl::AutoPointer< Type1 >::operator-> ( ) const
inline

Dereference operator.

Returns
The pointer to the memory.

◆ Get()

template<class Type1 >
Type1* Murl::AutoPointer< Type1 >::Get ( ) const
inline

Get the pointer to the memory.

Returns
The pointer to the memory.

◆ Release()

template<class Type1 >
Type1* Murl::AutoPointer< Type1 >::Release ( )
inline

Release the memory pointer.

The receiver of the memory returned takes the ownership. The memory pointer of this object is set to 0.

Returns
The pointer to the memory.

Referenced by Murl::AutoPointer< Type1 >::operator AutoPointer< Type2 >(), and Murl::AutoPointer< Type1 >::operator=().

◆ Reset()

template<class Type1 >
void Murl::AutoPointer< Type1 >::Reset ( Type1 *  pointer = nullptr)
inline

Take ownership of a memory location.

If the object already owns a memory, the memory is deleted.

Parameters
pointerThe memory to take the ownership from.

Referenced by Murl::AutoPointer< Type1 >::operator=().

◆ operator AutoPointer< Type2 >()

template<class Type1 >
template<class Type2 >
Murl::AutoPointer< Type1 >::operator AutoPointer< Type2 > ( )
inline

Conversion operator.

Transfer the memory ownership to an object of different type.

Returns
The Object of differen type.

References Murl::AutoPointer< Type1 >::Release().


The documentation for this class was generated from the following file:
  • murl_auto_pointer.h


Copyright © 2011-2024 Spraylight GmbH.