c++-gtk-utils
|
This is a generic class for managing the lifetime of objects allocated on freestore. More...
#include <c++-gtk-utils/shared_handle.h>
Public Member Functions | |
SharedHandle (T ptr=0) | |
SharedHandle (T ptr, Cgu::SharedHandleAllocFail::Leave tag) | |
void | reset (T ptr=0) |
void | reset (T ptr, Cgu::SharedHandleAllocFail::Leave tag) |
SharedHandle (const SharedHandle &sh_hand) | |
SharedHandle & | operator= (SharedHandle sh_hand) |
T | get () const |
operator T () const | |
unsigned int | get_refcount () const |
~SharedHandle () | |
This is a generic class for managing the lifetime of objects allocated on freestore.
The SharedHandle class is similar to the SharedPtr class (it keeps a reference count and deletes the handled object when the count reaches 0), but it does not have pointer semantics. Accordingly, it can be used to manage the memory of arrays and other objects allocated on the heap.
Because it is useful with arrays, by default it deallocates memory using C++ delete[]. However, if a SharedHandle object is passed a function object type as a second template argument when instantiated, it will use that function object to delete memory. This enables it to handle the memory of any object, such as objects to be deleted using std::free() or Glib's g_free(), g_list_free() or g_slice_free(). Instances (such as GcharScopedHandle, GcharSharedHandle, GerrorSharedHandle and GerrorScopedHandle) typdef'ed for particular deleters can conveniently manage objects of any kind.
To reflect the fact that it is just a handle for a pointer, it has different instantiation semantics from a SharedPtr object. A SharedPtr object is instantiated using this syntax:
A SharedHandle is instantiated using this syntax (note that the instantiated handle is for type T* and not T):
Apart from the operatorT() type conversion operator (which returns the underlying pointer), the only other method to obtain the underlying pointer is the get() method. If the object referenced is an array allocated on the heap, to use indexing you could either do this:
or this:
There is also a SharedLockHandle class, which has a thread-safe reference count, and a ScopedHandle class, which deletes its object as soon as it goes out of scope. A ScopedHandle class 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<>.
As of version 1.0.2, SharedHandle 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.
This library provides StandardArrayDelete, CFree, GFree, GerrorFree, GSliceFree, GSliceFreeSize and GSliceDestroy deleter functors, which can be used as the second template parameter of the SharedHandle class. As mentioned above, StandardArrayDelete is the default, and some typedef'ed instances of SharedHandle for gchar (with the GFree deleter) and for GError (with the GerrorFree deleter) are provided.
From version 1.2.12, the library provides ==, != and < comparison operators for SharedHandles, but only if the library is compiled with the --with-smart-ptr-comp option, or if the user code defines the symbol CGU_USE_SMART_PTR_COMPARISON before shared_handle.h is first parsed. This is because, if user code has provided such operators for these smart pointers itself, a duplicated function definition would arise.
If the library is compiled with the --with-glib-memory-slices-no-compat configuration option, Cgu::SharedHandle constructs its reference counting internals using glib memory slices. Although it is safe in a multi-threaded program if glib < 2.32 is installed to construct a static SharedHandle object in global namespace (that is, prior to g_thread_init() being called) by means of the default constructor and/or a pointer argument of NULL, it is not safe if constructed with a non-NULL pointer value. If glib >= 2.32 is installed, global objects with memory slices are safe in all circumstances. (Having said that, it would be highly unusual to have global SharedHandle objects.)
|
inlineexplicit |
Constructor taking an unmanaged object.
ptr | The object which the SharedHandle is to manage (if any). |
std::bad_alloc | This constructor will not throw if the 'ptr' argument has a NULL value (the default), otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. If such an exception is thrown, this constructor is exception safe (it does not leak resources), but as well as cleaning itself up this constructor will also delete the managed object passed to it to avoid a memory leak. If such automatic deletion is not wanted in that case, use the version of this constructor taking a Cgu::SharedHandleAllocFail::Leave tag argument. |
Since 1.0.2, SharedHandle 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.
|
inline |
Constructor taking an unmanaged object.
ptr | The object which the SharedHandle is to manage |
tag | Passing the tag emumerator Cgu::SharedHandleAllocFail::leave causes this constructor not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedHandleError will be thrown). |
Cgu::SharedHandleError | This constructor might throw Cgu::SharedHandleError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. This constructor is exception safe (it does not leak resources), and if such an exception is thrown it will clean itself up, but it will not attempt to delete the new managed object passed to it. Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedHandleError object. |
Since 0.9.1 (first version in which this constructor available).
Since 1.0.2, SharedHandle 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.
|
inline |
The copy constructor does not throw.
sh_hand | The handle to be copied. |
|
inline |
The destructor does not throw unless the destructor of a handled object throws - that should never happen.
|
inline |
This method does not throw.
|
inline |
This method does not throw.
|
inline |
This method does not throw.
|
inline |
This method does not throw unless the destructor of a handled object throws.
sh_hand | the assignor. |
|
inline |
Causes the SharedHandle to cease to manage its managed object (if any), deleting it if this is the last SharedHandle object managing it. The SharedHandle object will manage the new object passed (which must not be managed by any other SharedHandle object). This method is exception safe, but see the comments below on Cgu::SharedHandleError.
ptr | A new unmanaged object to manage (if no new object is to be managed, use the version of reset() taking a default value of NULL). |
tag | Passing the tag emumerator Cgu::SharedHandleAllocFail::leave causes this method not to delete the new managed object passed as the 'ptr' argument in the event of internal allocation in this method failing because of memory exhaustion (in that event, Cgu::SharedHandleError will be thrown). |
Cgu::SharedHandleError | This method might throw Cgu::SharedHandleError if memory is exhausted and the system would otherwise throw std::bad_alloc in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), and it will not attempt to delete the new managed object passed to it. Access to the object passed to the 'ptr' argument can be obtained via the thrown Cgu::SharedHandleError object. |
Since 0.9.1
|
inline |
Causes the SharedHandle to cease to manage its managed object (if any), deleting it if this is the last SharedHandle object managing it. If the argument passed is not NULL, the SharedHandle object will manage the new object passed (which must not be managed by any other SharedHandle object). This method is exception safe, but see the comments below on std::bad_alloc.
ptr | NULL (the default), or a new unmanaged object to manage. |
std::bad_alloc | This method will not throw if the 'ptr' argument has a NULL value (the default) and the destructor of a managed object does not throw, otherwise it might throw std::bad_alloc if memory is exhausted and the system throws in that case. Note that if such an exception is thrown then this method will do nothing (it is strongly exception safe and will continue to manage the object it was managing prior to the call), except that it will delete the new managed object passed to it to avoid a memory leak. If such automatic deletion in the event of such an exception is not wanted, use the reset() method taking a Cgu::SharedHandleAllocFail::Leave tag type as its second argument. |
Since 0.9.1