Last active
October 14, 2020 16:47
-
-
Save VehpuS/a9bcb3bd31774593e4cbd9b96298ebcd to your computer and use it in GitHub Desktop.
Expand as macros pattern demonstration in C - https://repl.it/@VehpuS/Expand-as-macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdbool.h> | |
#define NAME_ID_LIST(ENTRY) \ | |
ENTRY(MOSHE, 0, true) \ | |
ENTRY(DROR, 1, false) | |
#define NAME_ID_LIST_EXPAND_AS_ENUM(enum_name, enum_value, ...) enum_name = enum_value, | |
typedef enum name_id { | |
NAME_ID_LIST(NAME_ID_LIST_EXPAND_AS_ENUM) | |
NAME_ID_LAST, | |
} name_id_t; | |
/* | |
Expansion of NAME_ID_LIST(NAME_ID_LIST_EXPAND_AS_ENUM) | |
====================================================== | |
Step 1: | |
NAME_ID_LIST_EXPAND_AS_ENUM(MOSHE, 0, true) | |
NAME_ID_LIST_EXPAND_AS_ENUM(DROR, 1, false) | |
Step 2: | |
MOSHE = 0 | |
DROR = 1 | |
*/ | |
#define NAME_ID_LIST_EXPAND_AS_BOOL(bool_name, _, bool_value) bool BOOL_ ## bool_name = bool_value; | |
NAME_ID_LIST(NAME_ID_LIST_EXPAND_AS_BOOL) | |
/* | |
bool BOOL_MOSHE = true; | |
bool BOOL_DROR = false; | |
*/ | |
int main(void) { | |
printf("enum MOSHE = 0 ? %s\n", MOSHE == 0 ? "true" : "false"); | |
printf("enum DROR = 1 ? %s\n", DROR == 1 ? "true" : "false"); | |
printf("bool BOOL_MOSHE = true ? %s\n", BOOL_MOSHE == true ? "true" : "false"); | |
printf("enum BOOL_DROR = false ? %s\n", BOOL_DROR == false ? "true" : "false"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment