4 ms·
I've started moving to using python to generate what I need a lot of the time now in C. I'll either make a '.h' file, read it in and generate the boilerplate I
by anitil 6y ago
I've started moving to using python to generate what I need a lot of the time now in C. I'll either make a '.h' file, read it in and generate the boilerplate I need, or just do it using native python and emitting the C I need.
I'm never again hand-writing an my_enum_to_str routine.
- Galanwe 6y agoI share your feelings, though some years ago I discovered the neat trick of "xmacros". In a nutshell: #define COLOR_LIST \ X( RED ) \ X( GREEN ) \ X( BLUE ) \ X( PURPLE ) enum Color { #define X(code) code, COLOR_LIST #undef X }; static const char* colorNames[] = { #define X(code) #code, COLOR_LIST #undef X }; You can even make it cleaner and store more metadata: // --- color.def X( RED, "Red", 0xFF0000 ) X( GREEN, "Green", 0x00FF00 ) X( BLUE, "Blue", 0x0000FF ) // --- color.h enum Color { #define X(code,name,mask) code, # include "color.def" #endif }; static const char* colorNames[] = { #define X(code,name,mask) name, # include "color.def" #undef X }; static unsigned int colorMasks[] = { #define X(code,name,mask) mask, # include "color.def" #undef X };
- tempodox 6y agoYep, good old trick. Just don't forget to #undef X when you're done.