记录 C(或可能是 C++)中 X 宏的使用模式的良好参考资料是什么?
“X-Macros”的基本定义和示例以及一些参考是关于 C 预处理器的维基百科条目中给出:
X-Macro 是一个头文件(通常 使用“.def”扩展名而不是 传统的“.h”)包含 类似宏调用的列表(可以 称为“组件宏”)。
关于如何使用这种强大技术的一些好的信息来源有哪些? 有知名的开源库使用这种方法吗?
A basic definition and example and a few references for "X-Macros" is given in this wikipedia entry on the C pre-processor:
An X-Macro is a header file (commonly
using a ".def" extension instead of
the traditional ".h") that contains a
list of similar macro calls (which can
be referred to as "component macros").
What are some good sources of information on how to use this powerful technique?
Are there well-known open source libraries using this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在代码中经常使用 X Macros()。 该值来自仅将新数据添加到“X 列表”并且不修改任何其他代码。
X Macros() 最常见的用途是将错误文本与错误代码关联起来。 当添加新的错误代码时,程序员必须记住添加代码和文本,通常是在不同的位置。 X 宏允许将新的错误数据添加到单个位置,并自动填充到任何需要的地方。
不幸的是,这些机制使用了大量的预编译器魔法,可能会使代码有点难以阅读(例如使用
token1##token2
连接字符串,使用#token
创建字符串) )。 因此,我通常会在注释中解释 X 宏的作用。这是使用错误/返回值的示例。 所有新数据都会添加到“
X_ERROR
”列表中。 其他代码均无需修改。您还可以使用 X Macros() 生成代码。 例如,为了测试错误值是否“已知”,X 宏可以在 switch 语句中生成案例:
I use X Macros() in code a lot. The value comes from only adding new data only to the "X list" and not modifying any other code.
The most common use of X Macros() is for associating error text with error codes. When new error codes are added, programmers must remember to add the code and the text, typically in separate places. The X Macro allows the new error data to be added in a single place and get automatically populated anywhere it is needed.
Unfortunately, the mechanisms use a lot of pre-compiler magic that can make the code somewhat hard to read (e.g. string joining with
token1##token2
, string creation with#token
). Because of this I typically explain what the X Macro is doing in the comments.Here is an example using the error/return values. All new data gets added to the "
X_ERROR
" list. None of the other code hast to be modified.You can also use X Macros() to generate code. For example to test if an error value is "known", the X Macro can generate cases in a switch statement:
几年前,当我开始在代码中使用函数指针时,我发现了 X 宏。 我是一名嵌入式程序员,经常使用状态机。 我经常会编写这样的代码:
问题是我认为必须维护函数指针表的顺序以使其与状态枚举的顺序相匹配非常容易出错。
我的一位朋友向我介绍了 X 宏,这就像我脑子里的一个灯泡突然亮了。 说真的,我这辈子x宏你都去哪儿了!
所以现在我定义下表:
我可以按如下方式使用它:
作为
奖励,我还可以让预处理器构建我的函数原型,如下所示:
另一种用法是声明和初始化寄存器
然而,我最喜欢的用法是当涉及通信处理程序
首先,我创建一个通信表,其中包含每个命令名称和代码:
表中既有大写名称又有小写名称,因为大写字母将用于枚举,小写字母将用于函数名称。
然后,我还为每个命令定义结构,以定义每个命令的外观:
同样,我为每个命令响应定义结构:
然后我可以定义我的命令代码枚举:
我可以定义我的命令长度枚举:
我可以定义我的响应长度枚举:
我可以确定有多少命令,如下所示:
注意:我从未真正实例化 offset_struct_t,我只是将它用作编译器为我生成命令数量的一种方式。
请注意,我可以生成函数指针表,如下所示:
和我的函数原型:
现在最后一个最酷的用途是,我可以让编译器计算我的传输缓冲区应该有多大。
同样,这个联合就像我的偏移结构,它没有实例化,而是我可以使用 sizeof 运算符来声明我的传输缓冲区大小。
现在我的传输缓冲区 tx_buf 是最佳大小,当我向此通信处理程序添加命令时,我的缓冲区将始终是最佳大小。 凉爽的!
I discovered X-macros a couple of years ago when I started making use of function pointers in my code. I am an embedded programmer and I use state machines frequently. Often I would write code like this:
The problem was that I considered it very error prone to have to maintain the ordering of my function pointer table such that it matched the ordering of my enumeration of states.
A friend of mine introduced me to X-macros and it was like a light-bulb went off in my head. Seriously, where have you been all my life x-macros!
So now I define the following table:
And I can use it as follows:
and
as a bonus, I can also have the pre-processor build my function prototypes as follows:
Another usage is to declare and initialize registers
My favourite usage however is when it comes to communication handlers
First I create a comms table, containing each command name and code:
I have both the uppercase and lowercase names in the table, because the upper case will be used for enums and the lowercase for function names.
Then I also define structs for each command to define what each command looks like:
Likewise I define structs for each command response:
Then I can define my command code enumeration:
I can define my command length enumeration:
I can define my response length enumeration:
I can determine how many commands there are as follows:
NOTE: I never actually instantiate the offset_struct_t, I just use it as a way for the compiler to generator for me my number of commands.
Note then I can generate my table of function pointers as follows:
And my function prototypes:
Now lastly for the coolest use ever, I can have the compiler calculate how big my transmit buffer should be.
Again this union is like my offset struct, it is not instantiated, instead I can use the sizeof operator to declare my transmit buffer size.
Now my transmit buffer tx_buf is the optimal size and as I add commands to this comms handler, my buffer will always be the optimal size. Cool!
Dobb 博士有一篇关于此的文章。
Dr. Dobb's has an article on this.