C++ 如何在C++;?

C++ 如何在C++;?,c++,C++,我遇到过这样的代码 #define JOB_STATUS_PAUSED 0x00000001 #define JOB_STATUS_ERROR 0x00000002 #define JOB_STATUS_DELETING 0x00000004 #define JOB_STATUS_SPOOLING 0x00000008 #define JOB_STAT

我遇到过这样的代码

    #define JOB_STATUS_PAUSED               0x00000001
    #define JOB_STATUS_ERROR                0x00000002
    #define JOB_STATUS_DELETING             0x00000004
    #define JOB_STATUS_SPOOLING             0x00000008
    #define JOB_STATUS_PRINTING             0x00000010
    #define JOB_STATUS_OFFLINE              0x00000020
    #define JOB_STATUS_PAPEROUT             0x00000040
    #define JOB_STATUS_PRINTED              0x00000080
    #define JOB_STATUS_DELETED              0x00000100
    #define JOB_STATUS_BLOCKED_DEVQ         0x00000200
    #define JOB_STATUS_USER_INTERVENTION    0x00000400
    #define JOB_STATUS_RESTART              0x00000800

    DWORD func();
func返回一个dword,它是这些位掩码的组合。我想测试返回值的状态。我这样写代码

    if(ret&JOB_STATUS_PAUSED)
      string str="JOB_STATUS_PAUSED";
    if(ret&JOB_STATUS_ERROR)
      string str="JOB_STATUS_ERROR";
我想知道有没有一种优雅的方法来治疗比特面具?我认为std::bitset是不够的,我还需要得到宏的字符串。我认为这个宏可以帮助你,但我不知道如何使用它

   #define str(a) #a
   //if I input str(JOB_STATUS_PAUSED) then I can get "JOB_STATUS_PAUSED"
定义str(a)L#a 常量wchar_t*statusStr[]{ str(作业状态已暂停), str(作业状态错误), str(作业状态删除), str(作业状态假脱机), str(作业状态打印), str(作业状态离线), str(工作状态文件), str(工作状态打印), str(作业状态已删除), str(作业状态、封锁状态、开发), str(作业状态用户干预), str(作业状态重新启动) }; int len=dimf1(statusStr); std::载体ret; for(int i=0,init=1;iinit=init您可以使用位集。您应该看看如何将它们放在枚举中而不是定义中。这样可能会更好,而不是十六进制值,二进制文字可能更可读(
0b0001
0b0010
0b0100
),但这是有争议的。除了代码评审的注释外,我认为没有比在一系列
if
语句中显式地命名案例或循环并在此基础上应用一些函数更好的方法了(以状态为参数并使用
switch
语句的函数,或使用函数指针映射来委派任务)。
#define str(a) L#a
        const wchar_t* statusStr[]{
            str(JOB_STATUS_PAUSED),
            str(JOB_STATUS_ERROR),
            str(JOB_STATUS_DELETING),
            str(JOB_STATUS_SPOOLING),
            str(JOB_STATUS_PRINTING),
            str(JOB_STATUS_OFFLINE),
            str(JOB_STATUS_PAPEROUT),
            str(JOB_STATUS_PRINTED),
            str(JOB_STATUS_DELETED),
            str(JOB_STATUS_BLOCKED_DEVQ),
            str(JOB_STATUS_USER_INTERVENTION),
            str(JOB_STATUS_RESTART)
        };

        int len = dimof1(statusStr);
        std::vector<std::wstring> ret;

        for (int i = 0, init = 1; i < len; i++) {
            if (status & init) {
                ret.push_back(statusStr[i]);
            }
            init = init << 1;
        }
        return ret;

#undef str