Debian Codegraph @

src/dex-closure.h

raw symbols SCIP
1 /*
2 * dex-closure.h
3 *
4 * Copyright 2026 Christian Hergert <[email protected]>
5 *
6 * This library 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 library 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
17 * License along with this program; 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 <glib-object.h>
29
30 #include "dex-future.h"
31 #include "dex-version-macros.h"
32
33 G_BEGIN_DECLS
34
35 /**
36 * DEX_DEFINE_CLOSURE_VALUE:
37 * @type: field type
38 * @name: field name
39 *
40 * A value field descriptor for #DEX_DEFINE_CLOSURE_TYPE().
41 */
42 #define DEX_DEFINE_CLOSURE_VALUE(type, name) \
43 (_DEX_CLOSURE_FIELD_VALUE_DECLARE, _DEX_CLOSURE_FIELD_VALUE_CLEAR, type, name, NULL)
44
45 /**
46 * DEX_DEFINE_CLOSURE_VALUE_WITH_CLEAR:
47 * @type: field type
48 * @name: field name
49 * @clear: clear callback taking a pointer to @type
50 *
51 * A value field descriptor for #DEX_DEFINE_CLOSURE_TYPE() which is cleared
52 * by calling @clear with the address of the value.
53 */
54 #define DEX_DEFINE_CLOSURE_VALUE_WITH_CLEAR(type, name, clear) \
55 (_DEX_CLOSURE_FIELD_VALUE_DECLARE, _DEX_CLOSURE_FIELD_VALUE_WITH_CLEAR_CLEAR, \
56 type, name, clear)
57
58 /**
59 * DEX_DEFINE_CLOSURE_POINTER:
60 * @type: field type
61 * @name: field name
62 * @clear: clear callback for @type
63 *
64 * A pointer field descriptor for #DEX_DEFINE_CLOSURE_TYPE(). @clear is
65 * required; use #DEX_DEFINE_CLOSURE_VALUE() for pointers which do not need
66 * cleanup.
67 */
68 #define DEX_DEFINE_CLOSURE_POINTER(type, name, clear) \
69 (_DEX_CLOSURE_FIELD_POINTER_DECLARE, _DEX_CLOSURE_FIELD_POINTER_CLEAR, type, name, clear)
70
71 /**
72 * DEX_DEFINE_CLOSURE_OBJECT:
73 * @type: object type
74 * @name: field name
75 *
76 * An object pointer field descriptor for #DEX_DEFINE_CLOSURE_TYPE().
77 */
78 #define DEX_DEFINE_CLOSURE_OBJECT(type, name) \
79 (_DEX_CLOSURE_FIELD_OBJECT_DECLARE, _DEX_CLOSURE_FIELD_OBJECT_CLEAR, type, name, NULL)
80
81 /**
82 * DEX_DEFINE_CLOSURE_TYPE:
83 * @type_name: generated struct name
84 * @type_prefix: function prefix for generated helpers
85 * @...: one or more `DEX_DEFINE_CLOSURE_*()` field descriptors
86 *
87 * This macro is a lightweight helper for quickly defining closure-like state
88 * structs with matching `type_prefix_new()` and `type_prefix_free()` helpers.
89 *
90 * It is intentionally generic; while coroutine task states are a common use case,
91 * the generated type is also useful for quick one-off task/closure structs.
92 *
93 * Example:
94 * ```c
95 * DEX_DEFINE_CLOSURE_TYPE (MyTaskState, my_task_state,
96 * DEX_DEFINE_CLOSURE_VALUE (gsize, bytes),
97 * DEX_DEFINE_CLOSURE_POINTER (GBytes *, bytes_obj, g_bytes_unref),
98 * DEX_DEFINE_CLOSURE_OBJECT (GSocketConnection, conn))
99 * ```
100 *
101 * Since: 1.2
102 */
103 #define DEX_DEFINE_CLOSURE_TYPE(type_name, type_prefix, ...) \
104 typedef struct _##type_name \
105 { \
106 guint pc; \
107 DexFuture *pending; \
108 _DEX_CLOSURE_FOR_EACH (_DEX_CLOSURE_FIELD_DECLARE, __VA_ARGS__) \
109 } type_name; \
110 static inline type_name *type_prefix##_new (void) \
111 { \
112 return g_new0 (type_name, 1); \
113 } \
114 static inline void type_prefix##_free (type_name *state) \
115 { \
116 if (state == NULL) \
117 return; \
118 dex_clear ((gpointer) &state->pending); \
119 _DEX_CLOSURE_FOR_EACH (_DEX_CLOSURE_FIELD_CLEAR, __VA_ARGS__) \
120 g_free (state); \
121 }
122
123 #define _DEX_CLOSURE_FIELD_DECLARE(field) \
124 _DEX_CLOSURE_FIELD_DECLARE_1 field
125 #define _DEX_CLOSURE_FIELD_DECLARE_1(declare, clear_func, type, name, clear) \
126 declare (type, name, clear)
127
128 #define _DEX_CLOSURE_FIELD_VALUE_DECLARE(type, name, clear) type name;
129 #define _DEX_CLOSURE_FIELD_POINTER_DECLARE(type, name, clear) type name;
130 #define _DEX_CLOSURE_FIELD_OBJECT_DECLARE(type, name, clear) type *name;
131
132 #define _DEX_CLOSURE_FIELD_CLEAR(field) \
133 _DEX_CLOSURE_FIELD_CLEAR_1 field
134 #define _DEX_CLOSURE_FIELD_CLEAR_1(declare, clear_func, type, name, clear) \
135 clear_func (state, type, name, clear);
136
137 #define _DEX_CLOSURE_FIELD_VALUE_CLEAR(state, type, name, clear) \
138 G_STMT_START { } G_STMT_END
139
140 #define _DEX_CLOSURE_FIELD_VALUE_WITH_CLEAR_CLEAR(state, type, name, clear) \
141 G_STMT_START { \
142 (clear) (&(state)->name); \
143 } G_STMT_END
144
145 #define _DEX_CLOSURE_FIELD_POINTER_CLEAR(state, type, name, clear) \
146 G_STMT_START { \
147 g_clear_pointer (&(state)->name, clear); \
148 } G_STMT_END
149
150 #define _DEX_CLOSURE_FIELD_OBJECT_CLEAR(state, type, name, clear) \
151 G_STMT_START { \
152 g_clear_object ((GObject **) &((state)->name)); \
153 } G_STMT_END
154
155 #define _DEX_CLOSURE_FOR_EACH(macro, ...) \
156 _DEX_CLOSURE_CONCAT (_DEX_CLOSURE_FOR_EACH_, _DEX_CLOSURE_NARGS (__VA_ARGS__)) \
157 (macro, __VA_ARGS__)
158
159 #define _DEX_CLOSURE_CONCAT(a, b) \
160 _DEX_CLOSURE_CONCAT_EXPAND (a, b)
161 #define _DEX_CLOSURE_CONCAT_EXPAND(a, b) a##b
162
163 #define _DEX_CLOSURE_NARGS(...) \
164 _DEX_CLOSURE_NARGS_ (__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
165 #define _DEX_CLOSURE_NARGS_(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, ...) a17
166
167 #define _DEX_CLOSURE_FOR_EACH_0(macro)
168 #define _DEX_CLOSURE_FOR_EACH_1(macro, a1) \
169 macro(a1)
170 #define _DEX_CLOSURE_FOR_EACH_2(macro, a1, a2) \
171 macro(a1) macro(a2)
172 #define _DEX_CLOSURE_FOR_EACH_3(macro, a1, a2, a3) \
173 macro(a1) macro(a2) macro(a3)
174 #define _DEX_CLOSURE_FOR_EACH_4(macro, a1, a2, a3, a4) \
175 macro(a1) macro(a2) macro(a3) macro(a4)
176 #define _DEX_CLOSURE_FOR_EACH_5(macro, a1, a2, a3, a4, a5) \
177 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5)
178 #define _DEX_CLOSURE_FOR_EACH_6(macro, a1, a2, a3, a4, a5, a6) \
179 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6)
180 #define _DEX_CLOSURE_FOR_EACH_7(macro, a1, a2, a3, a4, a5, a6, a7) \
181 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7)
182 #define _DEX_CLOSURE_FOR_EACH_8(macro, a1, a2, a3, a4, a5, a6, a7, a8) \
183 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8)
184 #define _DEX_CLOSURE_FOR_EACH_9(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9) \
185 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
186 macro(a9)
187 #define _DEX_CLOSURE_FOR_EACH_10(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) \
188 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
189 macro(a9) macro(a10)
190 #define _DEX_CLOSURE_FOR_EACH_11(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) \
191 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
192 macro(a9) macro(a10) macro(a11)
193 #define _DEX_CLOSURE_FOR_EACH_12(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) \
194 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
195 macro(a9) macro(a10) macro(a11) macro(a12)
196 #define _DEX_CLOSURE_FOR_EACH_13(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) \
197 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
198 macro(a9) macro(a10) macro(a11) macro(a12) macro(a13)
199 #define _DEX_CLOSURE_FOR_EACH_14(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) \
200 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
201 macro(a9) macro(a10) macro(a11) macro(a12) macro(a13) macro(a14)
202 #define _DEX_CLOSURE_FOR_EACH_15(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) \
203 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
204 macro(a9) macro(a10) macro(a11) macro(a12) macro(a13) macro(a14) macro(a15)
205 #define _DEX_CLOSURE_FOR_EACH_16(macro, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16) \
206 macro(a1) macro(a2) macro(a3) macro(a4) macro(a5) macro(a6) macro(a7) macro(a8) \
207 macro(a9) macro(a10) macro(a11) macro(a12) macro(a13) macro(a14) macro(a15) macro(a16)
208
209 G_END_DECLS