Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Programming ➜ General ➜ X-Macro - conditional extern generation

X-Macro - conditional extern generation

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Cash   USA  (626 posts)  Bio
Date Mon 23 Feb 2009 04:02 PM (UTC)
Message
Hello all!

I am working on a system in C to auto-generate some tables for me based off of some information I have in a header file. I have everything working just fine, except for this part. I need to get it to generate extern prototypes, but only if a callback function is present. If I put NULL in the macro definition in the other file I don't want anything to be generated. An example of what I'm doing is below:


#define MACRO(NAME, CB) extern void CB( int id, int value );
   #include "Handler_Definitions.h"
#undef MACRO


And in Handler_Definitions.h would be:

MACRO(Object1, Generic_Callback)
MACRO(Object2, NULL)


How can I make it conditional? I haven't found any way to get the preprocessor to make it work the way I want. The macro for Object1 works just fine, but quite obviously Object2 will break things.

~Nick Cash
http://www.nick-cash.com
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 23 Feb 2009 06:55 PM (UTC)
Message
I'm not sure you can do it. A first pass that scans the appropriate file using Perl, Lua, or some other similar language, and converts "Handler_Definitions.h" appropriately might be your solution.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Mon 23 Feb 2009 09:01 PM (UTC)
Message
I don't know how to put macros within macros -- that is, how to define a macro that uses more preprocessor definitions like #if statements inside the macro -- or even if it is indeed possible in the first place.

How is the .h file generated? If you know that you're putting in a NULL, can you not just put in nothing instead, or even omit the line entirely?

It might be easier to do as Nick suggested and write a simple filter in Perl that does this for you.

Actually, here is one that does just what you want:


#!/usr/bin/perl

while (<STDIN>) {
    if (/MACRO\((.*), (.*)\)/) {
        if ($2 ne "NULL") {
            print "extern void $2(int id, int value);\n"
        }
    } else {
        print;
    }
}



Running it:


$ cat Handler_Definitions.h

some_line;

MACRO(Object1, Generic_Callback)
MACRO(Object2, NULL)

some_other_line;

$ cat Handler_Definitions.h| perl filter.pl

some_line;

extern void Generic_Callback(int id, int value);

some_other_line;

$

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #3 on Wed 25 Feb 2009 02:17 AM (UTC)
Message
Thanks for the Perl script. However, we decided we didn't want another step in the process. The .h file with all of the macro definitions is created by hand, so we just made a second macro that doesn't provide a callback.

~Nick Cash
http://www.nick-cash.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,731 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.