clibev

what this syntax in libev C language?


While reading the doc in libev I find some C code of which the syntax is quite weird.

static void
stdin_cb (EV_P_ ev_io *w, int revents)
{
     puts ("stdin ready");
     // for one-shot events, one must manually stop the watcher
     // with its corresponding stop function.
     ev_io_stop (EV_A_ w);

     // this causes all nested ev_run's to stop iterating
     ev_break (EV_A_ EVBREAK_ALL);
}

I'm not sure what the EV_P_ is here, could anyone help explain it to me?

I have tried to google the syntax of method signature in C but no good matches.


Solution

  • See ev.h:

    #if EV_MULTIPLICITY
    struct ev_loop;
    # define EV_P  struct ev_loop *loop /* a loop as sole parameter in a declaration */
    # define EV_P_ EV_P,                /* a loop as first of multiple parameters */
    ...
    #else
    # define EV_P void
    # define EV_P_
    ...
    #endif
    

    Therefore the line

    stdin_cb (EV_P_ ev_io *w, int revents)
    

    expands to

    stdin_cb (struct ev_loop *loop, ev_io *w, int revents)
    

    or

    stdin_cb (ev_io *w, int revents)
    

    depending on the value of EV_MULTIPLICITY

    As pointed out by @Shawn, there is a Macro magic section that explains it:

    EV_P, EV_P_

    This provides the loop parameter for functions, if one is required ("ev loop parameter"). The EV_P form is used when this is the sole parameter, EV_P_ is used when other parameters are following. Example:
    
       // this is how ev_unref is being declared
       static void ev_unref (EV_P);
    
       // this is how you can declare your typical callback
       static void cb (EV_P_ ev_timer *w, int revents)
    
    It declares a parameter loop of type struct ev_loop *, quite suitable for use with EV_A.