Debian Codegraph @

src/dex-state-machine.h

raw symbols SCIP
1 /*
2 * dex-state-machine.h
3 *
4 * Copyright 2026 Christian Hergert
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * 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-future.h"
29 #include "dex-scheduler.h"
30 #include "dex-version-macros.h"
31
32 G_BEGIN_DECLS
33
34 #define DEX_TYPE_STATE_MACHINE (dex_state_machine_get_type())
35 #define DEX_STATE_MACHINE(object) (G_TYPE_CHECK_INSTANCE_CAST(object, DEX_TYPE_STATE_MACHINE, DexStateMachine))
36 #define DEX_IS_STATE_MACHINE(object) (G_TYPE_CHECK_INSTANCE_TYPE(object, DEX_TYPE_STATE_MACHINE))
37
38 typedef struct _DexStateMachine DexStateMachine;
39 typedef struct _DexStateTransitionContext DexStateTransitionContext;
40
41 /**
42 * DexStateTransitionFunc:
43 * @context: a [[email protected]]
44 * @user_data: (nullable): user data provided to [[email protected]]
45 * @error: a location for a `GError`, or %NULL
46 *
47 * Callback used to execute a transition edge.
48 *
49 * @context is opaque and only valid for the duration of the callback. It must
50 * not be stored or used after the callback returns.
51 *
52 * Use [[email protected]_from] and
53 * [[email protected]_to] to inspect the edge that caused
54 * the callback to run. Use [[email protected]_state] and
55 * [[email protected]_state] to inspect or update the real
56 * state of the [[email protected]] while the transition is running. Use
57 * [[email protected]_to] to continue through another
58 * declared edge before queued transition requests are processed.
59 *
60 * Returns: %TRUE if the transition succeeded; otherwise %FALSE and @error is set
61 */
62 typedef gboolean (*DexStateTransitionFunc) (DexStateTransitionContext *context,
63 gpointer user_data,
64 GError **error);
65
66 /**
67 * DexStateTransition:
68 * @from: the state this edge may start from
69 * @to: the state this edge transitions to
70 * @func: callback used to execute the edge
71 *
72 * Describes one supported transition edge in a [[email protected]].
73 */
74 typedef struct _DexStateTransition
75 {
76 guint from;
77 guint to;
78 DexStateTransitionFunc func;
79 } DexStateTransition;
80
81 DEX_AVAILABLE_IN_1_2
82 guint dex_state_transition_context_get_from (DexStateTransitionContext *context);
83 DEX_AVAILABLE_IN_1_2
84 guint dex_state_transition_context_get_to (DexStateTransitionContext *context);
85 DEX_AVAILABLE_IN_1_2
86 guint dex_state_transition_context_get_state (DexStateTransitionContext *context);
87 DEX_AVAILABLE_IN_1_2
88 void dex_state_transition_context_set_state (DexStateTransitionContext *context,
89 guint state);
90 DEX_AVAILABLE_IN_1_2
91 DexFuture *dex_state_transition_context_wait_for_interrupt (DexStateTransitionContext *context) G_GNUC_WARN_UNUSED_RESULT;
92 DEX_AVAILABLE_IN_1_2
93 gboolean dex_state_transition_context_continue_to (DexStateTransitionContext *context,
94 guint target,
95 GError **error) G_GNUC_WARN_UNUSED_RESULT;
96 DEX_AVAILABLE_IN_1_2
97 GType dex_state_machine_get_type (void);
98 DEX_AVAILABLE_IN_1_2
99 DexStateMachine *dex_state_machine_new (GType state_enum_type,
100 guint initial_state,
101 const DexStateTransition *transitions,
102 guint n_transitions,
103 DexScheduler *scheduler,
104 gsize stack_size,
105 gpointer user_data,
106 GDestroyNotify user_data_destroy);
107 DEX_AVAILABLE_IN_1_2
108 DexFuture *dex_state_machine_transition (DexStateMachine *state_machine,
109 guint target) G_GNUC_WARN_UNUSED_RESULT;
110 DEX_AVAILABLE_IN_1_2
111 DexFuture *dex_state_machine_wait_for_state (DexStateMachine *state_machine,
112 guint state) G_GNUC_WARN_UNUSED_RESULT;
113 DEX_AVAILABLE_IN_1_2
114 gboolean dex_state_machine_interrupt (DexStateMachine *state_machine);
115 DEX_AVAILABLE_IN_1_2
116 guint dex_state_machine_get_requested_state (DexStateMachine *state_machine);
117 DEX_AVAILABLE_IN_1_2
118 guint dex_state_machine_get_state (DexStateMachine *state_machine);
119
120 G_DEFINE_AUTOPTR_CLEANUP_FUNC (DexStateMachine, dex_unref)
121
122 G_END_DECLS