Sign in to follow this  
the_edd

code duplication in variadic/non-variadic macro pair

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this