aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/qreg-commands.h
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-26 18:10:42 +0100
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-26 18:10:42 +0100
commitc2114fa0af73b42bc1ef302f7511ef87690cc0b1 (patch)
tree3a0ad484d1e4c06e20efa8358f4261f16abe7542 /src/qreg-commands.h
parentd7330f252e6b0a1326eac6b5fc0b219a7b706eb7 (diff)
TECO_DEFINE_STATE() no longer constructs callback names for mandatory callbacks, but tries to use static assertionsHEADmaster-fmsbw-cimaster
* Requiring state callbacks by generating their names (e.g. NAME##_input) has several disadvantages: * The callback is not explicitly referenced when the state is defined. So an unintroduced reader will see some static function, which is nowhere referenced and still doesn't cause "unused" warnings. * You cannot choose the name of function that implements the callback freely. * In "substates" you need to generate a callback function if you want to provide a default. You also need to provide dummy wrapper functions whenever you want to reuse some existing function as the implementation. * Instead, we are now using static assertions to check whether certain callbacks have been implemented. Unfortunately, this does not work on all compilers. In particular GCC won't consider references to state objects fully constant (even though they are) and does not allow them in _Static_assert (G_STATIC_ASSERT). This could only be made to work in newer GCC with -std=c2x or -std=gnu23 in combination with constexpr. It does work on Clang, though. So I introduced TECO_ASSERT_SAFE() which also passes if the expression is *not* constant. These static assertions are not crucial - they do not check anything that can differ between systems. So we can always rely on the checks performed by FreeBSD CI for instance. Also, you will of course quickly notice missing callbacks at runtime - with and without additional runtime assertions. * All mandatory callbacks must still be explicitly initialized in the TECO_DEFINE_STATE calls. * After getting rid of generated callback implementations, the TECO_DEFINE_STATE macros can finally be qualified with `static`. * The TECO_DECLARE_STATE() macro has been removed. It no longer abstracts anything and cannot be used to declare static teco_state_t anyway. Also TECO_DEFINE_UNDO_CALL() also doesn't have a DECLARE counterpart.
Diffstat (limited to 'src/qreg-commands.h')
-rw-r--r--src/qreg-commands.h44
1 files changed, 20 insertions, 24 deletions
diff --git a/src/qreg-commands.h b/src/qreg-commands.h
index e224797..d0f6990 100644
--- a/src/qreg-commands.h
+++ b/src/qreg-commands.h
@@ -51,48 +51,44 @@ gboolean teco_state_expectqreg_insert_completion(teco_machine_main_t *ctx, const
* Super class for states accepting Q-Register specifications.
*/
#define TECO_DEFINE_STATE_EXPECTQREG(NAME, ...) \
- static teco_state_t * \
- NAME##_input(teco_machine_main_t *ctx, gunichar chr, GError **error) \
- { \
- return teco_state_expectqreg_input(ctx, chr, error); \
- } \
TECO_DEFINE_STATE(NAME, \
.initial_cb = (teco_state_initial_cb_t)teco_state_expectqreg_initial, \
+ .input_cb = (teco_state_input_cb_t)teco_state_expectqreg_input, \
.process_edit_cmd_cb = (teco_state_process_edit_cmd_cb_t) \
teco_state_expectqreg_process_edit_cmd, \
.insert_completion_cb = (teco_state_insert_completion_cb_t) \
teco_state_expectqreg_insert_completion, \
.style = SCE_SCITECO_QREG, \
.expectqreg.type = TECO_QREG_REQUIRED, \
- .expectqreg.got_register_cb = NAME##_got_register, /* always required */ \
##__VA_ARGS__ \
- )
+ ); \
+ TECO_ASSERT_SAFE(NAME.expectqreg.got_register_cb != NULL)
/*
* FIXME: Some of these states are referenced only in qreg-commands.c,
* so they should be moved there?
*/
-TECO_DECLARE_STATE(teco_state_pushqreg);
-TECO_DECLARE_STATE(teco_state_popqreg);
+extern teco_state_t teco_state_pushqreg;
+extern teco_state_t teco_state_popqreg;
-TECO_DECLARE_STATE(teco_state_eqcommand);
-TECO_DECLARE_STATE(teco_state_loadqreg);
+extern teco_state_t teco_state_eqcommand;
+extern teco_state_t teco_state_loadqreg;
-TECO_DECLARE_STATE(teco_state_epctcommand);
-TECO_DECLARE_STATE(teco_state_saveqreg);
+extern teco_state_t teco_state_epctcommand;
+extern teco_state_t teco_state_saveqreg;
-TECO_DECLARE_STATE(teco_state_queryqreg);
+extern teco_state_t teco_state_queryqreg;
-TECO_DECLARE_STATE(teco_state_ctlucommand);
-TECO_DECLARE_STATE(teco_state_setqregstring_nobuilding);
-TECO_DECLARE_STATE(teco_state_eucommand);
-TECO_DECLARE_STATE(teco_state_setqregstring_building);
+extern teco_state_t teco_state_ctlucommand;
+extern teco_state_t teco_state_setqregstring_nobuilding;
+extern teco_state_t teco_state_eucommand;
+extern teco_state_t teco_state_setqregstring_building;
-TECO_DECLARE_STATE(teco_state_getqregstring);
-TECO_DECLARE_STATE(teco_state_setqreginteger);
-TECO_DECLARE_STATE(teco_state_increaseqreg);
+extern teco_state_t teco_state_getqregstring;
+extern teco_state_t teco_state_setqreginteger;
+extern teco_state_t teco_state_increaseqreg;
-TECO_DECLARE_STATE(teco_state_macro);
-TECO_DECLARE_STATE(teco_state_indirect);
+extern teco_state_t teco_state_macro;
+extern teco_state_t teco_state_indirect;
-TECO_DECLARE_STATE(teco_state_copytoqreg);
+extern teco_state_t teco_state_copytoqreg;