From 0a311af42c211aff76ff31a4962f4356ba486fdb Mon Sep 17 00:00:00 2001 From: Clifford Yapp <238416+starseeker@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:20:17 -0500 Subject: [PATCH] Allow for a "during execution" callback definition. Thinking about the search case, it's not pre or post command execution but is insteadn passed to search for execution *during* the command run. Allow for this case in assignment and retrieval functions. --- include/ged/defines.h | 13 ++++++++----- src/libged/exec.cpp | 4 ++-- src/libged/ged.cpp | 4 +++- src/libged/ged_private.h | 1 + 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/ged/defines.h b/include/ged/defines.h index ce43cdb790..c37aee13dc 100644 --- a/include/ged/defines.h +++ b/include/ged/defines.h @@ -405,9 +405,12 @@ GED_EXPORT extern void ged_init(struct ged *gedp); GED_EXPORT extern void ged_free(struct ged *gedp); -// Associate a callback function pointer for a command. If pre is non-zero, -// function will be registered to run BEFORE actual cmd logic is run, and if -// zero will be registered to run AFTER the cmd logic is run. +// Associate a callback function pointer for a command. If mode is less than zero, +// function will be registered to run BEFORE actual cmd logic is run, and if greater +// than zero will be registered to run AFTER the cmd logic is run. If mode is zero, +// then it is intended to be used *during* command execution and it will be up +// to the command's implementation to incorporate the callback into its +// execution logic. // // Only one function can be registered for each pre/post command slot - an // assignment to a command slot that already has an assigned function will @@ -425,10 +428,10 @@ GED_EXPORT extern void ged_free(struct ged *gedp); // // To clear all command callbacks, iterate over the ged_cmd_list results // and assign NULL f values. -GED_EXPORT extern int ged_clbk_set(struct ged *gedp, const char *cmd, int pre, bu_clbk_t f, void *d); +GED_EXPORT extern int ged_clbk_set(struct ged *gedp, const char *cmd, int mode, bu_clbk_t f, void *d); // Method calling code can use to get the current clbk info for a specific command. -GED_EXPORT extern int ged_clbk_get(bu_clbk_t *f, void **d, struct ged *gedp, const char *cmd, int pre); +GED_EXPORT extern int ged_clbk_get(bu_clbk_t *f, void **d, struct ged *gedp, const char *cmd, int mode); diff --git a/src/libged/exec.cpp b/src/libged/exec.cpp index 0dbf2648dd..8c4929e2a4 100644 --- a/src/libged/exec.cpp +++ b/src/libged/exec.cpp @@ -90,7 +90,7 @@ ged_exec(struct ged *gedp, int argc, const char *argv[]) // We have a command now - check for a pre-exec callback. bu_clbk_t f = NULL; void *d = NULL; - if ((ged_clbk_get(&f, &d, gedp, cmdname.c_str(), 1) == BRLCAD_OK) && f) { + if ((ged_clbk_get(&f, &d, gedp, cmdname.c_str(), -1) == BRLCAD_OK) && f) { cret = (*f)(argc, argv, gedp, d); if (cret != BRLCAD_OK) bu_log("error running %s pre-execution callback\n", cmdname.c_str()); @@ -111,7 +111,7 @@ ged_exec(struct ged *gedp, int argc, const char *argv[]) } // Command execution complete - check for a post command callback. - if ((ged_clbk_get(&f, &d, gedp, cmdname.c_str(), 0) == BRLCAD_OK) && f) { + if ((ged_clbk_get(&f, &d, gedp, cmdname.c_str(), -1) == BRLCAD_OK) && f) { cret = (*f)(argc, argv, gedp, d); if (cret != BRLCAD_OK) bu_log("error running %s post-execution callback\n", cmdname.c_str()); diff --git a/src/libged/ged.cpp b/src/libged/ged.cpp index fcaf0bfcb2..b05ac4ae5b 100644 --- a/src/libged/ged.cpp +++ b/src/libged/ged.cpp @@ -530,7 +530,8 @@ ged_clbk_set(struct ged *gedp, const char *cmd, int pre, bu_clbk_t f, void *d) std::string scmd = std::string(cmd); GED_CK_MAGIC(gedp); Ged_Internal *gedip = gedp->i->i; - std::map> *cm = (pre) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk; + std::map> *cm = (pre < 0) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk; + cm = (pre == 0) ? &gedip->cmd_during_clbk : cm; std::map>::iterator c_it = cm->find(scmd); if (c_it != cm->end()) ret |= GED_OVERRIDE; @@ -549,6 +550,7 @@ ged_clbk_get(bu_clbk_t *f, void **d, struct ged *gedp, const char *cmd, int pre) std::string scmd = std::string(cmd); Ged_Internal *gedip = gedp->i->i; std::map> *cm = (pre) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk; + cm = (pre == 0) ? &gedip->cmd_during_clbk : cm; std::map>::iterator c_it = cm->find(scmd); c_it = cm->find(scmd); if (c_it == cm->end()) { diff --git a/src/libged/ged_private.h b/src/libged/ged_private.h index 9e7b9fe975..2acd0422f3 100644 --- a/src/libged/ged_private.h +++ b/src/libged/ged_private.h @@ -48,6 +48,7 @@ class Ged_Internal { public: struct ged *gedp; std::map> cmd_prerun_clbk; + std::map> cmd_during_clbk; std::map> cmd_postrun_clbk; };