473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8483
Alexander Ulyanov <ua*@urmail.r u> 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(bl ock,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(re peat_until(y=f( y);,y>0),x>0);
(4); repeat_until({i nt x,y; x=h(x,y);},z);
(5); repeat_until(in t x,y; x=h(x,y);,z);
(6); repeat_until(in t 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*****@tang o-sierra-oscar-foxtrot-tango.fake.org> wrote:
Alexander Ulyanov <ua*@urmail.r u> 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(bl ock,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(re peat_until(y=f( y);,y>0),x>0);
(4); repeat_until({i nt x,y; x=h(x,y);},z);
(5); repeat_until(in t x,y; x=h(x,y);,z);
(6); repeat_until(in t 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.r u> 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
2053
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()() # succeeds if x in
699
34119
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
20
5937
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 making comparisons. If you have a file, in Python you could do:
242
13438
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
0
1749
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 and the module as well as where I would like the macros to run. Any input on how to make this work would be appreciated. This would automate a former 3 step process into one process.
6
2082
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 or the illegal chars for the insert command is it possible to encasulate the text/string so the server doesnt reads the string as a command?
9
35461
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 into the queries but on running the macro I have to enter each of the dates 3 times - once for each query. Is there an easy way to ask for the parameters once and for these to be passed onto each query?
12
3228
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 Test1 {
9
7714
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) puts("foo: x=\"" #x "\"")
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.