Cgu::ScopedHandle< T, Dealloc > Class Template Reference
handles and smart pointers

This is a generic scoped class for managing the lifetime of objects allocated on freestore. More...

`#include <c++-gtk-utils/shared_handle.h>`

## Public Member Functions

| ScopedHandle (const ScopedHandle &)=delete
ScopedHandle & | operator= (const ScopedHandle &)=delete
| ScopedHandle (T ptr=0)
void | reset (T ptr=0)
T | release ()
T | get () const
| operator T () const
| ~ScopedHandle ()

## Detailed Description

### template<class T, class Dealloc>
class Cgu::ScopedHandle< T, Dealloc >

This is a generic scoped class for managing the lifetime of objects allocated on freestore.

See Also
  SharedHandle SharedLockHandle SharedHandleError
  StandardArrayDelete CFree GFree GerrorFree GSliceFree GSliceFreeSize GSliceDestroy

This class deletes its object as soon as it goes out of scope. It can be viewed as a SharedHandle which cannot be assigned to or used as the argument to a copy constructor and therefore which cannot have a reference count of more than 1.

ScopedHandle objects can be instantiated for pointers to constant objects (such as ScopedHandle<const char*>), provided the deleter functor will take such pointers.

This library provides StandardArrayDelete, CFree, GFree, GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter functors, which can be used as the second template parameter of the ScopedHandle class. StandardArrayDelete is the default, and some typedef'ed instances of ScopedHandle for gchar (with the GFree deleter) and for GError (with the GerrorFree deleter) are provided: GcharScopedHandle and GerrorScopedHandle)

Comparison with std::unique_ptr

Although the semantics of std::unique_ptr in C++11 are not particularly suited to managing C objects with accessor functions (such as in glib), most of the things that can be done by this class can be done by using std::unique_ptr with a specialised deleter. However, this class is retained in the c++-gtk-utils library not only to retain compatibility with series 1.2 of the library, but also to cater for some cases not so easily met by std::unique_ptr:

1. The Cgu::ScopedHandle class takes its deleter as a template parameter, which means that typedefs can be used to enable handles for particular deleters to be easily created (and as mentioned, this library provides a number of pre-formed deleter functors and typedefs for them). With std::unique_ptr, custom deleters must be passed to the unique_ptr constructor on every occasion a unique_ptr is constructed to manage a new object (and they cannot be templated as a typedef).
2. This class provides non-move enforcement without making a const instance of it. A const std::unique_ptr cannot be moved from or to, but then it cannot have release() or reset() called for it either.

## Constructor & Destructor Documentation

template<class T, class Dealloc>

Cgu::ScopedHandle< T, Dealloc >::ScopedHandle | ( | const ScopedHandle< T, Dealloc > & |  | ) |

delete

This class cannot be copied. The copy constructor is deleted.

template<class T, class Dealloc>

Cgu::ScopedHandle< T, Dealloc >::ScopedHandle | ( | T | ptr = `0` | ) |

inlineexplicit

The constructor does not throw.

Parameters

  ptr | The object which the ScopedHandle is to manage (if any).

ScopedHandle objects can be instantiated for pointers to constant objects (such as SharedHandle<const char*>), provided the deleter functor will take such pointers.

template<class T, class Dealloc>

Cgu::ScopedHandle< T, Dealloc >::~ScopedHandle | ( |  | ) |

inline

The destructor does not throw unless the destructor of a handled object throws - that should never happen.

## Member Function Documentation

template<class T, class Dealloc>

T Cgu::ScopedHandle< T, Dealloc >::get | ( |  | ) | const

inline

This method does not throw.

Returns
  A pointer to the handled object (or NULL if none is handled).

template<class T, class Dealloc>

Cgu::ScopedHandle< T, Dealloc >::operator T | ( |  | ) | const

inline

This method does not throw.

Returns
  A pointer to the handled object (or NULL if none is handled).

template<class T, class Dealloc>

ScopedHandle& Cgu::ScopedHandle< T, Dealloc >::operator= | ( | const ScopedHandle< T, Dealloc > & |  | ) |

delete

This class cannot be copied. The assignment operator is deleted.

template<class T, class Dealloc>

T Cgu::ScopedHandle< T, Dealloc >::release | ( |  | ) |

inline

Causes the ScopedHandle to cease to manage the handled object, but does not delete that object. This method does not throw.

Returns
  A pointer to the previously handled object (or NULL if none was handled).

template<class T, class Dealloc>

void Cgu::ScopedHandle< T, Dealloc >::reset | ( | T | ptr = `0` | ) |

inline

Causes the ScopedHandle to delete its managed object (if any), and if the argument passed is not NULL, the ScopedHandle object will manage the new object passed (which must not be managed by any other ScopedHandle object). This method does not throw (assuming the destructor of a managed object does not throw).

Parameters

  ptr | NULL (the default), or a new unmanaged object to manage.

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

- shared_handle.h