473,791 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1675
On Sep 5, 1:46 pm, R Pradeep Chandran <s...@sig.below wrote:
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.beloww rote:
# 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.beloww rote:
# 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}_xy z, ;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
9139
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 <iostream> #include <string> using namespace std; #define B(X, Y) Y
205
10716
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
1580
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 topic! I just keep running into problems (Windows platform) because files are included in the wrong order, one include file messes up the other one etc.. I think I need to brush up on some basics here. In this example project, I have several cpp...
4
1965
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 cos(...) being calculated in my program every time. Is there a way to get the preprocessor to calculate the cosine and just replace COSTHETA with its final number? I know I could define a
4
1953
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
1624
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 now compile file1.c , make it's object file. and use this object file in the other file i.e file2.c. Then can we use the macro TEST1 in file2.c as well. As I understand about macros, they are replaced by their corresponding values , but are the...
0
1594
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 welcome... ;) http://appcore.home.comcast.net/appcore/src/ppmeta-recurse.c
5
15727
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 newlines in the preprocessed output file but I can't figure out how to do it. Here's a stab at what I'm trying to do but failing: #define CMD(s) text s \
36
2130
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) \ ({ *(77) = (val); }) \ #preprocessor_else_if (const_index == 1) \ ({ *(99) = (val); }) \
0
9669
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
10207
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
10154
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,...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5430
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
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.