#include <stdio.h>
#define ERRORS(_) \
_(NO_ERROR, 0x00) \
/* discontiguous */ \
_(EMPTY, 0x02) \
_(OVERFLOW, 0x03) \
_(TIMEOUT, 0x04) \
/** X-Macro to define ERROR_<FOO> enum entries */
#define AS_ENUM(name, ID) ERROR_##name = ID,
/** X-Macro to define {"ERROR_FOO", ID} struct table entries */
#define AS_LIST(name, ID) {#name, ID},
/** Register Enum */
enum ERROR {
/* Generate enum elements with X-Macro */
ERRORS(AS_ENUM)
ERROR_MAX
};
/**
* Error List Generated by X-Macro @ref AS_LIST.
*
* Works with non-contiguous sets.
*/
static const struct ErrorEntry {
char* name;
enum ERROR id;
} ERROR_LIST[] = {
ERRORS(AS_LIST)
};
void PrintAll(void)
{
size_t idx = 0;
for (idx = 0;
idx < sizeof(ERROR_LIST)/sizeof(ERROR_LIST[0]);
idx++)
{
const struct ErrorEntry* reg = &ERROR_LIST[idx];
printf("Error message 0x%x is \"%s\"\n", reg->id, reg->name);
}
}
int main(void)
{
PrintAll();
}