3 ms·
Code generation with X-Macros in C
- smithjchris 17y agoC can be loosely typed. Look at qsort for example. void pointers all around and a comparer function pointer. It just wraps the quick sort algorithm. If you design with flexibility in mind, you don't need code generation.
- sown 17y agoWe have these in our main product at work. They are powerful and very useful but they can get out of hand and be difficult to debug or add code to.
- DenisM 17y agoI have a better idea that does not require an extra include file: #define TASK_STATUS_ENUM(VALUE) ts##VALUE #define TASK_STATUS_ENUM_COMA(VALUE) TASK_STATUS_ENUM(VALUE) , #define TASK_STATUS_VALUES(_) _(Normal) _(ModifiedLocally) _(CreatedLocally) enum TaskStatus { TASK_STATUS_VALUES(TASK_STATUS_ENUM_COMA) }; #define STRINGIFY_VALUE(v) "ts" #v #define ENUM_TO_STR(x) if (status == TASK_STATUS_ENUM(x)) return STRINGIFY_VALUE(x); char* enumToStr(TaskStatus status) { TASK_STATUS_VALUES(ENUM_TO_STR); return STRINGIFY_VALUE(Normal); }