blob: e86697c74344879afb230d02fc4823724c1265bf (
plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/** \file
* Inkscape::Lifecycle - automatic object lifecycle management
*
* Copyright 2008 MenTaLguY <mental@rydia.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* See the file COPYING for details.
*
*/
#ifndef SEEN_INKSCAPE_LIFECYCLE_H
#define SEEN_INKSCAPE_LIFECYCLE_H
#include <glib/gtypes.h>
#include <glib/gatomic.h>
namespace Inkscape {
namespace Lifecycle {
class BaseManagedPtr;
// Abstract base class for managed objects
class AbstractManaged {
public:
AbstractManaged() {}
virtual ~AbstractManaged() {}
protected:
virtual void managedAddReference() const=0;
virtual void managedRemoveReference() const=0;
friend class BaseManagedPtr;
private:
AbstractManaged(AbstractManaged &); // no copy
void operator=(AbstractManaged &); // no assign
};
// Base class for managed pointers; not intended for direct use
class BaseManagedPtr {
protected:
BaseManagedPtr() : ptr(NULL) {}
BaseManagedPtr(AbstractManaged const *managed) : ptr(NULL) {
set(managed);
}
BaseManagedPtr(BaseManagedPtr const &other) : ptr(NULL) {
set(other.ptr);
}
AbstractManaged const *get() const { return ptr; }
void set(AbstractManaged const *managed) {
if (managed) {
ptr->managedAddReference();
}
if (ptr) {
ptr->managedRemoveReference();
}
ptr = managed;
}
private:
AbstractManaged const *ptr;
};
// Type-safe managed pointer
template <typename T>
class ManagedPtr : public BaseManagedPtr {
public:
ManagedPtr() : BaseManagedPtr() {}
ManagedPtr(T *managed) : BaseManagedPtr(managed) {}
ManagedPtr(ManagedPtr const &other) : BaseManagedPtr(other) {}
operator T *() const { return get_recast(); }
T &operator*() const { return *get_recast(); }
T *operator->() const { return get_recast(); }
ManagedPtr &operator=(T *managed) {
set(managed);
return *this;
}
ManagedPtr &operator=(ManagedPtr const &other) {
set(other.get());
return *this;
}
private:
T *get_recast() const {
return const_cast<T *>(static_cast<T const *>(get()));
}
};
// Wrapper for a simple pointer emulating the ManagedPtr interface
template <typename T>
class DumbPtr {
public:
DumbPtr() : ptr(NULL) {}
DumbPtr(T *p) : ptr(p) {}
DumbPtr(DumbPtr const &other) : ptr(other.ptr) {}
operator T *() const { return ptr; }
T &operator*() const { return *ptr; }
T *operator->() const { return ptr; }
DumbPtr &operator=(T *p) {
ptr = p;
return *this;
}
DumbPtr &operator=(DumbPtr const &other) {
ptr = other.ptr;
return *this;
}
private:
T *ptr;
};
// Dummy implementation of AbstractManaged to ease migration
class DummyManaged : public virtual AbstractManaged {
protected:
virtual void managedAddReference() const {}
virtual void managedRemoveReference() const {}
};
// Straightforward refcounting implementation of AbstractManaged
class SimpleManaged : public virtual AbstractManaged {
protected:
SimpleManaged() : refcount(0) {}
virtual ~SimpleManaged() {}
virtual void managedAddReference() const {
g_atomic_int_inc(&refcount);
}
virtual void managedRemoveReference() const {
if (g_atomic_int_dec_and_test(&refcount)) {
const_cast<SimpleManaged *>(this)->managedDispose();
}
}
virtual void managedDispose() {
delete this;
}
private:
mutable volatile gint refcount;
};
}
}
#endif
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|