|
Boost Users : |
Subject: Re: [Boost-users] [interceptors] How to intercept when a function is called
From: Michael Powell (mwpowellhtx_at_[hidden])
Date: 2014-07-02 19:45:34
On Wed, Jul 2, 2014 at 5:15 PM, Edward Diener <eldiener_at_[hidden]> wrote:
> On 7/2/2014 6:00 PM, Michael Powell wrote:
>>
>> On Wed, Jul 2, 2014 at 2:41 PM, Jason Roehm <jasonr_at_[hidden]> wrote:
>>>
>>>
>>> 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.
>>
>>
>> Poor choice of wording I think on my part. Not necessarily that an
>> instance is being called, although that's a compelling thing to think
>> about. I need to know, for instance, in this case, not only the moment
>> when vector is being push_back-ed, but also the item being inserted.
>>
>> Which with a solution like the pointer or dereference overload, would
>> get a grand before-snapshot, but not just after.
>>
>>> 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.
>>
>>
>> It seems like I want to extend std::bind itself, if it were possibly.
>> I'll have a look at that. I can cook up a functor wrapper simple
>> enough. The trick is capturing just what I need and wiring up the
>> callback(s) around the actual bound function call.
>
>
> The Boost signals2 library lets you setup a callback when an event happens.
> How you define that event and what is called when the event occurs is up to
> you and you set this up for yourself.
Yes, I understand that, and appreciate it. However, in this case the
event is the call itself.
The best way I could manage that was to simply wrap a vector (or
deque) of std::function<something(somethings)>, the primary one being
the thing I want to monitor. Pass in the variadic arguments to the
wrapper operator()(Args... args), and call any appended (or prepended)
handlers afterward (or beforehand).
It was either that or coming up with a fancier binding functor. But in
this case I kind of like the extensibility of the variadic template
arguments. The only corner case I've run into is a peculiar (or not so
much) SFINAE issue with the void "return" type. Other than that, works
great.
Thanks...
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
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