Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I have several hundred of #if defined(XXX) ... #endif in an easy-to-read table format?

How can I have several hundred of #if defined(XXX) ... #endif in an easy-to-read table format?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++asp-netcomdiscussion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    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); #endif

    As 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);
    #endif

    Can 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.

    L CPalliniC 2 Replies Last reply
    0
    • A arnold_w

      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); #endif

      As 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);
      #endif

      Can 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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What problem are you actually trying to resolve? The requirement for hundreds of #defines suggests something in your design needs looking at.

      A 1 Reply Last reply
      0
      • L Lost User

        What problem are you actually trying to resolve? The requirement for hundreds of #defines suggests something in your design needs looking at.

        A Offline
        A Offline
        arnold_w
        wrote on last edited by
        #3

        I'm trying to solve this: How should I manage my register addresses and expose some of them properly and ensure consistency? - C / C++ / MFC Discussion Boards[^]

        L 1 Reply Last reply
        0
        • A arnold_w

          I'm trying to solve this: How should I manage my register addresses and expose some of them properly and ensure consistency? - C / C++ / MFC Discussion Boards[^]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          A 1 Reply Last reply
          0
          • L Lost User

            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.

            A Offline
            A Offline
            arnold_w
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • A arnold_w

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • L Lost User

                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.

                A Offline
                A Offline
                arnold_w
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • A arnold_w

                  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); #endif

                  As 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);
                  #endif

                  Can 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.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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 both C and Lua programs.

                  In testa che avete, signor di Ceprano?

                  A 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    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 both C and Lua programs.

                    A Offline
                    A Offline
                    arnold_w
                    wrote on last edited by
                    #9

                    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?

                    CPalliniC 1 Reply Last reply
                    0
                    • A arnold_w

                      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?

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #10

                      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 the C++ 11 regex classes).

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups