Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > macro inside macro

Reply
Thread Tools

macro inside macro

 
 
Mike Manilone
Guest
Posts: n/a
 
      10-03-2011
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
 
Reply With Quote
 
 
 
 
Mike Manilone
Guest
Posts: n/a
 
      10-03-2011
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.
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      10-04-2011
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".

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-04-2011
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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      10-04-2011
William Ahern <> 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."
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-05-2011
On Tue, 2011-10-04, Ben Pfaff wrote:
> William Ahern <> 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 .
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-05-2011
Jorgen Grahn <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.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      10-05-2011
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.)

 
Reply With Quote
 
Mike Manilone
Guest
Posts: n/a
 
      10-06-2011


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments