Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
GEO::SmartPointer< T > Class Template Reference

A smart pointer with reference-counted copy semantics. More...

#include <geogram/basic/smart_pointer.h>

Public Member Functions

 SmartPointer ()
 Creates an empty smart pointer.
 
 SmartPointer (T *ptr)
 Creates a smart pointer that owns a pointer. More...
 
 SmartPointer (const SmartPointer< T > &rhs)
 Create a copy of a smart pointer. More...
 
 ~SmartPointer ()
 Deletes a smart pointer. More...
 
SmartPointer< T > & operator= (T *ptr)
 Assignment from a pointer. More...
 
SmartPointer< T > & operator= (const SmartPointer< T > &rhs)
 Assignment from a smart pointer. More...
 
void reset ()
 Resets pointer. More...
 
T * operator-> () const
 Dereferences object member. More...
 
T & operator* () const
 Dereferences object. More...
 
 operator T* () const
 Conversion operator. More...
 
T * get () const
 Get pointer. More...
 
bool is_null () const
 Check if stored pointer is null. More...
 

Related Functions

(Note that these are not member functions.)

template<class T1 , class T2 >
bool operator== (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Equal operator. More...
 
template<class T1 , class T2 >
bool operator!= (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Not equal operator. More...
 
template<class T1 , class T2 >
bool operator< (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Less than operator. More...
 
template<class T1 , class T2 >
bool operator<= (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Less or equal operator. More...
 
template<class T1 , class T2 >
bool operator> (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Greater than operator. More...
 
template<class T1 , class T2 >
bool operator>= (const SmartPointer< T1 > &lhs, const SmartPointer< T2 > &rhs)
 Greater or equal operator. More...
 

Detailed Description

template<class T>
class GEO::SmartPointer< T >

A smart pointer with reference-counted copy semantics.

Template Parameters
Tthe type of pointers stored in the SmartPointer.

SmartPointers have the ability of taking ownership of a pointer of type T and share that ownership: once they take ownership, the group of owners of a pointer become responsible for its deletion when the last one of them releases that ownership.

The object pointed to must implement the two following static functions:

  • T::ref(T*): to increment the reference count
  • T::unref(T*): to decrement the reference count.

More specifically, SmartPointer can be used with classes inheriting the Counted class.

See also
Counted

Definition at line 76 of file smart_pointer.h.

Constructor & Destructor Documentation

◆ SmartPointer() [1/2]

template<class T >
GEO::SmartPointer< T >::SmartPointer ( T *  ptr)
inline

Creates a smart pointer that owns a pointer.

This calls T::ref() on the pointer ptr to take ownership on it.

Parameters
[in]ptrsource pointer convertible to T*

Definition at line 91 of file smart_pointer.h.

◆ SmartPointer() [2/2]

template<class T >
GEO::SmartPointer< T >::SmartPointer ( const SmartPointer< T > &  rhs)
inline

Create a copy of a smart pointer.

This calls T::ref() on the pointer help by rhs to take ownership on it.

Parameters
[in]rhsthe smart pointer to copy

Definition at line 102 of file smart_pointer.h.

◆ ~SmartPointer()

template<class T >
GEO::SmartPointer< T >::~SmartPointer ( )
inline

Deletes a smart pointer.

This calls T::unref() to release ownership on the help pointer. If this smart pointer is the last one owning the pointer, the pointer is deleted.

Definition at line 113 of file smart_pointer.h.

Member Function Documentation

◆ get()

template<class T >
T* GEO::SmartPointer< T >::get ( ) const
inline

Get pointer.

Returns
the stored pointer

Definition at line 198 of file smart_pointer.h.

◆ is_null()

template<class T >
bool GEO::SmartPointer< T >::is_null ( ) const
inline

Check if stored pointer is null.

Returns
true if the stored pointer is null, false otherwise.

Definition at line 206 of file smart_pointer.h.

◆ operator T*()

template<class T >
GEO::SmartPointer< T >::operator T* ( ) const
inline

Conversion operator.

Returns
the stored pointer

Definition at line 190 of file smart_pointer.h.

◆ operator*()

template<class T >
T& GEO::SmartPointer< T >::operator* ( ) const
inline

Dereferences object.

Returns the stored pointer in order to dereference it. This member function shall not be called if the stored pointer is a null pointer.

Returns
the stored pointer if not null, or aborts otherwise.

Definition at line 181 of file smart_pointer.h.

◆ operator->()

template<class T >
T* GEO::SmartPointer< T >::operator-> ( ) const
inline

Dereferences object member.

Returns the stored pointer in order to access one of its members. This member function shall not be called if the stored pointer is a null pointer.

Returns
the stored pointer if not null, or aborts otherwise.

Definition at line 169 of file smart_pointer.h.

◆ operator=() [1/2]

template<class T >
SmartPointer<T>& GEO::SmartPointer< T >::operator= ( const SmartPointer< T > &  rhs)
inline

Assignment from a smart pointer.

Releases ownership on the stored pointer as if reset() were called and takes ownership on the pointer stored in rhs.

Parameters
[in]rhsthe smart pointer to copy
Returns
this smart pointer

Definition at line 140 of file smart_pointer.h.

◆ operator=() [2/2]

template<class T >
SmartPointer<T>& GEO::SmartPointer< T >::operator= ( T *  ptr)
inline

Assignment from a pointer.

Releases ownership on the stored pointer as if reset() were called and takes ownership on ptr.

Parameters
[in]ptra pointer convertible to T*
Returns
this smart pointer

Definition at line 124 of file smart_pointer.h.

◆ reset()

template<class T >
void GEO::SmartPointer< T >::reset ( )
inline

Resets pointer.

Releases ownership on the help pointer and resets it to null. The smart pointer becomes as if it were default-constructed.

Note
P.reset() is equivalent to assigning a nullptr pointer: p = nullptr

Definition at line 157 of file smart_pointer.h.

Friends And Related Function Documentation

◆ operator!=()

template<class T1 , class T2 >
bool operator!= ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Not equal operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is not equal to the pointer stored in rhs.

Definition at line 236 of file smart_pointer.h.

◆ operator<()

template<class T1 , class T2 >
bool operator< ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Less than operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is less than the pointer stored in rhs.

Definition at line 249 of file smart_pointer.h.

◆ operator<=()

template<class T1 , class T2 >
bool operator<= ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Less or equal operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is less than or equal to the pointer stored in rhs.

Definition at line 262 of file smart_pointer.h.

◆ operator==()

template<class T1 , class T2 >
bool operator== ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Equal operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is equal to the pointer stored in rhs.

Definition at line 223 of file smart_pointer.h.

◆ operator>()

template<class T1 , class T2 >
bool operator> ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Greater than operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is greater than the pointer stored in rhs.

Definition at line 275 of file smart_pointer.h.

◆ operator>=()

template<class T1 , class T2 >
bool operator>= ( const SmartPointer< T1 > &  lhs,
const SmartPointer< T2 > &  rhs 
)
related

Greater or equal operator.

Parameters
[in]lhsthe first pointer to compare
[in]rhsthe second pointer to compare
Returns
true if the pointer stored in lhs is greater than or equal to the pointer stored in rhs.

Definition at line 288 of file smart_pointer.h.


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