Debian Codegraph @

src/dex-async-pair.c

raw symbols SCIP
1 /*
2 * dex-async-pair.c
3 *
4 * Copyright 2022-2023 Christian Hergert <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25
26 #include "dex-async-pair-private.h"
27 #include "dex-error.h"
28
29 DEX_DEFINE_FINAL_TYPE (DexAsyncPair, dex_async_pair, DEX_TYPE_FUTURE)
30
31 #undef DEX_TYPE_ASYNC_PAIR
32 #define DEX_TYPE_ASYNC_PAIR dex_async_pair_type
33
34 static void
35 dex_async_pair_discard (DexFuture *future)
36 {
37 DexAsyncPair *async_pair = DEX_ASYNC_PAIR (future);
38
39 if (async_pair->cancel_on_discard)
40 g_cancellable_cancel (async_pair->cancellable);
41 }
42
43 static void
44 dex_async_pair_finalize (DexObject *object)
45 {
46 DexAsyncPair *async_pair = DEX_ASYNC_PAIR (object);
47
48 g_clear_object (&async_pair->instance);
49 g_clear_object (&async_pair->cancellable);
50 g_clear_pointer (&async_pair->info, g_free);
51
52 DEX_OBJECT_CLASS (dex_async_pair_parent_class)->finalize (object);
53 }
54
55 static void
56 dex_async_pair_class_init (DexAsyncPairClass *async_pair_class)
57 {
58 DexObjectClass *object_class = DEX_OBJECT_CLASS (async_pair_class);
59 DexFutureClass *future_class = DEX_FUTURE_CLASS (async_pair_class);
60
61 object_class->finalize = dex_async_pair_finalize;
62
63 future_class->discard = dex_async_pair_discard;
64 }
65
66 static void
67 dex_async_pair_init (DexAsyncPair *async_pair)
68 {
69 async_pair->cancellable = g_cancellable_new ();
70 async_pair->cancel_on_discard = TRUE;
71 }
72
73 static void
74 dex_async_pair_ready_callback (GObject *object,
75 GAsyncResult *result,
76 gpointer user_data)
77 {
78 DexAsyncPair *async_pair = user_data;
79 GValue value = G_VALUE_INIT;
80 GError *error = NULL;
81 GType gtype;
82
83 g_assert (G_IS_OBJECT (object));
84 g_assert (G_IS_ASYNC_RESULT (result));
85 g_assert (DEX_IS_ASYNC_PAIR (async_pair));
86
87 if (g_cancellable_is_cancelled (async_pair->cancellable))
88 {
89 error = g_error_new_literal (G_IO_ERROR,
90 G_IO_ERROR_CANCELLED,
91 "Operation cancelled");
92 goto complete;
93 }
94
95 #define FINISH_AS(ap, TYPE) \
96 (((TYPE (*) (gpointer, GAsyncResult*, GError**))ap->info->finish) (ap->instance, result, &error))
97
98 gtype = async_pair->info->return_type;
99
100 switch (gtype)
101 {
102 case G_TYPE_BOOLEAN:
103 g_value_init (&value, G_TYPE_BOOLEAN);
104 g_value_set_boolean (&value, FINISH_AS (async_pair, gboolean));
105 break;
106
107 case G_TYPE_INT:
108 g_value_init (&value, G_TYPE_INT);
109 g_value_set_int (&value, FINISH_AS (async_pair, int));
110 break;
111
112 case G_TYPE_UINT:
113 g_value_init (&value, G_TYPE_UINT);
114 g_value_set_uint (&value, FINISH_AS (async_pair, guint));
115 break;
116
117 case G_TYPE_INT64:
118 g_value_init (&value, G_TYPE_INT64);
119 g_value_set_int64 (&value, FINISH_AS (async_pair, gint64));
120 break;
121
122 case G_TYPE_UINT64:
123 g_value_init (&value, G_TYPE_UINT64);
124 g_value_set_uint64 (&value, FINISH_AS (async_pair, guint64));
125 break;
126
127 case G_TYPE_LONG:
128 g_value_init (&value, G_TYPE_LONG);
129 g_value_set_long (&value, FINISH_AS (async_pair, glong));
130 break;
131
132 case G_TYPE_ULONG:
133 g_value_init (&value, G_TYPE_ULONG);
134 g_value_set_ulong (&value, FINISH_AS (async_pair, gulong));
135 break;
136
137 case G_TYPE_STRING:
138 g_value_init (&value, G_TYPE_STRING);
139 g_value_take_string (&value, FINISH_AS (async_pair, char *));
140 break;
141
142 case G_TYPE_POINTER:
143 g_value_init (&value, G_TYPE_POINTER);
144 g_value_set_pointer (&value, FINISH_AS (async_pair, gpointer));
145 break;
146
147 case G_TYPE_OBJECT:
148 g_value_init (&value, G_TYPE_OBJECT);
149 g_value_take_object (&value, FINISH_AS (async_pair, gpointer));
150 break;
151
152 case G_TYPE_ENUM:
153 case G_TYPE_FLAGS:
154 default:
155 if (gtype == G_TYPE_ENUM || g_type_is_a (gtype, G_TYPE_ENUM))
156 {
157 g_value_init (&value, gtype);
158 g_value_set_enum (&value, FINISH_AS (async_pair, guint));
159 }
160 else if (gtype == G_TYPE_FLAGS || g_type_is_a (gtype, G_TYPE_FLAGS))
161 {
162 g_value_init (&value, gtype);
163 g_value_set_flags (&value, FINISH_AS (async_pair, guint));
164 }
165 else if (g_type_is_a (gtype, G_TYPE_BOXED))
166 {
167 g_value_init (&value, gtype);
168 g_value_take_boxed (&value, FINISH_AS (async_pair, gpointer));
169 }
170 else
171 {
172 error = g_error_new (DEX_ERROR,
173 DEX_ERROR_TYPE_NOT_SUPPORTED,
174 "Type '%s' is not currently supported by DexAsyncPair!",
175 g_type_name (async_pair->info->return_type));
176 }
177 break;
178 }
179
180 #undef FINISH_AS
181
182 complete:
183 if (error != NULL)
184 dex_future_complete (DEX_FUTURE (async_pair), NULL, g_steal_pointer (&error));
185 else
186 dex_future_complete (DEX_FUTURE (async_pair), &value, NULL);
187
188 g_value_unset (&value);
189 dex_clear (&async_pair);
190 }
191
192 DexFuture *
193 dex_async_pair_new (gpointer instance,
194 const DexAsyncPairInfo *info)
195 {
196 void (*async_func) (gpointer, GCancellable*, GAsyncReadyCallback, gpointer);
197 DexAsyncPair *async_pair;
198
199 g_return_val_if_fail (!instance || G_IS_OBJECT (instance), NULL);
200 g_return_val_if_fail (info != NULL, NULL);
201
202 async_func = info->async;
203
204 async_pair = (DexAsyncPair *)dex_object_create_instance (DEX_TYPE_ASYNC_PAIR);
205 async_pair->info = g_memdup2 (info, sizeof *info);
206 g_set_object (&async_pair->instance, instance);
207
208 async_func (async_pair->instance,
209 async_pair->cancellable,
210 dex_async_pair_ready_callback,
211 dex_ref (async_pair));
212
213 return DEX_FUTURE (async_pair);
214 }
215
216 /**
217 * dex_async_pair_get_cancellable:
218 * @async_pair: a [[email protected]]
219 *
220 * Gets the cancellable for the async pair.
221 *
222 * If the [[email protected]] is discarded by its callers, then it will
223 * automatically be cancelled using [[email protected]].
224 *
225 * Returns: (transfer none): the [[email protected]] associated with the
226 * @async_pair
227 */
228 GCancellable *
229 dex_async_pair_get_cancellable (DexAsyncPair *async_pair)
230 {
231 g_return_val_if_fail (DEX_IS_ASYNC_PAIR (async_pair), NULL);
232
233 return async_pair->cancellable;
234 }
235
236 /**
237 * dex_async_pair_return_object:
238 * @async_pair: a [[email protected]]
239 * @instance: (type GObject) (transfer full): a [[email protected]]
240 *
241 * Resolves @async_pair with a value of @instance.
242 *
243 * This function is meant to be used when manually wrapping
244 * various [[email protected]] based API.
245 *
246 * The ownership of @instance is taken when calling this function.
247 */
248 void
249 dex_async_pair_return_object (DexAsyncPair *async_pair,
250 gpointer instance)
251 {
252 GValue value = G_VALUE_INIT;
253
254 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
255 g_return_if_fail (G_IS_OBJECT (instance));
256
257 g_value_init (&value, G_OBJECT_TYPE (instance));
258 g_value_take_object (&value, instance);
259 dex_future_complete (DEX_FUTURE (async_pair), &value, NULL);
260 g_value_unset (&value);
261 }
262
263 /**
264 * dex_async_pair_return_error:
265 * @async_pair: a [[email protected]]
266 * @error: (transfer full): a [[email protected]]
267 *
268 * Rejects @async_pair with @error.
269 *
270 * This function is meant to be used when manually wrapping
271 * various [[email protected]] based API.
272 *
273 * The ownership of @error is taken when calling this function.
274 */
275 void
276 dex_async_pair_return_error (DexAsyncPair *async_pair,
277 GError *error)
278 {
279 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
280 g_return_if_fail (error != NULL);
281
282 dex_future_complete (DEX_FUTURE (async_pair), NULL, error);
283 }
284
285 void
286 dex_async_pair_return_int64 (DexAsyncPair *async_pair,
287 gint64 value)
288 {
289 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
290
291 dex_future_complete (DEX_FUTURE (async_pair), &(GValue) {G_TYPE_INT64, {{.v_int64 = value}}}, NULL);
292 }
293
294 void
295 dex_async_pair_return_uint64 (DexAsyncPair *async_pair,
296 guint64 value)
297 {
298 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
299
300 dex_future_complete (DEX_FUTURE (async_pair), &(GValue) {G_TYPE_UINT64, {{.v_uint64 = value}}}, NULL);
301 }
302
303 void
304 dex_async_pair_return_boolean (DexAsyncPair *async_pair,
305 gboolean value)
306 {
307 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
308
309 dex_future_complete (DEX_FUTURE (async_pair), &(GValue) {G_TYPE_BOOLEAN, {{.v_int = value}}}, NULL);
310 }
311
312 /**
313 * dex_async_pair_return_string:
314 * @async_pair: a [[email protected]]
315 * @value: (transfer full) (nullable): a string or %NULL
316 *
317 * Resolves @async_pair with @value.
318 */
319 void
320 dex_async_pair_return_string (DexAsyncPair *async_pair,
321 char *value)
322 {
323 GValue gvalue = G_VALUE_INIT;
324
325 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
326
327 g_value_init (&gvalue, G_TYPE_STRING);
328 g_value_take_string (&gvalue, value);
329 dex_future_complete (DEX_FUTURE (async_pair), &gvalue, NULL);
330 g_value_unset (&gvalue);
331 }
332
333 /**
334 * dex_async_pair_return_boxed: (skip)
335 * @async_pair: a [[email protected]]
336 * @boxed_type: a `GType` of %G_TYPE_BOXED
337 * @instance: (transfer full): the boxed value to store
338 *
339 * Resolves @async_pair with @instance.
340 */
341 void
342 dex_async_pair_return_boxed (DexAsyncPair *async_pair,
343 GType boxed_type,
344 gpointer instance)
345 {
346 GValue gvalue = G_VALUE_INIT;
347
348 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
349 g_return_if_fail (g_type_is_a (boxed_type, G_TYPE_BOXED));
350
351 g_value_init (&gvalue, boxed_type);
352 g_value_take_boxed (&gvalue, instance);
353 dex_future_complete (DEX_FUTURE (async_pair), &gvalue, NULL);
354 g_value_unset (&gvalue);
355 }
356
357 /**
358 * dex_async_pair_return_variant:
359 * @async_pair: a [[email protected]]
360 * @variant: (transfer full): the [[email protected]] to resolve with
361 *
362 * Resolves @async_pair with @variant.
363 */
364 void
365 dex_async_pair_return_variant (DexAsyncPair *async_pair,
366 GVariant *variant)
367 {
368 GValue gvalue = G_VALUE_INIT;
369
370 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
371
372 g_value_init (&gvalue, G_TYPE_VARIANT);
373 g_value_take_variant (&gvalue, variant);
374 dex_future_complete (DEX_FUTURE (async_pair), &gvalue, NULL);
375 g_value_unset (&gvalue);
376 }
377
378 /**
379 * dex_async_pair_set_cancel_on_discard:
380 * @async_pair: a [[email protected]]
381 * @cancel_on_discard: if the operation should cancel when the future is discarded
382 *
383 * Sets whether or not the future should cancel the async operation when
384 * the future is discarded. This happens when no more futures are awaiting
385 * the completion of this future.
386 *
387 * Since: 0.4
388 */
389 void
390 dex_async_pair_set_cancel_on_discard (DexAsyncPair *async_pair,
391 gboolean cancel_on_discard)
392 {
393 g_return_if_fail (DEX_IS_ASYNC_PAIR (async_pair));
394
395 dex_object_lock (async_pair);
396 async_pair->cancel_on_discard = !!cancel_on_discard;
397 dex_object_unlock (async_pair);
398 }