在 C 预处理器中重命名变量
我有一些现有代码使用 enum
而不是回调函数指针。使用表调用回调:callback_table[enum]
。
枚举以 enum_ 前缀
命名,相应的函数以 func_ 前缀
命名。
因此,涉及回调的函数调用如下所示:
foo (param, enum_name);
在浏览代码时,我必须采用名称部分和前缀 func_ 而不是仅仅执行“跳转到定义”。
我想要一个宏,使我的代码看起来像这样:
foo (param, f2e(func_name));
这样我就可以保持现有代码不变,并且仍然能够执行“跳转到定义”。
可以有这样的宏吗?最简单的解决方案是省略 func_
这意味着 f2e simple 附加 enum_ 前缀
,但我更喜欢仍然可以使用 func_
的解决方案>。这可能吗?
I have some existing code that uses an enum
instead of a function pointer for callbacks. The callback is called using a table: callback_table[enum]
.
The enums are named with an enum_ prefix
and the corresponding function is named with a func_ prefix
.
So, function calls involving callbacks look like this:
foo (param, enum_name);
While browsing code I have to take the name part and prefix func_ instead of just doing a "Jump to definition".
I would like to have a macro so that my code looks like this:
foo (param, f2e(func_name));
so that I can keep the existing code unchanged and still be able to do a "Jump to definition".
Is it possible to have such a macro? The simplest solution would be to omit func_
which means that f2e simple attaches the enum_ prefix
, but I would prefer a solution where I can still have func_
. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点迂回,但您可以这样做:(
为了这个示例,我们假设您有三个可能的枚举值
enum_one
、enum_two
和enum_ Three
)。缺点(当然)是您需要为枚举的每个可能值指定一个特殊的#define。
替代
作为替代方案,如果您唯一需要的是方便使用函数名称,那么您可以使用 IDE 的“跳转到定义”功能...您可以执行以下操作:
那么您的调用将看起来像这样:
也许有点多余,但它可以以最小的对其余代码的侵入来实现您的目标。
It's a little round-about, but you could do something like this:
(let's assume, for the sake of this example, that you have three possible enum values
enum_one
,enum_two
, andenum_three
).the drawback (of course) is that you'll need a special #define for each possible value of your enum.
Alternative
As an alternative, if your only need is to have the function name handy, so you can use your IDE's "jump to definition" feature... you could do something like this:
then your call would look like this:
a little redundant perhaps, but it would accomplish your goal with minimal intrusion into the rest of your code.