2
* This file is part of unity-2d
4
* Copyright 2011 Canonical Ltd.
7
* - Aurélien Gâteau <aurelien.gateau@canonical.com>
11
#ifndef GSCOPEDPOINTER_H
12
#define GSCOPEDPOINTER_H
17
#include <QScopedPointer>
20
#include <glib-object.h>
23
* Helper class for GScopedPointer
25
template <class T, void (*cleanup_fcn)(T*)>
26
class GScopedPointerDeleter
29
static void cleanup(T* ptr)
38
* A GScopedPointer works like a QScopedPointer, except the cleanup static
39
* method is replaced with a cleanup function, making it useful to handle C
40
* structs whose cleanup function prototype is "void cleanup(T*)"
42
* Best way to use it is to define a typedef for your C struct, like this:
44
* typedef GScopedPointer<Foo, foo_free> GFooPointer;
46
template <class T, void (*cleanup)(T*)>
47
class GScopedPointer : public QScopedPointer<T, GScopedPointerDeleter<T, cleanup> >
50
GScopedPointer(T* ptr = 0)
51
: QScopedPointer<T, GScopedPointerDeleter<T, cleanup> >(ptr)
57
* Helper class for GObjectScopedPointer
59
template <class T, void (*cleanup_fcn)(gpointer)>
60
class GObjectScopedPointerDeleter
63
static void cleanup(T* ptr)
72
* A GObjectScopedPointer is similar to a GScopedPointer. The only difference
73
* is its cleanup function signature is "void cleanup(gpointer)" and defaults to
74
* g_object_unref(), making it useful for GObject-based classes.
76
* You can use it directly like this:
78
* GObjectScopedPointer<GFoo> foo;
80
* Or define a typedef for your class:
82
* typedef GObjectScopedPointer<GFoo> GFooPointer;
84
* Note: GObjectScopedPointer does *not* call gobject_ref() when assigned a
87
template <class T, void (*cleanup)(gpointer) = g_object_unref>
88
class GObjectScopedPointer : public QScopedPointer<T, GObjectScopedPointerDeleter<T, cleanup> >
91
GObjectScopedPointer(T* ptr = 0)
92
: QScopedPointer<T, GObjectScopedPointerDeleter<T, cleanup> >(ptr)
96
// A Generic GObject pointer
97
typedef GObjectScopedPointer<GObject> GObjectPointer;
99
// Take advantage of the cleanup signature of GObjectScopedPointer to define
101
typedef GObjectScopedPointer<gchar, g_free> GCharPointer;
103
#endif /* GSCOPEDPOINTER_H */