macro inside macro

M

Mike Manilone

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
 
M

Mike Manilone

Hi,

#!/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.
 
N

Nobody

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".
 
E

Eric Sosman

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

Ben Pfaff

William Ahern said:
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.
 
J

Jorgen Grahn

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
 
S

Stefan Ram

Jorgen Grahn said:
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.
 
S

Stefan Ram

Mike Manilone said:
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.)
 
M

Mike Manilone

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

macro chain 4
reasoning for a macro 14
Macro Mystery ... help? 4
macro expansion 2
Macro expansion in several lines 3
macro FAQ 22
Endianness macros 48
Macro that allocates storage and "returns" value 12

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top