1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*
* This file is part of unity-2d
*
* Copyright 2011 Canonical Ltd.
*
* Authors:
* - Aurélien Gâteau <aurelien.gateau@canonical.com>
*
* License: GPL v3
*/
#ifndef GSCOPEDPOINTER_H
#define GSCOPEDPOINTER_H
// Local
// Qt
#include <QScopedPointer>
// GLib
#include <glib-object.h>
/**
* Helper class for GScopedPointer
*/
template <class T, void (*cleanup_fcn)(T*)>
class GScopedPointerDeleter
{
public:
static void cleanup(T* ptr)
{
if (ptr) {
cleanup_fcn(ptr);
}
}
};
/**
* A GScopedPointer works like a QScopedPointer, except the cleanup static
* method is replaced with a cleanup function, making it useful to handle C
* structs whose cleanup function prototype is "void cleanup(T*)"
*
* Best way to use it is to define a typedef for your C struct, like this:
*
* typedef GScopedPointer<Foo, foo_free> GFooPointer;
*/
template <class T, void (*cleanup)(T*)>
class GScopedPointer : public QScopedPointer<T, GScopedPointerDeleter<T, cleanup> >
{
public:
GScopedPointer(T* ptr = 0)
: QScopedPointer<T, GScopedPointerDeleter<T, cleanup> >(ptr)
{}
};
/**
* Helper class for GObjectScopedPointer
*/
template <class T, void (*cleanup_fcn)(gpointer)>
class GObjectScopedPointerDeleter
{
public:
static void cleanup(T* ptr)
{
if (ptr) {
cleanup_fcn(ptr);
}
}
};
/**
* A GObjectScopedPointer is similar to a GScopedPointer. The only difference
* is its cleanup function signature is "void cleanup(gpointer)" and defaults to
* g_object_unref(), making it useful for GObject-based classes.
*
* You can use it directly like this:
*
* GObjectScopedPointer<GFoo> foo;
*
* Or define a typedef for your class:
*
* typedef GObjectScopedPointer<GFoo> GFooPointer;
*
* Note: GObjectScopedPointer does *not* call gobject_ref() when assigned a
* pointer.
*/
template <class T, void (*cleanup)(gpointer) = g_object_unref>
class GObjectScopedPointer : public QScopedPointer<T, GObjectScopedPointerDeleter<T, cleanup> >
{
public:
GObjectScopedPointer(T* ptr = 0)
: QScopedPointer<T, GObjectScopedPointerDeleter<T, cleanup> >(ptr)
{}
};
// A Generic GObject pointer
typedef GObjectScopedPointer<GObject> GObjectPointer;
// Take advantage of the cleanup signature of GObjectScopedPointer to define
// a GCharPointer
typedef GObjectScopedPointer<gchar, g_free> GCharPointer;
#endif /* GSCOPEDPOINTER_H */
|