有没有一种方法可以编写类似C/C ++函数的宏,以测试是否定义了类似对象的宏并生成使用该宏的代码? [英] Is there a way to write a C/C++ function-like macro that tests if an object-like macro is defined and generates code uses it?

查看:74
本文介绍了有没有一种方法可以编写类似C/C ++函数的宏,以测试是否定义了类似对象的宏并生成使用该宏的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在网站上找到有关在宏内使用 #if #ifdef 的其他问题,以及克服限制以实现各种目的的方法,但是这些都没有他们或他们的答案似乎与我的用例相符,这对我来说似乎是显而易见的:

I can find other questions on the site about using #if and #ifdef inside macros, and ways to get around the restriction to achieve various things, but none of them or their answers seem to match my use-case, which seems like an obvious one to me:

// the system flags are defined as object-like macros in this header...
#include <sys/stat.h>

// naive attempt at function-like macro that of course cannot work
#define MAYBE_EXPORT_FLAG(flag)                \
  #if defined(flag).                           \
    exports.Set(Napi::String::New(env, #flag), \
                Napi::Number::New(env, flag)); \
  #endif


Napi::Object Init(Napi::Env env, Napi::Object exports) {
    MAYBE_EXPORT_FLAG(UF_NODUMP)
    MAYBE_EXPORT_FLAG(UF_IMMUTABLE)
    MAYBE_EXPORT_FLAG(UF_APPEND)

    return exports;
}

例如,上一个问题看起来很相似,并且提供了一种解决方法,但是我无法将那组宏用于我的情况,或者它是否适用,我对那里的内容一无所知.无论如何,它不会在生成的代码中使用宏的参数.

For instance, this previous question seems similar and a workaround is provided, but I couldn't adapt that set of macros to my case, or if it is adaptable I'm not understanding something there. In any case it doesn't use the macro's parameter in the generated code.

我确实发现周围存在一些技巧,可以解决其他一些问题想要的事情,但是我找不到一种使它们中的任何一种适合这种情况的方法.但是它们很棘手.那么,有什么把戏吗?还是根本不可能?

I did find that there were some tricks around to do what some of the other questions wanted but I couldn't find a way to adapt any of them to this case. But they are quite tricky. So is there some trick or is it simply impossible?

推荐答案

您最终要做的是为每个感兴趣的符号定义一个宏,该宏根据是否定义该符号而扩展:

What you end up needing to do is define a macro for every symbol of interest that expands based on whether the symbol is defined:

#include <sys/stat.h>

#ifdef UF_NODUMP
#define IFDEF_UF_NODUMP(...)  __VA_ARGS__
#else
#define IFDEF_UF_NODUMP(...)
#endif
#ifdef UF_IMMUTABLE
#define IFDEF_UF_IMMUTABLE(...)  __VA_ARGS__
#else
#define IFDEF_UF_IMMUTABLE(...)
#endif
#ifdef UF_APPEND
#define IFDEF_UF_APPEND(...)  __VA_ARGS__
#else
#define IFDEF_UF_APPEND(...)
#endif

#define MAYBE_EXPORT_FLAG(flag)                    \
    IFDEF_ ## flag(                                \
        exports.Set(Napi::String::New(env, #flag), \
                    Napi::Number::New(env, flag)); \
    )

Napi::Object Init(Napi::Env env, Napi::Object exports) {
    MAYBE_EXPORT_FLAG(UF_NODUMP)
    MAYBE_EXPORT_FLAG(UF_IMMUTABLE)
    MAYBE_EXPORT_FLAG(UF_APPEND)

    return exports;
}

这有点痛苦且重复,但是您可以将所有重复的内容放在其自己的头文件中,而无需太多引用.根据您要尝试执行的操作,您可能会发现为每个符号定义 IFNDEF _ 宏也很有用.

This is somewhat painful and repetitive, but you can put all the repeated stuff in its own header file somewhere that you don't need to refer to much. Depending on what you are are trying to do, you might find it useful to define IFNDEF_ macros for each symbol as well.

这篇关于有没有一种方法可以编写类似C/C ++函数的宏,以测试是否定义了类似对象的宏并生成使用该宏的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆