-
Notifications
You must be signed in to change notification settings - Fork 3
/
g_ptr.h
191 lines (154 loc) · 3.46 KB
/
g_ptr.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Smart pointer for GLib
*
* Inspired by
* https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/glib/GRefPtr.h
* and https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/glib/GUniquePtr.h
*/
#pragma once
#include <memory>
#include <glib.h>
#include <libsoup/soup.h>
template<typename T>
inline T* g_ptr_ref(T* ptr) {
if (ptr) {
g_ptr_ref_impl(ptr);
}
return ptr;
}
template<typename T>
inline void g_ptr_unref(T* ptr) {
if (ptr) {
g_ptr_unref_impl(ptr);
}
}
template<typename T>
inline void g_ptr_ref_impl(T* ptr) {
g_object_ref_sink(ptr);
}
template<typename T>
inline void g_ptr_unref_impl(T* ptr) {
g_object_unref(ptr);
}
template<>
inline void g_ptr_ref_impl(GBytes* ptr) {
g_bytes_ref(ptr);
}
template<>
inline void g_ptr_unref_impl(GBytes* ptr) {
g_bytes_unref(ptr);
}
template<>
inline void g_ptr_ref_impl(SoupBuffer* ptr) {
soup_buffer_copy(ptr);
}
template<>
inline void g_ptr_unref_impl(SoupBuffer* ptr) {
soup_buffer_free(ptr);
}
template<>
inline void g_ptr_ref_impl(GThread* ptr) {
g_thread_ref(ptr);
}
template<>
inline void g_ptr_unref_impl(GThread* ptr) {
g_thread_unref(ptr);
}
template<>
inline void g_ptr_ref_impl(GMainContext* ptr) {
g_main_context_ref(ptr);
}
template<>
inline void g_ptr_unref_impl(GMainContext* ptr) {
g_main_context_unref(ptr);
}
template<typename T>
struct take_t {
T* ptr;
take_t(T* ptr)
: ptr(ptr) {}
template<typename U>
take_t(U* ptr)
: ptr(reinterpret_cast<T*>(ptr)) {}
};
template<typename T>
struct g_ptr_t {
struct deleter {
void operator()(T* ptr) const {
g_ptr_unref(ptr);
}
};
g_ptr_t() = default;
g_ptr_t(g_ptr_t<T>&&) = default;
g_ptr_t<T>& operator=(g_ptr_t<T>&& other) = default;
g_ptr_t(const g_ptr_t<T>& other)
: g_ptr_t(other.as()) {}
g_ptr_t<T>& operator=(const g_ptr_t<T>& other) {
return operator=(other.as());
}
g_ptr_t(T* ptr)
: wrapped_ptr(g_ptr_ref(ptr)) {}
g_ptr_t<T>& operator=(T* ptr) {
wrapped_ptr.reset(g_ptr_ref(ptr));
return *this;
}
g_ptr_t(take_t<T> taken)
: wrapped_ptr(taken.ptr) {}
g_ptr_t<T>& operator=(take_t<T> taken) {
wrapped_ptr.reset(taken.ptr);
return *this;
}
template<typename U = T>
U* as() const {
return reinterpret_cast<U*>(wrapped_ptr.get());
}
[[gnu::warn_unused_result]] T* release() {
return wrapped_ptr.release();
}
T*
operator->() const noexcept {
return as();
}
explicit operator bool() const noexcept {
return !!wrapped_ptr;
}
operator T*() const noexcept {
return as();
}
private:
std::unique_ptr<T, deleter> wrapped_ptr;
};
template<typename T>
auto take(T* ptr) {
return g_ptr_t{take_t{ptr}};
}
template<typename T>
inline void g_delete_func(T* ptr) {
g_free(ptr);
}
template<>
inline void g_delete_func(GError* ptr) {
g_error_free(ptr);
}
template<>
inline void g_delete_func(char** ptr) {
g_strfreev(ptr);
}
template<>
inline void g_delete_func(SoupURI* ptr) {
soup_uri_free(ptr);
}
template<typename T>
struct g_ptr_deleter {
void operator()(T* ptr) const {
if (ptr) {
g_delete_func(ptr);
}
}
};
template<typename T>
using g_unique_ptr_t = std::unique_ptr<T, g_ptr_deleter<T>>;
template<typename T>
auto g_unique_ptr_take(T* ptr) {
return g_unique_ptr_t<T>{ptr};
}