Jump to content
  • Advertisement
Sign in to follow this  
the_edd

code duplication in variadic/non-variadic macro pair

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a bunch of macros that look something like this:
#if defined(TEST_O_MATIC_USE_VARIADIC_MACROS)
    #define TESTFIX(strliteral, ...) 
        namespace { namespace test_o_matic_defs_ { 
            struct TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) : __VA_ARGS__ 
            { 
                TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) (::test_o_matic::logger &); 
                static void tom_func_(::test_o_matic::logger &lgr) { TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) instance_(lgr); } 
            }; 
            ::test_o_matic::test 
                TEST_O_MATIC_TOKEN_JOIN(tst_, __LINE__) 
                (strliteral, TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__)::tom_func_, __FILE__, __LINE__, __DATE__, __TIME__); 
        } } 
        test_o_matic_defs_::TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__):: 
        TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__)(test_o_matic::logger &local_tom_logger_)
#else
    #define TESTFIX(strliteral, fixclass) 
        namespace { namespace test_o_matic_defs_ { 
            struct TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) : fixclass 
            { 
                TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) (::test_o_matic::logger &); 
                static void tom_func_(::test_o_matic::logger &lgr) { TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__) instance_(lgr); } 
            }; 
            ::test_o_matic::test 
                TEST_O_MATIC_TOKEN_JOIN(tst_, __LINE__) 
                (strliteral, TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__)::tom_func_, __FILE__, __LINE__, __DATE__, __TIME__); 
        } } 
        test_o_matic_defs_::TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__):: 
        TEST_O_MATIC_TOKEN_JOIN(class_, __LINE__)(test_o_matic::logger &local_tom_logger_)
#endif

As you can see, there's a version of TESTFIX() for compilers supporting C99-style variadic macros and a version for compilers that don't support them. There's also a huge amount of code duplication that I'd really like to get rid of. I can trim off a few lines here or there by pulling out the parts that don't mention macro arguments, but that would obfuscate things, I think. I'm hoping there's a way in that I can write a macro once and then have two very thin wrappers for the variadic/non-variadic cases, but I can't see anything that helps in C99? EDIT: backslashes removed from multi-line macro definitions as it was messing with the formatting of the post :(

Share this post


Link to post
Share on other sites
Advertisement
Have you considered just writing a Python script to generate the necessary source files or something?

Quote:
Original post by the_edd
EDIT: backslashes removed from multi-line macro definitions as it was messing with the formatting of the post :(


If you add a space after each backslash, the backslashes will show up properly. It won't be copy-and-pasteable, but it will at least *look* right.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Have you considered just writing a Python script to generate the necessary source files or something?


I hadn't! That's probably the best I'm going to be able to do by the looks of things.

Quote:

Quote:
Original post by the_edd
EDIT: backslashes removed from multi-line macro definitions as it was messing with the formatting of the post :(


If you add a space after each backslash, the backslashes will show up properly. It won't be copy-and-pasteable, but it will at least *look* right.


Ah, thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!