473,385 Members | 1,400 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,385 software developers and data experts.

Preprocessor - Generate enumeration from two macros

Hi All,

Short version:
Is it possible to write macros to expand the following

DATAGROUP(abc)
ELEMENT(xyz)
ENDDATAGROUP

to

enum dg_abc {
el_abc_xyz,
};

using macro definitions that comply with the ISO standard?

Long version:

I have a set of legacy code which I have to modify and it uses a set of
macros as shown below to generate some data types and data structures.

DATAGROUP(abc)
ELEMENT(xyz)
ENDDATAGROUP

One of the data types generated will be an enumeration. For the above
example, it will be

enum dg_abc {
el_xyz,
};

So far, everything works OK. Till now, there was only one data group and
the element names are unique. Now, there is a need to add more than one
data group with the option to use the same element name in more than one
data group. As an example,

DATAGROUP(abc)
ELEMENT(xyz)
ENDDATAGROUP

DATAGROUP(def)
ELEMENT(xyz)
ENDDATAGROUP

The problem is that with the current macros, the types will be generated
as below.

enum dg_abc {
el_xyz,
};

enum dg_def {
el_xyz,
};

This will result in a compilation error. It would be great if I can avoid
this by prefixing the name of the data group to the enumeration constants
as shown in the short version of the question. Any ideas?

If something like this doesn't work, I am planning to redesign the
configuration macros. But, this would mean changing a lot of existing code
and I would prefer to avoid that.

Have a great day,
Pradeep
--
If you really have to reply by email use rpc AT pobox DOT com
Sep 5 '07 #1
3 1655
On Sep 5, 1:46 pm, R Pradeep Chandran <s...@sig.belowwrote:
Hi All,

Short version:
Is it possible to write macros to expand the following

DATAGROUP(abc)
ELEMENT(xyz)
ENDDATAGROUP

to

enum dg_abc {
el_abc_xyz,

};

using macro definitions that comply with the ISO standard?
No.

But you can write compliant macros to generate that code if you do
something like

#define GROUPNAME abc
DATAGROUP(abc)
ELEMENT(xyz)
ENDDATAGROUP
#undef GROUPNAME

Of course, you could (should) remove or ignore the parameter to
DATAGROUP, since it is redundant and a possible source of bugs.

Implementation is left as an exercise for the reader.

Regards,

-=Dave

Sep 5 '07 #2
R Pradeep Chandran <se*@sig.belowwrote:
# Hi All,
#
# Short version:
# Is it possible to write macros to expand the following
#
# DATAGROUP(abc)
# ELEMENT(xyz)
# ENDDATAGROUP
#
# to
#
# enum dg_abc {
# el_abc_xyz,
# };

You can with a better macro processor than cpp. It's your
decision whether to stick with cpp and cope with it, or
add another step to your compilation.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore.
Sep 5 '07 #3
SM Ryan wrote:
R Pradeep Chandran <se*@sig.belowwrote:
# Hi All,
#
# Short version:
# Is it possible to write macros to expand the following
#
# DATAGROUP(abc)
# ELEMENT(xyz)
# ENDDATAGROUP
#
# to
#
# enum dg_abc {
# el_abc_xyz,
# };

You can with a better macro processor than cpp. It's your
decision whether to stick with cpp and cope with it, or
add another step to your compilation.
Indeed so.
<AOT>
Of them, Unimal preserves the look-and-feel of the code, like so:
#MP Setstr DG="abc"
enum dg_#mp%sDG { ;expands to enum dg_abc {
el_#mp{%sDG}_xyz, ;expands to el_abc_xyz,
};
#MP Setstr DG="def"
....................
Or you can wrap the whole thing in a macro to invoke like
#MP my_enum(abc, zyx, uvw)
</AOT>

--
Ark
"As always, sorry for shameless self-promotion"
Sep 6 '07 #4

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

Similar topics

25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
12
by: Florian | last post by:
Hi, I'm hoping that someone can point me into the right direction here. I'm trying to gain a bit of a better understanding on how the preprocessor works - something that seems like a simple...
4
by: Jay Wolfe | last post by:
Hi, If I define a macro as follows #define COSTHETA cos(45.0*3.14159256/180.0) I believe that any time COSTHETA appears it is simply replaced by the defined text, which results in the...
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
2
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We...
0
by: Chris Thomasson | last post by:
Here is a "simple" method for using the preprocessor to generate code via. recursion... What do you think of my experimental recursion implementation? Can you compile it? Any comments are...
5
by: John Speth | last post by:
Hi Group- I want to use the C preprocessor to generate expanded text as a text processor for software test script generation. The preprocessor output will never be compiled. I need to insert...
36
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index ==...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.