~unity-2d-team/unity-2d/maverick

« back to all changes in this revision

Viewing changes to libunity-2d-private/src/gscopedpointer.h

  • Committer: Florian Boucault
  • Date: 2011-04-22 15:01:24 UTC
  • Revision ID: florian@boucault.net-20110422150124-72jeot6nh3k1bv0b
[core] Introduces GScopedPointer and GObjectScopedPointer, two smart pointers which makes it easier to deal with C structures without forgetting to release them.

Show diffs side-by-side

added added

removed removed

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