macro inside macro

Discussion in 'C Programming' started by Mike Manilone, Oct 3, 2011.

  1. Hi there,

    I often write app with GObject.

    Well, of course, GObject is hard to use.
    So I thought about making a set of macros to make this job easier.

    Just like this (assume the class is 'GtkButton'):
    #define NAME GTK
    #define SPACE BUTTON
    #define Type GtkButton
    #define name_space gtk_button

    static void
    name_space ## _init (Type *self)
    {
    /* do something */
    }

    Well, GObject always needs some macros to do something usual. So can I
    write these macros like this?
    #define NAME##_IS_##SPACE ...
    #define NAME##_IS_##SPACE##CLASS ...

    Maybe I need a try. But my laptop is broken these days.
    So, could someone please tell me what happens?

    Best Regards,
    Mike Manilone
    Mike Manilone, Oct 3, 2011
    #1
    1. Advertising

  2. Hi,

    On 2011-10-4 0:36, China Blue Corn Chips wrote:
    > #!/usr/bin/tclsh
    >
    > if {[llength $argv]} {
    > set gofile [open [lindex $argv 0] r]
    > set cfile [open [file root [lindex $argv 0]].c w]
    > } else {
    > set gofile stdin
    > set cfile stdout
    > }
    > while {[gets $gofile line]>=0} {
    > if {[regexp {^\s*#\s*namespace\s+(\S+)\s+(\S+)\s*\x7B\s*$} $line - name
    > space]} {
    > set name [string tolower $name]
    > set Name [string totitle $name]
    > set NAME [string toupper $name]
    > set space [string tolower $space]
    > set Space [string totitle $space]
    > set SPACE [string toupper $space]
    > puts $cfile "
    > #undef NAME\n#define NAME $NAME
    > #undef SPACE\n#define SPACE $SPACE
    > #undef Type\n#define Type ${Name}${Space}
    > #undef name_space\n#define name_space ${name}_${space}
    >
    > #define ${NAME}_IS_${SPACE} ...
    > #define ${NAME}_IS_${SPACE}CLASS ...
    > ...
    >
    > static void\n${name}_${space}_init (Type *self) \x7B"
    > } elseif {[regexp {^\s*#\s*method\s+(.*)\s+(\w+)(\s*\()(.*)$} $line - type
    > opname etc]} {
    > puts $cfile "static $type ${name}_${space}_${opname}${etc}"
    > } else {
    > puts $cfile $line
    > }
    > }
    >


    This script looks very nice.
    I'll try it later.

    Thank you very much!

    Best Regards,
    Mike Manilone.
    Mike Manilone, Oct 3, 2011
    #2
    1. Advertising

  3. Mike Manilone

    Nobody Guest

    On Mon, 03 Oct 2011 09:36:33 -0700, China Blue Corn Chips wrote:

    >> So I thought about making a set of macros to make this job easier.

    >
    > Do you have m4 available? It's much easier to do complicated macros with
    > m4 than beat your head against cpp.


    While this is true, a counterpoint is that cpp was designed to work with
    C, while m4 wasn't, so using m4 will be a case of "get worse before it
    gets better".
    Nobody, Oct 4, 2011
    #3
  4. Mike Manilone

    Eric Sosman Guest

    On 10/3/2011 10:08 AM, Mike Manilone wrote:
    > Hi there,
    >
    > I often write app with GObject.
    >
    > Well, of course, GObject is hard to use.


    Makes one wonder why you "often" choose to torture yourself ...

    > So I thought about making a set of macros to make this job easier.
    >
    > Just like this (assume the class is 'GtkButton'):
    > #define NAME GTK
    > #define SPACE BUTTON
    > #define Type GtkButton
    > #define name_space gtk_button
    >
    > static void
    > name_space ## _init (Type *self)


    Um, er, no. The ## operator is only defined within a macro
    replacement list, not in "open code."

    > {
    > /* do something */
    > }
    >
    > Well, GObject always needs some macros to do something usual.


    Sorry; I don't understand this.

    > So can I
    > write these macros like this?
    > #define NAME##_IS_##SPACE ...
    > #define NAME##_IS_##SPACE##CLASS ...


    No; same reason: You're using ## outside a macro expansion.

    > Maybe I need a try. But my laptop is broken these days.
    > So, could someone please tell me what happens?


    Nothing good. ;) I'm not familiar with GObject, but I get the
    sense that you're not so much using it as fighting it. Perhaps it
    would be helpful to read code written by GObject experts; this is
    often a good way to pick up a framework's idioms and best practices.

    --
    Eric Sosman
    d
    Eric Sosman, Oct 4, 2011
    #4
  5. Mike Manilone

    Ben Pfaff Guest

    William Ahern <william@wilbur.25thandClement.com> writes:

    > Valid counterpoints regarding M4 might be that it's a fully recursive
    > text-replacement language which can be hard to mentally parse and to spot
    > undesirable side-effects, and that it's no longer commonly used so it may be
    > difficult for others to work on initially.


    It's pretty widely used as part of Autoconf so it may not be
    *that* hard to find people who've used it.
    --
    Peter Seebach on C99:
    "[F]or the most part, features were added, not removed. This sounds
    great until you try to carry a full-sized printout of the standard
    around for a day."
    Ben Pfaff, Oct 4, 2011
    #5
  6. Mike Manilone

    Jorgen Grahn Guest

    On Tue, 2011-10-04, Ben Pfaff wrote:
    > William Ahern <william@wilbur.25thandClement.com> writes:
    >
    >> Valid counterpoints regarding M4 might be that it's a fully recursive
    >> text-replacement language which can be hard to mentally parse and to spot
    >> undesirable side-effects, and that it's no longer commonly used so it may be
    >> difficult for others to work on initially.

    >
    > It's pretty widely used as part of Autoconf so it may not be
    > *that* hard to find people who've used it.


    On the other hand, lots of people (including me) fear Autoconf, so
    this relationship may not be good PR for m4.

    Sendmail's configuration is also m4-based, by the way ;-)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Oct 5, 2011
    #6
  7. Mike Manilone

    Stefan Ram Guest

    Jorgen Grahn <> writes:
    >On the other hand, lots of people (including me) fear Autoconf, so
    >this relationship may not be good PR for m4.


    Last time I looked at it, there was no native Autoconf for
    Microsoft® Windows, so Autoconf is not really helpful to
    port Autoconf software to Windows, even though sometimes
    there might be a certain combination of macro definitions
    that would allow autoconfed source code to compile and run
    under Microsoft® Windows.

    Since most UNIX tools (such as m4) have already be ported to
    native Microsoft® Windows (not only to Cygwin), it should be
    possible to port Autoconf, too.

    Fear might not be the right word, but without Autoconf for
    Microsoft® Windows, porting Autoconfed software comes down
    to manually »implement« Autoconf, which is no real pleasure
    either.
    Stefan Ram, Oct 5, 2011
    #7
  8. Mike Manilone

    Stefan Ram Guest

    Mike Manilone <> writes:
    >write these macros like this?
    > #define NAME##_IS_##SPACE ...
    > #define NAME##_IS_##SPACE##CLASS ...


    A prepreprocessor can be used as in the following example:

    @define i include
    #i <stdio.h>
    int main( void ){ printf( "alpha\n" ); }

    . Now,

    tr # \` < main.c | tr @ # | cpp | tr \` #

    gives

    #include <stdio.h>
    int main( void ){ printf( "alpha\n" ); }

    . (This use of »tr« assumes that »#«, »@«, and »`« are not
    used in the rest of the program, otherwise use »sed« to
    replace them only at the start of a line.)
    Stefan Ram, Oct 5, 2011
    #8
  9. On 2011-10-5 21:28, Stefan Ram wrote:
    > Microsoft® Windows, so Autoconf is not really helpful to
    > port Autoconf software to Windows

    MSYS and Cygwin can both run the `configure' script and generate
    Makefile. In most cases, they can build correct binaries.

    So, we can generate the `configure' on UNIX then run it on Windows.
    However, most Windows programmers won't use AutoTools, huh?


    Best Regards,
    Mike Manilone
    Mike Manilone, Oct 6, 2011
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,097
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    570
    Jack Klein
    Sep 21, 2003
  3. Miki Tebeka

    # inside a macro body

    Miki Tebeka, Sep 27, 2004, in forum: C Programming
    Replies:
    6
    Views:
    444
    S.Tobias
    Sep 28, 2004
  4. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    489
    Mark McIntyre
    Nov 22, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    462
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page