Macros to en/disable blockhook entries.
authorBen Morrow <ben@morrow.me.uk>
Tue, 13 Jul 2010 21:20:21 +0000 (22:20 +0100)
committerRafael Garcia-Suarez <rgs@consttype.org>
Thu, 15 Jul 2010 10:28:01 +0000 (12:28 +0200)
This allows the individual callbacks to be switched on and off as
necessary, without removing the entry from PL_blockhooks.

op.h
pod/perlguts.pod

diff --git a/op.h b/op.h
index 30a41c8..2ea1dce 100644 (file)
--- a/op.h
+++ b/op.h
@@ -670,6 +670,17 @@ Set an entry in the BHK structure, and set the flags to indicate it is
 valid. I<which> is a preprocessing token indicating which entry to set.
 The type of I<ptr> depends on the entry.
 
+=for apidoc Am|void|BhkDISABLE|BHK *hk|which
+Temporarily disable an entry in this BHK structure, by clearing the
+appropriate flag. I<which> is a preprocessor token indicating which
+entry to disable.
+
+=for apidoc Am|void|BhkENABLE|BHK *hk|which
+Re-enable an entry in this BHK structure, by setting the appropriate
+flag. I<which> is a preprocessor token indicating which entry to enable.
+This will assert (under -DDEBUGGING) if the entry doesn't contain a valid
+pointer.
+
 =for apidoc m|void|CALL_BLOCK_HOOKS|which|arg
 Call all the registered block hooks for type I<which>. I<which> is a
 preprocessing token; the type of I<arg> depends on I<which>.
@@ -687,10 +698,21 @@ preprocessing token; the type of I<arg> depends on I<which>.
 #define BhkENTRY(hk, which) \
     ((BhkFLAGS(hk) & BHKf_ ## which) ? ((hk)->bhk_ ## which) : NULL)
 
+#define BhkENABLE(hk, which) \
+    STMT_START { \
+       BhkFLAGS(hk) |= BHKf_ ## which; \
+       assert(BhkENTRY(hk, which)); \
+    } STMT_END
+
+#define BhkDISABLE(hk, which) \
+    STMT_START { \
+       BhkFLAGS(hk) &= ~(BHKf_ ## which); \
+    } STMT_END
+
 #define BhkENTRY_set(hk, which, ptr) \
     STMT_START { \
        (hk)->bhk_ ## which = ptr; \
-       (hk)->bhk_flags |= BHKf_ ## which; \
+       BhkENABLE(hk, which); \
     } STMT_END
 
 #define CALL_BLOCK_HOOKS(which, arg) \
index d0178e7..62e99bd 100644 (file)
@@ -1905,10 +1905,12 @@ start.
 
 Once registered, there is no mechanism to switch these hooks off, so if
 that is necessary you will need to do this yourself. An entry in C<%^H>
-is probably the best way, so the effect is lexically scoped. You should
-also be aware that generally speaking at least one scope will have
-opened before your extension is loaded, so you will see some
-C<pre/post_end> pairs that didn't have a matching C<start>.
+is probably the best way, so the effect is lexically scoped; however it
+is also possible to use the C<BhkDISABLE> and C<BhkENABLE> macros to
+temporarily switch entries on and off. You should also be aware that
+generally speaking at least one scope will have opened before your
+extension is loaded, so you will see some C<pre/post_end> pairs that
+didn't have a matching C<start>.
 
 =head1 Examining internal data structures with the C<dump> functions