c++-gtk-utils
Public Member Functions | List of all members
Cgu::ScopedHandle< T, Dealloc > Class Template Reference

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 (T ptr=0)
 
void reset (T ptr=0)
 
release ()
 
get () const
 
 operator T () const
 
 ~ScopedHandle ()
 

Detailed Description

template<class T, class Dealloc = StandardArrayDelete<T>>
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. It is used where, if you wanted pointer semantics, you might use a const std::auto_ptr<>, except that it has a reset() method.

As of version 1.0.2, ScopedHandle objects can be instantiated for pointers to constant objects (such as ScopedHandle<const char*>), provided the deleter functor will take such pointers. Prior to version 1.0.2, it could only manage pointers to non-const objects.

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)

Constructor & Destructor Documentation

template<class T, class Dealloc = StandardArrayDelete<T>>
Cgu::ScopedHandle< T, Dealloc >::ScopedHandle ( ptr = 0)
inlineexplicit

The constructor does not throw.

Parameters
ptrThe object which the ScopedHandle is to manage (if any).

Since 1.0.2, ScopedHandle objects can be instantiated for pointers to constant objects (such as SharedHandle<const char*>), provided the deleter functor will take such pointers. Prior to version 1.0.2, it could only manage pointers to non-const objects.

template<class T, class Dealloc = StandardArrayDelete<T>>
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 = StandardArrayDelete<T>>
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 = StandardArrayDelete<T>>
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 = StandardArrayDelete<T>>
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).

Since 1.0.2

template<class T, class Dealloc = StandardArrayDelete<T>>
void Cgu::ScopedHandle< T, Dealloc >::reset ( 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
ptrNULL (the default), or a new unmanaged object to manage.

Since 0.9.1


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