473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Code blocks as macro parameters

Hi all.

Is it possible to pass the whole blocks of code (possibly including
" and ,) as macro parameters?

I want to do something like:

MACRO(FOO, "Foo",
"return "Foobar";",
"foo(); bar();")

(just an example, obviously this won't compile)

being parsed to:

1.
P_FOO, /* Part of enum */
2.
"FOO", /* Part of string literal array */
3.
"Foo", /* Part of string literal array */
4.
case P_FOO: /* Part of switch */
return "Foobar";
break;
5.
case P_FOO: /* Part of switch */
foo(); bar();
break;

Depending on some #define, of course. I don't want to edit five
source files to add or remove something; keeping them consistent
is difficult.

I'm currently doing this with a m4 macro and it works fine, but I'd
like to avoid using an extra tool (which may not be available on
Windows or other non-UNIX systems).

--
Alexander Ulyanov, maintainer of PosBand roguelike
E-mail: uav AT urmail DOT ru Web: http://orthanc.chat.ru/pos/
"...And his name is Melkor, Lord of All, Giver of Freedom, and he shall
make you stronger that they." -- Akallabeth, the Downfall of Numenor
Nov 14 '05 #1
3 8446
Alexander Ulyanov <ua*@urmail.ru> wrote:
# Hi all.
#
# Is it possible to pass the whole blocks of code (possibly including
# " and ,) as macro parameters?

cat <<':eof' >/tmp/xx.c
#define repeat_until(block,predicate) do {block} while (!(predicate)
(1); repeat_until(x = f(x);,x>0);
(2); repeat_until(x = f(y,"b"); y = g(x,"a");,x>0);
(3); repeat_until(repeat_until(y=f(y);,y>0),x>0);
(4); repeat_until({int x,y; x=h(x,y);},z);
(5); repeat_until(int x,y; x=h(x,y);,z);
(6); repeat_until(int x; int y; x=h(x,y);,z);
:eof
cc -E /tmp/xx.c

The preprocessor is only looking for comma and parentheses. Parentheses
have to be balanced and commas outside of parentheses are argument
separators. You can almost always avoid commas: comma expressions
can be parenthesised, and joined declarations (like int x,y;) can be
separated (int x; int y;).

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Quit killing people. That's high profile.
Nov 14 '05 #2
On 2004-08-20, SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
Alexander Ulyanov <ua*@urmail.ru> wrote:
# Hi all.
#
# Is it possible to pass the whole blocks of code (possibly including
# " and ,) as macro parameters?

cat <<':eof' >/tmp/xx.c
#define repeat_until(block,predicate) do {block} while (!(predicate)
(1); repeat_until(x = f(x);,x>0);
(2); repeat_until(x = f(y,"b"); y = g(x,"a");,x>0);
(3); repeat_until(repeat_until(y=f(y);,y>0),x>0);
(4); repeat_until({int x,y; x=h(x,y);},z);
(5); repeat_until(int x,y; x=h(x,y);,z);
(6); repeat_until(int x; int y; x=h(x,y);,z);
:eof
cc -E /tmp/xx.c

The preprocessor is only looking for comma and parentheses. Parentheses
have to be balanced and commas outside of parentheses are argument
separators. You can almost always avoid commas: comma expressions
can be parenthesised, and joined declarations (like int x,y;) can be
separated (int x; int y;).


Thanks!

--
Alexander Ulyanov, maintainer of PosBand roguelike
E-mail: uav AT urmail DOT ru Web: http://orthanc.chat.ru/pos/
"...And his name is Melkor, Lord of All, Giver of Freedom, and he shall
make you stronger that they." -- Akallabeth, the Downfall of Numenor
Nov 14 '05 #3
Alexander Ulyanov <ua*@urmail.ru> wrote:
Hi all.

Is it possible to pass the whole blocks of code (possibly including
" and ,) as macro parameters?

I want to do something like:

MACRO(FOO, "Foo",
"return "Foobar";",
"foo(); bar();")

(just an example, obviously this won't compile)
Regarding the code snippets, you can use an expression in brackets:
( foo(), bar() )
My experience is that using a single expression is preferable
to a set of statements, if possible.

In this case it is possible to avoid having the "return" as part
of the macro definition (see below).
being parsed to:

1.
P_FOO, /* Part of enum */
2.
"FOO", /* Part of string literal array */
3.
"Foo", /* Part of string literal array */
4.
case P_FOO: /* Part of switch */
return "Foobar";
break;
No need to 'break' if you have just returned :)
5.
case P_FOO: /* Part of switch */
foo(); bar();
break;

Depending on some #define, of course. I don't want to edit five
source files to add or remove something; keeping them consistent
is difficult.


Obviously you can't invoke the definition once and then have it copy
the code to various places; you will have to define a new meaning
for the macro when you want to use it. If the "calls" to MACRO()
are in "macro.h" then you could go:

enum {
#define MACRO(a,b,c,d) P_##a,
#include "macro.h"
#undef MACRO
};
char const *foo1 = {
#define MACRO(a,b,c,d) #a,
#include "macro.h"
#undef MACRO
NULL /* trailing commas are not portable */
};

switch(bar) {
#define MACRO(a,b,c,d) case P_##a: d; return c;
#include "macro.h"
#undef MACRO
};

etc.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Useless example first: def foo(x): a = suite: y = 1 b = suite: y = 2 a() # equivalent effect to local y = 1 above (a,b)() # effect of suite b if bool(x), otherwise suite a vars()() #...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
by: jpodesta | last post by:
Hello- I am fairly new to MS Access and would like to use some macros in .xls in an Access Module. I have tried to do this on my own but failed to make it work. I have included the xls macros...
6
by: Lelle | last post by:
Hello ! how can i insert text containg code examples from a textbox into a database using SQL insert statment. i have no problem to just add text that dont contains code and script examples...
9
by: skinnybloke | last post by:
Hi - I have 3 access queries which I run via 1 macro. Each of the queries now requires 2 parameters when they the run. The parameters are start and end dates. I have built the parameters...
12
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class...
9
by: Two-Horned Unicorn | last post by:
The following code compiles and works as expected when compiled with gcc. /* empty-args.c */ #include <stdio.h> #define foo(x,y) puts("bar: x=\"" #x "\"; y=\"" #y "\"") #define bar(x) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.