Boost logo

Boost Users :

Subject: Re: [Boost-users] [interceptors] How to intercept when a function is called
From: Jason Roehm (jasonr_at_[hidden])
Date: 2014-07-02 15:41:55


On 07/02/2014 03:32 PM, Michael Powell wrote:
> Hello,
>
> I don't know if there is a first or second class library in Boost to
> do with this already. Or at least a C++ mechanism I could leverage.
>
> Basically, I want to hook or intercept when a function call is made. I
> do not want to change the underlying call, although I suppose that
> hook could be done as well. For now, I just want a callback when that
> function calls is made.
>
> For instance, a vector push_back listener. I want to listen when items
> are push_back-ed onto the vector. The items should still land in the
> vector. I just want the notification when that occurs.
>
> I've dug around for different "callback" flavors, but these just seem
> to be wrappers to an underlying method pointer, bound function, along
> these lines. I can do that easy enough but it doesn't buy me the
> interception when the push back is actually called.
>
> The tricky part with push_back as well, is that it comes in two
> flavors, so there needs to be a stand in lambda, something to declare
> the actual kind of argument(s) I want. Then there's the matter of
> Variadics. But for now I am not concerned with either of these.

You might have a look at the execute-around pattern. There is an example
here:

https://gitorious.org/redistd/redistd/commit/8079aa6399d7555a21d1551830c2d229d6bcba4c

Essentially, you wrap the object that you care about in another object
that implicitly converts to a pointer to the underlying type, but the
dereference operator is set up in such a way that you can invoke
arbitrary callables before and after each member function invocation.

I don't see a way to easily make this function-specific, or for a way to
determine inside the hook function which of the member functions was called.

Jason


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net