diff options
| author | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-12-26 18:10:42 +0100 |
|---|---|---|
| committer | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-12-26 18:10:42 +0100 |
| commit | c2114fa0af73b42bc1ef302f7511ef87690cc0b1 (patch) | |
| tree | 3a0ad484d1e4c06e20efa8358f4261f16abe7542 /src/parser.h | |
| parent | d7330f252e6b0a1326eac6b5fc0b219a7b706eb7 (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/parser.h')
| -rw-r--r-- | src/parser.h | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/src/parser.h b/src/parser.h index 2308925..4ab4d25 100644 --- a/src/parser.h +++ b/src/parser.h @@ -248,15 +248,18 @@ gboolean teco_state_process_edit_cmd(teco_machine_t *ctx, teco_machine_t *parent * @implements teco_state_t * @ingroup states * - * @todo Should we eliminate required callbacks, this could be turned into a - * struct initializer TECO_INIT_STATE() and TECO_DECLARE_STATE() would become pointless. - * This would also ease declaring static states. + * Base class of all states. + * + * Since states are constant, you can append static assertions for required callbacks + * and other conditions. + * You should use TECO_ASSERT_SAFE(), but it won't be checked on all supported compilers. + * You should not put anything in front of the definition, though, so you + * can write `static TECO_DEFINE_STATE(...)`. */ #define TECO_DEFINE_STATE(NAME, ...) \ /** @ingroup states */ \ teco_state_t NAME = { \ .initial_cb = NULL, /* do nothing */ \ - .input_cb = (teco_state_input_cb_t)NAME##_input, /* always required */ \ .refresh_cb = NULL, /* do nothing */ \ .end_of_macro_cb = teco_state_end_of_macro, \ .process_edit_cmd_cb = teco_state_process_edit_cmd, \ @@ -265,11 +268,8 @@ gboolean teco_state_process_edit_cmd(teco_machine_t *ctx, teco_machine_t *parent .keymacro_mask = TECO_KEYMACRO_MASK_DEFAULT, \ .style = SCE_SCITECO_DEFAULT, \ ##__VA_ARGS__ \ - } - -/** @ingroup states */ -#define TECO_DECLARE_STATE(NAME) \ - extern teco_state_t NAME + }; \ + TECO_ASSERT_SAFE(NAME.input_cb != NULL) /* in cmdline.c */ gboolean teco_state_caseinsensitive_process_edit_cmd(teco_machine_t *ctx, teco_machine_t *parent_ctx, gunichar chr, GError **error); @@ -610,18 +610,11 @@ gboolean teco_state_expectstring_insert_completion(teco_machine_main_t *ctx, con * Super-class for states accepting string arguments * Opaquely cares about alternative-escape characters, * string building commands and accumulation into a string - * - * @note Generating the input_cb could be avoided if there were a default - * implementation. */ #define TECO_DEFINE_STATE_EXPECTSTRING(NAME, ...) \ - static teco_state_t * \ - NAME##_input(teco_machine_main_t *ctx, gunichar chr, GError **error) \ - { \ - return teco_state_expectstring_input(ctx, chr, error); \ - } \ TECO_DEFINE_STATE(NAME, \ .initial_cb = (teco_state_initial_cb_t)teco_state_expectstring_initial, \ + .input_cb = (teco_state_input_cb_t)teco_state_expectstring_input, \ .refresh_cb = (teco_state_refresh_cb_t)teco_state_expectstring_refresh, \ .process_edit_cmd_cb = (teco_state_process_edit_cmd_cb_t) \ teco_state_expectstring_process_edit_cmd, \ @@ -631,10 +624,10 @@ gboolean teco_state_expectstring_insert_completion(teco_machine_main_t *ctx, con .style = SCE_SCITECO_STRING, \ .expectstring.string_building = TRUE, \ .expectstring.last = TRUE, \ - .expectstring.process_cb = NULL, /* do nothing */ \ - .expectstring.done_cb = NAME##_done, /* always required */ \ + .expectstring.process_cb = NULL, /* do nothing */ \ ##__VA_ARGS__ \ - ) + ); \ + TECO_ASSERT_SAFE(NAME.expectstring.done_cb != NULL) gboolean teco_state_expectfile_process(teco_machine_main_t *ctx, const teco_string_t *str, gsize new_chars, GError **error); |
