Skip to content

Commit

Permalink
Allow for a "during execution" callback definition.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
starseeker committed Nov 25, 2024
1 parent d50d118 commit 0a311af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
13 changes: 8 additions & 5 deletions include/ged/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);



Expand Down
4 changes: 2 additions & 2 deletions src/libged/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/libged/ged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::pair<bu_clbk_t, void *>> *cm = (pre) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk;
std::map<std::string, std::pair<bu_clbk_t, void *>> *cm = (pre < 0) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk;
cm = (pre == 0) ? &gedip->cmd_during_clbk : cm;
std::map<std::string, std::pair<bu_clbk_t, void *>>::iterator c_it = cm->find(scmd);
if (c_it != cm->end())
ret |= GED_OVERRIDE;
Expand All @@ -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<std::string, std::pair<bu_clbk_t, void *>> *cm = (pre) ? &gedip->cmd_prerun_clbk : &gedip->cmd_postrun_clbk;
cm = (pre == 0) ? &gedip->cmd_during_clbk : cm;
std::map<std::string, std::pair<bu_clbk_t, void *>>::iterator c_it = cm->find(scmd);
c_it = cm->find(scmd);
if (c_it == cm->end()) {
Expand Down
1 change: 1 addition & 0 deletions src/libged/ged_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Ged_Internal {
public:
struct ged *gedp;
std::map<std::string, std::pair<bu_clbk_t, void *>> cmd_prerun_clbk;
std::map<std::string, std::pair<bu_clbk_t, void *>> cmd_during_clbk;
std::map<std::string, std::pair<bu_clbk_t, void *>> cmd_postrun_clbk;
};

Expand Down

0 comments on commit 0a311af

Please sign in to comment.