Preprocessor - Generate enumeration from two macros

  • Thread starter R Pradeep Chandran
  • Start date
R

R Pradeep Chandran

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
 
D

Dave Hansen

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
 
S

SM Ryan

# 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.
 
A

Ark Khasin

SM said:
# 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top