From: Vincent Pit Date: Fri, 30 Mar 2012 20:25:35 +0000 (+0200) Subject: Switch to a config-object based interface X-Git-Tag: v0.02~11 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Fre-engine-Hooks.git;a=commitdiff_plain;h=b85e0f01bc196351b28fb97eb8ae37871076722a Switch to a config-object based interface --- diff --git a/Hooks.xs b/Hooks.xs index af878f3..0c9510a 100644 --- a/Hooks.xs +++ b/Hooks.xs @@ -58,8 +58,7 @@ STATIC SV *reh_hint(pTHX) { typedef struct reh_action { struct reh_action *next; - reh_comp_hook comp; - reh_exec_hook exec; + reh_config cbs; const char *key; STRLEN klen; } reh_action; @@ -73,7 +72,7 @@ STATIC perl_mutex reh_action_list_mutex; #endif /* USE_ITHREADS */ #undef reh_register -void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec) { +void reh_register(pTHX_ const char *key, reh_config *cfg) { reh_action *a; char *key_dup; STRLEN i, len; @@ -87,8 +86,7 @@ void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec) key_dup[len] = '\0'; a = PerlMemShared_malloc(sizeof *a); - a->comp = comp; - a->exec = exec; + a->cbs = *cfg; a->key = key_dup; a->klen = len; @@ -115,11 +113,11 @@ void reh_call_comp_hook(pTHX_ regexp *rx, regnode *node) { REH_UNLOCK(&reh_action_list_mutex); for (; a; a = a->next) { - if (a->comp) { + if (a->cbs.comp) { char *p = strstr(keys, a->key); if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ') - a->comp(aTHX_ rx, node); + a->cbs.comp(aTHX_ rx, node); } } } @@ -138,11 +136,11 @@ void reh_call_exec_hook(pTHX_ regexp *rx, regnode *node, regmatch_info *reginfo, REH_UNLOCK(&reh_action_list_mutex); for (; a; a = a->next) { - if (a->exec) { + if (a->cbs.exec) { char *p = strstr(keys, a->key); if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ') - a->exec(aTHX_ rx, node, reginfo, st); + a->cbs.exec(aTHX_ rx, node, reginfo, st); } } } diff --git a/lib/re/engine/Hooks.pm b/lib/re/engine/Hooks.pm index 07b71e4..50ba58e 100644 --- a/lib/re/engine/Hooks.pm +++ b/lib/re/engine/Hooks.pm @@ -45,7 +45,10 @@ In your XS file : BOOT: { - reh_register("Devel::Regexp::Instrument", dri_comp_hook, dri_exec_hook); + reh_config cfg; + cfg.comp = dri_comp_hook; + cfg.exec = dri_exec_hook; + reh_register("Devel::Regexp::Instrument", &cfg); } In your Perl module file : @@ -107,12 +110,35 @@ Currently evaluates to : typedef void (*reh_exec_hook)(pTHX_ regexp *, regnode *, regmatch_info *, regmatch_state *); +=head2 C + +A typedef'd struct that holds a set of all the different callbacks publicized by this module. +It has the following members : + +=over 4 + +=item * + +C + +A function pointer of type C that will be called each time a regnode is compiled. +Allowed to be C if you don't want to call anything for this phase. + +=item * + +C + +A function pointer of type C that will be called each time a regnode is executed. +Allowed to be C if you don't want to call anything for this phase. + +=back + =head2 C - void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec); + void reh_register(pTHX_ const char *key, reh_config *cfg); -Registers under the given name C a callback C that will run during the compilation phase and a callback C that will run during the execution phase. -Null function pointers are allowed in case you don't want to hook one of the phases. +Registers the callbacks specified by the C object C under the given name C. +C can be a pointer to a static object of type C. C should match with the argument passed to L and L in Perl land. An exception will be thrown if C has already been used to register callbacks. diff --git a/re_engine_hooks.h b/re_engine_hooks.h index 190e98d..3613490 100644 --- a/re_engine_hooks.h +++ b/re_engine_hooks.h @@ -7,8 +7,13 @@ typedef void (*reh_comp_hook)(pTHX_ regexp *, regnode *); typedef void (*reh_exec_hook)(pTHX_ regexp *, regnode *, regmatch_info *, regmatch_state *); -void reh_register(pTHX_ const char *key, reh_comp_hook comp, reh_exec_hook exec); -#define reh_register(K, C, E) reh_register(aTHX_ (K), (C), (E)) +typedef struct { + reh_comp_hook comp; + reh_exec_hook exec; +} reh_config; + +void reh_register(pTHX_ const char *, reh_config *); +#define reh_register(K, C) reh_register(aTHX_ (K), (C)) #endif /* RE_ENGINE_HOOKS_H */ diff --git a/t/re-engine-Hooks-TestDist/TestDist.xs b/t/re-engine-Hooks-TestDist/TestDist.xs index 95e20ed..4e1d511 100644 --- a/t/re-engine-Hooks-TestDist/TestDist.xs +++ b/t/re-engine-Hooks-TestDist/TestDist.xs @@ -79,14 +79,29 @@ PROTOTYPES: ENABLE BOOT: { - reh_register(__PACKAGE__ "::foo", reht_foo_comp, reht_foo_exec); - reht_foo_var = NULL; + { + reh_config foo_cfg; + foo_cfg.comp = reht_foo_comp; + foo_cfg.exec = reht_foo_exec; + reh_register(__PACKAGE__ "::foo", &foo_cfg); + reht_foo_var = NULL; + } - reh_register(__PACKAGE__ "::bar", reht_bar_comp, reht_bar_exec); - reht_bar_var = NULL; + { + reh_config bar_cfg; + bar_cfg.comp = reht_bar_comp; + bar_cfg.exec = reht_bar_exec; + reh_register(__PACKAGE__ "::bar", &bar_cfg); + reht_bar_var = NULL; + } - reh_register(__PACKAGE__ "::custom", reht_custom_comp, reht_custom_exec); - reht_custom_var = NULL; + { + reh_config custom_cfg; + custom_cfg.comp = reht_custom_comp; + custom_cfg.exec = reht_custom_exec; + reh_register(__PACKAGE__ "::custom", &custom_cfg); + reht_custom_var = NULL; + } } void