c++-gtk-utils: Cgu::GSliceDestroy< T > Class Template Reference

c++-gtk-utils

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Cgu::GSliceDestroy< T > Class Template Reference
handles and smart pointers

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)

## Detailed Description

### template<class T>
class Cgu::GSliceDestroy< T >

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:

using namespace Cgu;
SharedHandle<MyClass*, GSliceDestroy<MyClass*> > h; // won't throw
{ // scope block for p variable
MyClass* p = g_slice_new(MyClass);
try {new(p) MyClass;} // MyClass constructor might throw
catch(...) {
g_slice_free(MyClass, p);
throw;
}
h.reset(p); // might throw but if so cleans up
}
...

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).

## Member Function Documentation

## ◆ operator()()

template<class T >

void Cgu::GSliceDestroy< T >::operator() | ( | T | obj | ) |
| inline

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

- shared_handle.h