libev C语言的语法是什么?

gijlo24d  于 2023-01-12  发布在  其他
关注(0)|答案(2)|浏览(103)

在阅读libev中的文档时,我发现一些语法相当奇怪的C代码。

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);
}

我不确定这里的EV_P_是什么,有人能帮我解释一下吗?
我试过谷歌的语法方法签名在C中,但没有很好的匹配。

kgqe7b3p

kgqe7b3p1#

参见版本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

因此,生产线

stdin_cb (EV_P_ ev_io *w, int revents)

扩展到

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

stdin_cb (ev_io *w, int revents)

取决于EV_MULTIPLICITY的值
正如@Shawn所指出的,有一个Macro魔术部分对此进行了解释:
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.
bweufnob

bweufnob2#

EV_P_是一个宏,意思是"一个ev循环作为参数,加上一个逗号"。
EV_A_是一个宏,意思是"一个ev循环作为参数,加上一个逗号"。
它们的defined

#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 */
#define EV_A  loop                  /* a loop as sole argument to a function call */
#define EV_A_ EV_A,                 /* a loop as first of multiple arguments */

或作为

# define EV_P void
# define EV_P_
# define EV_A
# define EV_A_

(Some删除了空格,以便更好地匹配。)
这意味着

static void stdin_cb( EV_P_ ev_io *w, int revents ) {
     puts( "stdin ready" );
     ev_io_stop( EV_A_ w );
     ev_break( EV_A_ EVBREAK_ALL );
}

相当于

static void stdin_cb( struct ev_loop *loop, ev_io *w, int revents ) {
     puts( "stdin ready" );
     ev_io_stop( loop, w );
     ev_break( loop, EVBREAK_ALL );
}

static void stdin_cb( ev_io *w, int revents ) {
     puts( "stdin ready" );
     ev_io_stop( w );
     ev_break( EVBREAK_ALL );
}

使用哪组#define指令是可配置的。
如果EV_MULTIPLICITY设置为非零,则使用第一个设置。第一个设置允许在同一个程序中使用多个ev循环(可能在不同的线程中)。
如果EV_MULTIPLICITY未设置或为零,则使用第二个集合。第二个集合更高效,因为它使用全局变量,而不是向每个ev相关函数传递一个结构。但程序只能有一个事件循环。

相关问题