Debian Codegraph @

src/dex-future.h

raw symbols SCIP
1 /*
2 * dex-future.h
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 #pragma once
23
24 #if !defined (DEX_INSIDE) && !defined (DEX_COMPILATION)
25 # error "Only <libdex.h> can be included directly."
26 #endif
27
28 #include "dex-enums.h"
29 #include "dex-object.h"
30
31 G_BEGIN_DECLS
32
33 #define DEX_TYPE_FUTURE (dex_future_get_type())
34 #define DEX_FUTURE(object) (G_TYPE_CHECK_INSTANCE_CAST(object, DEX_TYPE_FUTURE, DexFuture))
35 #define DEX_IS_FUTURE(object) (G_TYPE_CHECK_INSTANCE_TYPE(object, DEX_TYPE_FUTURE))
36
37 typedef struct _DexFuture DexFuture;
38
39 /**
40 * DexFutureCallback:
41 * @future: a resolved or rejected [[email protected]]
42 * @user_data: closure data associated with the callback
43 *
44 * A `DexFutureCallback` can be executed from a [[email protected]] as response to
45 * another [[email protected]] resolving or rejecting.
46 *
47 * The callback will be executed within the scheduler environment the block is
48 * created within when using [[email protected]], [[email protected]]
49 * [[email protected]], [[email protected]], and similar functions.
50 *
51 * This is the expected way to handle completion of a future when not using
52 * [[email protected]] via [[email protected]].
53 *
54 * Returns: (transfer full) (nullable): a [[email protected]] or %NULL
55 */
56 typedef DexFuture *(*DexFutureCallback) (DexFuture *future,
57 gpointer user_data);
58
59 DEX_AVAILABLE_IN_ALL
60 GType dex_future_get_type (void);
61 DEX_AVAILABLE_IN_ALL
62 DexFutureStatus dex_future_get_status (DexFuture *future);
63 DEX_AVAILABLE_IN_ALL
64 gboolean dex_future_is_resolved (DexFuture *future);
65 DEX_AVAILABLE_IN_ALL
66 gboolean dex_future_is_rejected (DexFuture *future);
67 DEX_AVAILABLE_IN_ALL
68 gboolean dex_future_is_pending (DexFuture *future);
69 DEX_AVAILABLE_IN_ALL
70 const GValue *dex_future_get_value (DexFuture *future,
71 GError **error);
72 DEX_AVAILABLE_IN_ALL
73 DexFuture *dex_future_new_for_value (const GValue *value) G_GNUC_WARN_UNUSED_RESULT;
74 DEX_AVAILABLE_IN_ALL
75 DexFuture *dex_future_new_for_error (GError *error) G_GNUC_WARN_UNUSED_RESULT;
76 DEX_AVAILABLE_IN_ALL
77 DexFuture *dex_future_new_infinite (void) G_GNUC_WARN_UNUSED_RESULT;
78 DEX_AVAILABLE_IN_ALL
79 DexFuture *dex_future_new_reject (GQuark domain,
80 int error_code,
81 const char *format,
82 ...) G_GNUC_PRINTF (3, 4) G_GNUC_WARN_UNUSED_RESULT;
83 DEX_AVAILABLE_IN_ALL
84 DexFuture *dex_future_new_for_errno (int errno_) G_GNUC_WARN_UNUSED_RESULT;
85 DEX_AVAILABLE_IN_ALL
86 DexFuture *dex_future_new_for_string (const char *string) G_GNUC_WARN_UNUSED_RESULT;
87 DEX_AVAILABLE_IN_ALL
88 DexFuture *dex_future_new_take_string (char *string) G_GNUC_WARN_UNUSED_RESULT;
89 DEX_AVAILABLE_IN_ALL
90 DexFuture *dex_future_new_for_fd (int fd) G_GNUC_WARN_UNUSED_RESULT;
91 DEX_AVAILABLE_IN_ALL
92 DexFuture *dex_future_new_for_int (int v_int) G_GNUC_WARN_UNUSED_RESULT;
93 DEX_AVAILABLE_IN_ALL
94 DexFuture *dex_future_new_for_uint (guint v_uint) G_GNUC_WARN_UNUSED_RESULT;
95 DEX_AVAILABLE_IN_ALL
96 DexFuture *dex_future_new_enum (GType enum_type,
97 guint enum_value) G_GNUC_WARN_UNUSED_RESULT;
98 DEX_AVAILABLE_IN_ALL
99 DexFuture *dex_future_new_for_int64 (gint64 v_int64) G_GNUC_WARN_UNUSED_RESULT;
100 DEX_AVAILABLE_IN_ALL
101 DexFuture *dex_future_new_for_uint64 (guint64 v_uint64) G_GNUC_WARN_UNUSED_RESULT;
102 DEX_AVAILABLE_IN_ALL
103 DexFuture *dex_future_new_for_boolean (gboolean v_bool) G_GNUC_WARN_UNUSED_RESULT;
104 DEX_AVAILABLE_IN_ALL
105 DexFuture *dex_future_new_for_double (double v_double) G_GNUC_WARN_UNUSED_RESULT;
106 DEX_AVAILABLE_IN_ALL
107 DexFuture *dex_future_new_for_float (float v_float) G_GNUC_WARN_UNUSED_RESULT;
108 DEX_AVAILABLE_IN_ALL
109 DexFuture *dex_future_new_take_variant (GVariant *v_variant) G_GNUC_WARN_UNUSED_RESULT;
110 DEX_AVAILABLE_IN_ALL
111 DexFuture *dex_future_new_take_boxed (GType boxed_type,
112 gpointer value) G_GNUC_WARN_UNUSED_RESULT;
113 DEX_AVAILABLE_IN_ALL
114 DexFuture *dex_future_new_for_pointer (gpointer pointer) G_GNUC_WARN_UNUSED_RESULT;
115 DEX_AVAILABLE_IN_ALL
116 DexFuture *dex_future_new_for_object (gpointer value) G_GNUC_WARN_UNUSED_RESULT;
117 DEX_AVAILABLE_IN_ALL
118 DexFuture *dex_future_new_take_object (gpointer value) G_GNUC_WARN_UNUSED_RESULT;
119 DEX_AVAILABLE_IN_ALL
120 DexFuture *dex_future_then (DexFuture *future,
121 DexFutureCallback callback,
122 gpointer callback_data,
123 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
124 DEX_AVAILABLE_IN_ALL
125 DexFuture *dex_future_then_loop (DexFuture *future,
126 DexFutureCallback callback,
127 gpointer callback_data,
128 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
129 DEX_AVAILABLE_IN_ALL
130 DexFuture *dex_future_catch (DexFuture *future,
131 DexFutureCallback callback,
132 gpointer callback_data,
133 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
134 DEX_AVAILABLE_IN_ALL
135 DexFuture *dex_future_catch_loop (DexFuture *future,
136 DexFutureCallback callback,
137 gpointer callback_data,
138 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
139 DEX_AVAILABLE_IN_ALL
140 DexFuture *dex_future_finally (DexFuture *future,
141 DexFutureCallback callback,
142 gpointer callback_data,
143 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
144 DEX_AVAILABLE_IN_ALL
145 DexFuture *dex_future_finally_loop (DexFuture *future,
146 DexFutureCallback callback,
147 gpointer callback_data,
148 GDestroyNotify callback_data_destroy) G_GNUC_WARN_UNUSED_RESULT;
149 DEX_AVAILABLE_IN_ALL
150 DexFuture *dex_future_all (DexFuture *first_future,
151 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
152 DEX_AVAILABLE_IN_ALL
153 DexFuture *dex_future_allv (DexFuture * const *futures,
154 guint n_futures) G_GNUC_WARN_UNUSED_RESULT;
155 DEX_AVAILABLE_IN_ALL
156 DexFuture *dex_future_all_race (DexFuture *first_future,
157 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
158 DEX_AVAILABLE_IN_ALL
159 DexFuture *dex_future_all_racev (DexFuture * const *futures,
160 guint n_futures) G_GNUC_WARN_UNUSED_RESULT;
161 DEX_AVAILABLE_IN_ALL
162 DexFuture *dex_future_any (DexFuture *first_future,
163 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
164 DEX_AVAILABLE_IN_ALL
165 DexFuture *dex_future_anyv (DexFuture * const *futures,
166 guint n_futures) G_GNUC_WARN_UNUSED_RESULT;
167 DEX_AVAILABLE_IN_ALL
168 DexFuture *dex_future_first (DexFuture *first_future,
169 ...) G_GNUC_NULL_TERMINATED G_GNUC_WARN_UNUSED_RESULT;
170 DEX_AVAILABLE_IN_ALL
171 DexFuture *dex_future_firstv (DexFuture * const *futures,
172 guint n_futures) G_GNUC_WARN_UNUSED_RESULT;
173 DEX_AVAILABLE_IN_1_2
174 DexFuture *dex_future_with_timeout_seconds (DexFuture *future,
175 int seconds) G_GNUC_WARN_UNUSED_RESULT;
176 DEX_AVAILABLE_IN_1_2
177 DexFuture *dex_future_with_timeout_msec (DexFuture *future,
178 int msec) G_GNUC_WARN_UNUSED_RESULT;
179 DEX_AVAILABLE_IN_1_2
180 DexFuture *dex_future_with_timeout (DexFuture *future,
181 gint64 usec) G_GNUC_WARN_UNUSED_RESULT;
182 DEX_AVAILABLE_IN_1_2
183 DexFuture *dex_future_with_deadline (DexFuture *future,
184 gint64 deadline) G_GNUC_WARN_UNUSED_RESULT;
185 DEX_AVAILABLE_IN_ALL
186 const char *dex_future_get_name (DexFuture *future);
187 DEX_AVAILABLE_IN_ALL
188 void dex_future_set_static_name (DexFuture *future,
189 const char *name);
190 DEX_AVAILABLE_IN_ALL
191 void dex_future_disown (DexFuture *future);
192 DEX_AVAILABLE_IN_ALL
193 gboolean dex_await (DexFuture *future,
194 GError **error);
195 DEX_AVAILABLE_IN_ALL
196 gboolean dex_await_boolean (DexFuture *future,
197 GError **error);
198 DEX_AVAILABLE_IN_ALL
199 guint dex_await_enum (DexFuture *future,
200 GError **error);
201 DEX_AVAILABLE_IN_ALL
202 int dex_await_fd (DexFuture *future,
203 GError **error);
204 DEX_AVAILABLE_IN_ALL
205 guint dex_await_flags (DexFuture *future,
206 GError **error);
207 DEX_AVAILABLE_IN_ALL
208 char *dex_await_string (DexFuture *future,
209 GError **error) G_GNUC_WARN_UNUSED_RESULT;
210 DEX_AVAILABLE_IN_ALL
211 float dex_await_float (DexFuture *future,
212 GError **error);
213 DEX_AVAILABLE_IN_ALL
214 double dex_await_double (DexFuture *future,
215 GError **error);
216 DEX_AVAILABLE_IN_ALL
217 gpointer dex_await_pointer (DexFuture *future,
218 GError **error);
219 DEX_AVAILABLE_IN_ALL
220 int dex_await_int (DexFuture *future,
221 GError **error);
222 DEX_AVAILABLE_IN_ALL
223 guint dex_await_uint (DexFuture *future,
224 GError **error);
225 DEX_AVAILABLE_IN_ALL
226 gint64 dex_await_int64 (DexFuture *future,
227 GError **error);
228 DEX_AVAILABLE_IN_ALL
229 guint64 dex_await_uint64 (DexFuture *future,
230 GError **error);
231 DEX_AVAILABLE_IN_ALL
232 GVariant *dex_await_variant (DexFuture *future,
233 GError **error) G_GNUC_WARN_UNUSED_RESULT;
234 DEX_AVAILABLE_IN_ALL
235 gpointer dex_await_boxed (DexFuture *future,
236 GError **error) G_GNUC_WARN_UNUSED_RESULT;
237 DEX_AVAILABLE_IN_ALL
238 gpointer dex_await_object (DexFuture *future,
239 GError **error) G_GNUC_WARN_UNUSED_RESULT;
240
241 #if G_GNUC_CHECK_VERSION(3,0) && !defined(DEX_DISABLE_STATIC_NAME_MACROS)
242 # define _DEX_FUTURE_NEW_(func, counter, ...) \
243 ({ DexFuture *G_PASTE(__f, counter) = G_PASTE (dex_future_, func) (__VA_ARGS__); \
244 dex_future_set_static_name (DEX_FUTURE (G_PASTE (__f, counter)), G_STRLOC); \
245 G_PASTE (__f, counter); })
246 # define _DEX_FUTURE_NEW(func, ...) _DEX_FUTURE_NEW_(func, __COUNTER__, __VA_ARGS__)
247 # define dex_future_then(...) _DEX_FUTURE_NEW(then, __VA_ARGS__)
248 # define dex_future_then_loop(...) _DEX_FUTURE_NEW(then_loop, __VA_ARGS__)
249 # define dex_future_catch(...) _DEX_FUTURE_NEW(catch, __VA_ARGS__)
250 # define dex_future_catch_loop(...) _DEX_FUTURE_NEW(catch_loop, __VA_ARGS__)
251 # define dex_future_finally(...) _DEX_FUTURE_NEW(finally, __VA_ARGS__)
252 # define dex_future_finally_loop(...) _DEX_FUTURE_NEW(finally_loop, __VA_ARGS__)
253 # define dex_future_all(...) _DEX_FUTURE_NEW(all, __VA_ARGS__)
254 # define dex_future_allv(...) _DEX_FUTURE_NEW(allv, __VA_ARGS__)
255 # define dex_future_all_race(...) _DEX_FUTURE_NEW(all_race, __VA_ARGS__)
256 # define dex_future_all_racev(...) _DEX_FUTURE_NEW(all_racev, __VA_ARGS__)
257 # define dex_future_any(...) _DEX_FUTURE_NEW(any, __VA_ARGS__)
258 # define dex_future_anyv(...) _DEX_FUTURE_NEW(anyv, __VA_ARGS__)
259 # define dex_future_first(...) _DEX_FUTURE_NEW(first, __VA_ARGS__)
260 # define dex_future_firstv(...) _DEX_FUTURE_NEW(firstv, __VA_ARGS__)
261 # define dex_future_with_timeout_seconds(...) _DEX_FUTURE_NEW(with_timeout_seconds, __VA_ARGS__)
262 # define dex_future_with_timeout_msec(...) _DEX_FUTURE_NEW(with_timeout_msec, __VA_ARGS__)
263 # define dex_future_with_timeout(...) _DEX_FUTURE_NEW(with_timeout, __VA_ARGS__)
264 # define dex_future_with_deadline(...) _DEX_FUTURE_NEW(with_deadline, __VA_ARGS__)
265 # define dex_future_new_for_value(...) _DEX_FUTURE_NEW(new_for_value, __VA_ARGS__)
266 # define dex_future_new_for_error(...) _DEX_FUTURE_NEW(new_for_error, __VA_ARGS__)
267 # define dex_future_new_for_errno(...) _DEX_FUTURE_NEW(new_for_errno, __VA_ARGS__)
268 # define dex_future_new_for_string(...) _DEX_FUTURE_NEW(new_for_string, __VA_ARGS__)
269 # define dex_future_new_take_string(...) _DEX_FUTURE_NEW(new_take_string, __VA_ARGS__)
270 # define dex_future_new_for_int(...) _DEX_FUTURE_NEW(new_for_int, __VA_ARGS__)
271 # define dex_future_new_for_uint(...) _DEX_FUTURE_NEW(new_for_uint, __VA_ARGS__)
272 # define dex_future_new_for_int64(...) _DEX_FUTURE_NEW(new_for_int64, __VA_ARGS__)
273 # define dex_future_new_for_uint64(...) _DEX_FUTURE_NEW(new_for_uint64, __VA_ARGS__)
274 # define dex_future_new_for_boolean(...) _DEX_FUTURE_NEW(new_for_boolean, __VA_ARGS__)
275 # define dex_future_new_for_double(...) _DEX_FUTURE_NEW(new_for_double, __VA_ARGS__)
276 # define dex_future_new_enum(...) _DEX_FUTURE_NEW(new_enum, __VA_ARGS__)
277 # define dex_future_new_for_float(...) _DEX_FUTURE_NEW(new_for_float, __VA_ARGS__)
278 # define dex_future_new_take_variant(...) _DEX_FUTURE_NEW(new_take_variant, __VA_ARGS__)
279 # define dex_future_new_take_boxed(...) _DEX_FUTURE_NEW(new_take_boxed, __VA_ARGS__)
280 # define dex_future_new_for_object(...) _DEX_FUTURE_NEW(new_for_object, __VA_ARGS__)
281 # define dex_future_new_take_object(...) _DEX_FUTURE_NEW(new_take_object, __VA_ARGS__)
282 # define dex_future_new_for_pointer(...) _DEX_FUTURE_NEW(new_for_pointer, __VA_ARGS__)
283 #endif
284
285 #define dex_future_new_true() dex_future_new_for_boolean(TRUE)
286 #define dex_future_new_false() dex_future_new_for_boolean(FALSE)
287
288 #define dex_return_error_if_fail(expr) \
289 G_STMT_START { \
290 if G_UNLIKELY (!(expr)) \
291 return dex_future_new_reject (G_IO_ERROR, \
292 G_IO_ERROR_INVAL, \
293 "Expression failed: %s", \
294 G_STRINGIFY (expr)); \
295 } G_STMT_END
296
297 G_DEFINE_AUTOPTR_CLEANUP_FUNC (DexFuture, dex_unref)
298
299 G_END_DECLS