(.handler = (_handler),) 的中文翻译是:(.处理程序 = (_处理程序),)

huangapple go评论57阅读模式
英文:

Source analysis iw (.handler = (_handler),)

问题

我正在阅读源代码iw,但我卡在这个结构体上。

我不理解这些代码是什么意思?(.handler = (_handler),等等...)

我该如何理解它,或者有没有任何文档可以学习这个。

	static struct cmd						\
	__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden = {\
		.name = (_name),					\
		.args = (_args),					\
		.cmd = (_nlcmd),					\
		.nl_msg_flags = (_flags),				\
		.hidden = (_hidden),					\
		.idby = (_idby),					\
		.handler = (_handler),					\
		.help = (_help),					\
		.parent = _section,					\
		.selector = (_sel),					\
	};								\



<details>
<summary>英文:</summary>

I&#39;m reading the source code [iw](https://github.com/Distrotech/iw) and 
I&#39;m stucked in this struct.

I dont&#39;t understand what these code mean? (.handler = (_handler), etc...)

How can I understand that or any documents to learn that.

```#define __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel)\
	static struct cmd						\
	__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden = {\
		.name = (_name),					\
		.args = (_args),					\
		.cmd = (_nlcmd),					\
		.nl_msg_flags = (_flags),				\
		.hidden = (_hidden),					\
		.idby = (_idby),					\
		.handler = (_handler),					\
		.help = (_help),					\
		.parent = _section,					\
		.selector = (_sel),					\
	};								\




</details>


# 答案1
**得分**: 1

这是一个在静态结构中展开的宏。

如果您查看`iw/iw.h`文件中`cmd`结构的定义,您会看到`handler`成员是一个接受大量参数并返回整数的函数指针。

该宏初始化了结构,将提供的函数地址分配给此指针。现在,`handler`成员可以被用作结构的接口,可以通过它调用提供的函数。

<details>
<summary>英文:</summary>

This is a macro that unfolds in a static structure.

#define __COMMAND(_section, _symname, _name, _args, _nlcmd, _flags, _hidden, _idby, _handler, _help, _sel)
static struct cmd
__cmd ## _ ## _symname ## _ ## _handler ## _ ## _nlcmd ## _ ## _idby ## _ ## _hidden
attribute((used)) attribute((section("__cmd"))) = {
.name = (_name),
.args = (_args),
.cmd = (_nlcmd),
.nl_msg_flags = (_flags),
.hidden = (_hidden),
.idby = (_idby),
.handler = (_handler),
.help = (_help),
.parent = _section,
.selector = (_sel),
}


If you take a look at the definition of `cmd` struct from `iw/iw.h` file, you would see that the `handler` member is a function pointer taking a whole bunch of parameters and returning int.

struct cmd {
const char *name;
const char *args;
const char help;
const enum nl80211_commands cmd;
int nl_msg_flags;
int hidden;
const enum command_identify_by idby;
/

* The handler should return a negative error code,
* zero on success, 1 if the arguments were wrong
* and the usage message should and 2 otherwise.
*/
int (*handler)(struct nl80211_state *state,
struct nl_cb *cb,
struct nl_msg *msg,
int argc, char **argv,
enum id_input id);
const struct cmd *(*selector)(int argc, char **argv);
const struct cmd *parent;
};

The macro initializes the struct assigning the provided function address to this pointer. The `handler` member now can be used as sort of interface for the struct and one may call the provided function via it.

</details>



huangapple
  • 本文由 发表于 2023年2月19日 18:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定