How can I have several hundred of #if defined(XXX) ... #endif in an easy-to-read table format?
-
This question is somewhat related to the question Passing a "value", that was either #defined or #undef, into a macro and check if it was defined? - C / C++ / MFC Discussion Boards[^] , but the core question in this thread is quite different than that of the other thread so I think this should be a separate thread. In the other thread I was looking to have a macro do my checks, but it would be ok to do it without a macro as well. The important thing is that the checks (there are several hundreds of them) are nicely formatted in way that makes it easy to read. So, is it possible to achieve something like this with some clever workaround:
#if defined(ADDR1) && defined(ADDR2) _Static_assert(ADDR1==ADDR2, #ADDR1 " is not equal to " #ADDR2); #endif
#if defined(ADDR3) && defined(ADDR4) _Static_assert(ADDR3==ADDR4, #ADDR3 " is not equal to " #ADDR4); #endif
#if defined(ADDR5) && defined(ADDR6) _Static_assert(ADDR5==ADDR6, #ADDR5 " is not equal to " #ADDR6); #endif
#if defined(ADDR7) && defined(ADDR8) _Static_assert(ADDR7==ADDR8, #ADDR7 " is not equal to " #ADDR8); #endifAs you can see, the "correct" way of doing this (the way the compiler wants it) is not easy to read at all (and not so easy to generate in Excel or edit using the block caret feature in Eclipse):
#if defined(ADDR1) && defined(ADDR2)
_Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);
#endif
#if defined(ADDR3) && defined(ADDR4)
_Static_assert(ADDR3 == ADDR4, #ADDR3 " is not equal to " #ADDR4);
#endif
#if defined(ADDR5) && defined(ADDR6)
_Static_assert(ADDR5 == ADDR6, #ADDR5 " is not equal to " #ADDR6);
#endif
#if defined(ADDR7) && defined(ADDR8)
_Static_assert(ADDR7 == ADDR8, #ADDR7 " is not equal to " #ADDR8);
#endifCan anyone think of a neat trick? Maybe something involving #include files? I think I've read somewhere that you can have several #include on the same line.
-
This question is somewhat related to the question Passing a "value", that was either #defined or #undef, into a macro and check if it was defined? - C / C++ / MFC Discussion Boards[^] , but the core question in this thread is quite different than that of the other thread so I think this should be a separate thread. In the other thread I was looking to have a macro do my checks, but it would be ok to do it without a macro as well. The important thing is that the checks (there are several hundreds of them) are nicely formatted in way that makes it easy to read. So, is it possible to achieve something like this with some clever workaround:
#if defined(ADDR1) && defined(ADDR2) _Static_assert(ADDR1==ADDR2, #ADDR1 " is not equal to " #ADDR2); #endif
#if defined(ADDR3) && defined(ADDR4) _Static_assert(ADDR3==ADDR4, #ADDR3 " is not equal to " #ADDR4); #endif
#if defined(ADDR5) && defined(ADDR6) _Static_assert(ADDR5==ADDR6, #ADDR5 " is not equal to " #ADDR6); #endif
#if defined(ADDR7) && defined(ADDR8) _Static_assert(ADDR7==ADDR8, #ADDR7 " is not equal to " #ADDR8); #endifAs you can see, the "correct" way of doing this (the way the compiler wants it) is not easy to read at all (and not so easy to generate in Excel or edit using the block caret feature in Eclipse):
#if defined(ADDR1) && defined(ADDR2)
_Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);
#endif
#if defined(ADDR3) && defined(ADDR4)
_Static_assert(ADDR3 == ADDR4, #ADDR3 " is not equal to " #ADDR4);
#endif
#if defined(ADDR5) && defined(ADDR6)
_Static_assert(ADDR5 == ADDR6, #ADDR5 " is not equal to " #ADDR6);
#endif
#if defined(ADDR7) && defined(ADDR8)
_Static_assert(ADDR7 == ADDR8, #ADDR7 " is not equal to " #ADDR8);
#endifCan anyone think of a neat trick? Maybe something involving #include files? I think I've read somewhere that you can have several #include on the same line.
-
What problem are you actually trying to resolve? The requirement for hundreds of #defines suggests something in your design needs looking at.
-
-
I don't see the problem. Use the same defined name in all your code, then you do not need to care. If it's not defined then the compiler will complain.
If I use the same defines both in my local code and in the shared h-file then I get compiler warnings, "REGISTER_NAME" redefined. I need to include the shared h-file in my own project as well because in addition to addresses, it also contains structs and enums that contains information about what the data in the different registers mean. If I get rid of my local file (the comprehensive file with ALL register addresses) then I expose all the registers and I don't want that, I only want to expose a subset. Maybe I should move my structs and enums to a separate shared h-file and ensure that (through special inclusion guards) my local and the shared h-file are never included into the same file, but I would prefer to keep everything (addresses, structs and enums) in the same file.
-
If I use the same defines both in my local code and in the shared h-file then I get compiler warnings, "REGISTER_NAME" redefined. I need to include the shared h-file in my own project as well because in addition to addresses, it also contains structs and enums that contains information about what the data in the different registers mean. If I get rid of my local file (the comprehensive file with ALL register addresses) then I expose all the registers and I don't want that, I only want to expose a subset. Maybe I should move my structs and enums to a separate shared h-file and ensure that (through special inclusion guards) my local and the shared h-file are never included into the same file, but I would prefer to keep everything (addresses, structs and enums) in the same file.
-
You can use #define statements in each source module to include only certain sections of the headers, which is the standard way of doing it. Take a look at any of the Windows header files and you will see examples.
The registers that should be exposed are not contiguous, they appear quite randomly in the address space. Currently, they are ordered in ascending order of the address and it's too late in the development phase to re-number the addresses according to whether they are exposed or not. So, adding a lot of #ifdefs would pollute the code quite a bit and make it much harder to read. I'm a firm believer in having code that easy to read for the human eye.
-
This question is somewhat related to the question Passing a "value", that was either #defined or #undef, into a macro and check if it was defined? - C / C++ / MFC Discussion Boards[^] , but the core question in this thread is quite different than that of the other thread so I think this should be a separate thread. In the other thread I was looking to have a macro do my checks, but it would be ok to do it without a macro as well. The important thing is that the checks (there are several hundreds of them) are nicely formatted in way that makes it easy to read. So, is it possible to achieve something like this with some clever workaround:
#if defined(ADDR1) && defined(ADDR2) _Static_assert(ADDR1==ADDR2, #ADDR1 " is not equal to " #ADDR2); #endif
#if defined(ADDR3) && defined(ADDR4) _Static_assert(ADDR3==ADDR4, #ADDR3 " is not equal to " #ADDR4); #endif
#if defined(ADDR5) && defined(ADDR6) _Static_assert(ADDR5==ADDR6, #ADDR5 " is not equal to " #ADDR6); #endif
#if defined(ADDR7) && defined(ADDR8) _Static_assert(ADDR7==ADDR8, #ADDR7 " is not equal to " #ADDR8); #endifAs you can see, the "correct" way of doing this (the way the compiler wants it) is not easy to read at all (and not so easy to generate in Excel or edit using the block caret feature in Eclipse):
#if defined(ADDR1) && defined(ADDR2)
_Static_assert(ADDR1 == ADDR2, #ADDR1 " is not equal to " #ADDR2);
#endif
#if defined(ADDR3) && defined(ADDR4)
_Static_assert(ADDR3 == ADDR4, #ADDR3 " is not equal to " #ADDR4);
#endif
#if defined(ADDR5) && defined(ADDR6)
_Static_assert(ADDR5 == ADDR6, #ADDR5 " is not equal to " #ADDR6);
#endif
#if defined(ADDR7) && defined(ADDR8)
_Static_assert(ADDR7 == ADDR8, #ADDR7 " is not equal to " #ADDR8);
#endifCan anyone think of a neat trick? Maybe something involving #include files? I think I've read somewhere that you can have several #include on the same line.
As an alternative, you could automatically generate (e.g.
pre-build
step) both the correct header and its pretty representation (e.g. human readable file) starting from a sigle configuration file. I use a similar approach to share constant values in bothC
andLua
programs. -
As an alternative, you could automatically generate (e.g.
pre-build
step) both the correct header and its pretty representation (e.g. human readable file) starting from a sigle configuration file. I use a similar approach to share constant values in bothC
andLua
programs. -
Yes, if worse comes to worse I will do this. Is there source code available somewhere for a "preprocessor light" that can automatically detect #defines that are evaluated to numbers?
Very probably there is, but, unfortunately, I am not aware of. I do use a little
Lua
script for the purpose.Lua
features powerful string manipulation functions and it is a very enjoyable scripting language. You might also use a more popular scripting language, like, for instance,Python
(another option is, if you like it, using theC++ 11 regex
classes).