c++-gtk-utils
|
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle, SharedLockHandle or ScopedHandle template classes, which calls glib's g_slice_free1(), but before doing so also explicitly calls the destructor of a C++ object constructed in the memory. More...
#include <c++-gtk-utils/shared_handle.h>
Public Member Functions | |
void | operator() (T obj) |
A deleter functor for use as the second (Dealloc) template parameter of the SharedHandle, SharedLockHandle or ScopedHandle template classes, which calls glib's g_slice_free1(), but before doing so also explicitly calls the destructor of a C++ object constructed in the memory.
The managed memory block to be deleted by the GSliceDestroy functor must have the same size as the size of the object for which the functor is instantiated by pointer, as for example as allocated with the g_slice_new or g_slice_new0 macros (in other words, the GSliceDestroy template parameter must match the argument passed to those macros), and the memory block must have had that object constructed in it with the global placement new expression: see the example below. Sometimes it is more convenient to implement C++ objects in glib memory slices that way, rather than to have custom new and delete member operators of the classes concerned which use glib's g_slice_*(). However, a SharedHandle class with a GSliceDestroy deleter is not as easy to use as the SharedPtr class, as SharedHandle has no operator*() nor operator->() method (the get() method would have to be used to obtain the underlying pointer).
One consequence of the static sizing (and so typing) of memory slices is that a GSliceDestroy object instantiated for the management of a particular class must not be used by a SharedHandle, SharedLockHandle or ScopedHandle object which attempts to manage a class derived from it. This comes back to the point that the GSliceDestroy template parameter must match the argument passed to the g_slice_new or g_slice_new0 macros.
The type of the template argument for the functor is a pointer to the managed type: it is the same as the first template argument of the relevant SharedHandle, SharedLockHandle or ScopedHandle object. For example, to construct a SharedHandle managing an object of type MyClass to be constructed in a glib memory slice in an exception safe way:
The availability of this functor is not dependent on the library having been installed with the --with-glib-memory-slices-compat or --with-glib-memory-slices-no-compat configuration option (see Memory allocation for further details of those options).
Since 1.0.3
|
inline |