简体   繁体   English

头文件中的“关键字”短语有什么作用?

[英]What does the 'keyword' phrase do in header file?

Don't do C++ so this might be a silly question.不要使用 C++,所以这可能是一个愚蠢的问题。 I'm examining a header file .h where parameter IDs are supposedly defined, according to the documentation.根据文档,我正在检查一个头文件 .h,其中应该定义了参数 ID。 I've come across the lines where the parameters are and they are as follows:我遇到了参数所在的行,它们如下:

#define  SPCDLL_ERROR_KEYWORDS \
{ \
  keyword (NONE), \
  keyword (OPEN_FILE), \
  keyword (FILE_NVALID), \
  keyword (MEM_ALLOC), \
};

#define  PARAMETERS_KEYWORDS \
{ \
  keyword (LIMIT_LOW), \
  keyword (LIMIT_HIGH), \
  keyword (LEVEL), \
  keyword (HOLDOFF), \
};

#ifdef keyword
#undef keyword  /* prevents redefinition warning */
#endif

#define keyword(key) key

However I have searched and can't find out exactly what the keyword(key) line is doing?但是我已经搜索过,但无法确切地找出关键字(键)行在做什么? Is it assigning each parameter and ID based on it's order in the list?它是否根据列表中的顺序分配每个参数和 ID? Would that make LIMIT_LOW, ID=1?这会使 LIMIT_LOW, ID=1 吗? Why is it not explicitly defined.为什么没有明确定义。 Also as there are two lists of definitions of keyword how does it work?此外,由于有两个关键字定义列表,它是如何工作的?

I come from Python land where I would have defined these as two dictionaries with IDs and values for both Error_names and Parameter_vals.我来自 Python 领域,在那里我将它们定义为两个字典,其中包含 Error_names 和 Parameter_vals 的 ID 和值。

If someone could explain this I would be grateful.如果有人可以解释这一点,我将不胜感激。

#define means you are making a macro. #define表示您正在制作一个宏。 keyword(key) means you are making a function in that macro, which takes a parameter, key . keyword(key)表示您正在该宏中创建一个函数,该函数接受一个参数key The last key means you are taking that key parameter, and then you are doing something with it.最后一个key意味着您正在使用该键参数,然后您正在用它做一些事情。 It can be anything, you can std::cout it, or really anything else.它可以是任何东西,您可以使用std::cout或其他任何东西。


When you are calling that macro, you pass in the macro keyword and you put in anything for key.当您调用该宏时,您传入了 macro keyword并为 key 输入了任何内容。

You use X macros in the top of your file, and you have your keyword macro in those functions.您在文件顶部使用X 宏,并在这些函数中使用keyword宏。

Note that the lines请注意,线条

keyword (SOME_CONSTANT), \

are all part of a macro.都是宏的一部分。 That is, they will appear wherever the macros SPCDLL_ERROR_KEYWORDS and PARAMETERS_KEYWORDS get expanded.也就是说,它们将出现在宏SPCDLL_ERROR_KEYWORDSPARAMETERS_KEYWORDS展开的任何地方。

And, wherever that expansion takes place, it will use the keyword() definition in the last line of your code snippet.而且,无论扩展发生在哪里,它都会使用代码片段最后一行中的keyword()定义。 So, the expression PARAMETERS_KEYWORDS will actually expand to因此,表达式PARAMETERS_KEYWORDS实际上将扩展为

{
    LIMIT_LOW,
    LIMIT_HIGH,
    LEVEL,
    HOLDOFF,
};

However, it depends on how keyword() is actually defined at the point of use, it could be defined to expand to anything that the C preprocessor is capable of producing.但是,这取决于在使用时如何实际定义keyword() ,它可以定义为扩展到 C 预处理器能够生成的任何内容。 For instance例如

#define keyword(foo) 1
char foo[] = SPCDLL_ERROR_KEYWORDS;
printf("There are %d SPCDLL_ERROR keywords\n", sizeof(foo));

would produce the actual count of keyword() invocations within SPCDLL_ERROR_KEYWORDS .将在SPCDLL_ERROR_KEYWORDS生成keyword()调用的实际计数。

I have no clue in which context these macros are actually used/useful, but such constructions typically serve some specific purpose: The effect of keyword(FOO) is basically the same as FOO by itself, but it marks the use of FOO for other uses .我不知道这些宏在哪个上下文中实际使用/有用,但此类构造通常用于某些特定目的: keyword(FOO)的效果本身与FOO基本相同,但它标志着FOO用于其他用途. Such other uses can be in some other macros, or other tools that are used to process the source code.这种其他用途可以在一些其他宏或用于处理源代码的其他工具中。 Like documentation tools, linters, automatic header generation scripts, and so on, and so forth.像文档工具、linters、自动头生成脚本等等。

The only way to find out is actually grepping for where the expression \\<keyword\\> is used throughout the code base.找出答案的唯一方法实际上是在整个代码库中查找表达式\\<keyword\\>的使用位置。 Most likely, this will let you stumble across comments that provide hints, and/or some script that actually does the additional processing.最有可能的是,这会让您偶然发现提供提示的注释和/或一些实际执行额外处理的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM