0

Main scan function of Hyperscan API looks like:

hs_error_t hs_scan(const hs_database_t *db, 
                   const char *data,
                   unsigned int length,
                   unsigned int flags,
                   hs_scratch_t *scratch,
                   match_event_handler onEvent,
                   void *context);

Somewhere in the header file the match_event_handler is defined as:

typedef int (*match_event_handler)(unsigned int id,
                                   unsigned long long from,
                                   unsigned long long to,
                                   unsigned int flags,
                                   void *context);

I have a class named Databag which has a member function with exact same prototype (Please note it is not static):

class Databag{
...
public:
int event_handler(unsigned int id,
                  unsigned long long from,
                  unsigned long long to,
                  unsigned int flags,
                  void *context);
...
}

I want to pass this method as argument to hyperscan, something like:

Databag the_databag;
hs_scan(db, data, length, flags, scratch, databag.event_handler, context);

Any idea how make it work? I think there should be an easy way using .* and .-> operators, something like:

int (Databag::*member_event_handler)(unsigned int id,
                                     unsigned long long from,
                                     unsigned long long to,
                                     unsigned int flags,
                                     void* context) = &Databag::event_handler;
Databag the_databag;
hs_scan(db, data, length, flags, scratch, databag.*member_event_handler, context);

And ... it failed.

How I should continue to make it working? Or is it possible at all?

Dariush Eivazi
  • 159
  • 1
  • 1
  • 9
  • 2
    *which has a member function with exact same prototype* -- No, it is not the exact same prototype. Non-static member functions and non-member functions are not the same thing, regardless of whether the arguments are the same. – PaulMcKenzie May 20 '22 at 12:37
  • And the duplicate basically points out something you should have realized -- how will `Hyperscan API` (who will issue the callback) know anything about the `Databag` object it will invoke the call on? If you look at how to call a non-static member function using a pointer, you will see that the syntax requires the caller to know the object. – PaulMcKenzie May 20 '22 at 12:41
  • As I understood there is not any straight way to pass non-static member methods because the called method has no idea about the object. Thanks. I will think about some other ways. – Dariush Eivazi May 20 '22 at 12:58
  • Some gentleman commented a great way to solve my problem, but unfortunately deleted it soon. Anyway, Thank for the idea of passing the pointer of databag as context to the hs_scan, which will be propagated by it when calling the event handler. then inside the event handler I can call the member event handler of the desired object. – Dariush Eivazi May 20 '22 at 13:15
  • 1
    Well-designed callback APIs let you specify some arbitrary data that will be passed to the callback function. The `context` argument for `hs_scan` is this pointer of your choice, and this is passed to your callback function, also as `context`. You only need to add a free function as an intermediate step. – molbdnilo May 20 '22 at 13:16
  • Thanks a lot Sir molbdnilo. Your great idea saved me. – Dariush Eivazi May 20 '22 at 13:19

0 Answers0