I have met code like this.
#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();
The func return a dword which is the combination of these bit-mask. I want to test the return value which status it has. I write code like this.
if(ret&JOB_STATUS_PAUSED)
string str="JOB_STATUS_PAUSED";
if(ret&JOB_STATUS_ERROR)
string str="JOB_STATUS_ERROR";
I wonder is there a graceful way to treat bit mask? I think std::bitset is not enough, I also need to get the string of the macro. I think this macro can help, but I have no idea how to use it.
#define str(a) #a
//if I input str(JOB_STATUS_PAUSED) then I can get "JOB_STATUS_PAUSED"
#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
I think this meets my requirement.
User contributions licensed under CC BY-SA 3.0