[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  X-Macro - conditional extern generation
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

X-Macro - conditional extern generation

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


Posted by Nick Cash   USA  (626 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] 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
[Go to top] 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.


5,015 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]