473,503 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preprocessor magic needed

I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)
My guess is that it is impossible. But then, in the Boost preprocessor
package, they achieve some things that I would also have thought
impossible. :-)

This has to work in C, so I cannot use C++ templates.

--
Claus Tondering

Mar 17 '06 #1
8 2889


cl*************@gmail.com wrote On 03/17/06 09:26,:
I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)
The expansion of a macro cannot produce preprocessor
directives, hence it cannot produce preprocessor conditionals.

However, you might be able to do something like

#define alpha /* nil */
#define m_alpha /* nil */

.... so that when MACRO(alpha) expands, each component of its
expansion is also a macro that then "expands" to nothing.

Personally, I don't think the preprocessor is the right
tool for this.
This has to work in C, so I cannot use C++ templates.


Why cross-post to comp.lang.c++, then? Followups
set to comp.lang.c only.

--
Er*********@sun.com

Mar 17 '06 #2
cl*************@gmail.com wrote:
I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)


Yes, it is possible:

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/control/expr_iif.hpp>
#include <boost/preprocessor/detail/is_nullary.hpp>
#include <boost/preprocessor/logical/compl.hpp>

#define LIBRARY_MACRO(id) \
BOOST_PP_EXPR_IIF( \
BOOST_PP_COMPL( \
BOOST_PP_IS_NULLARY( \
BOOST_PP_CAT(LIBRARY_MACRO_, id) \
) \
), \
X BOOST_PP_CAT(m_, X) \
) \
/**/
#define LIBRARY_MACRO_alpha ()

LIBRARY_MACRO(alpha) // expands to nothing
LIBRARY_MACRO(X) // expands to X m_X

Regards,
Paul Mensonides
Mar 18 '06 #3
On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"
<pm******@comcast.net> wrote:
cl*************@gmail.com wrote:
I need to write a macro that inserts
someStruct m_someStruct;
into another struct declaration.

The problem is that if the programmer specifies one particluar struct
(called alpha), nothing should be inserted.

So, is it possible to write a macro that does this:

MACRO(alpha) expands to nothing
MACRO(X) expands to X m_##X (for all values of X except alpha)


Yes, it is possible:

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/control/expr_iif.hpp>
#include <boost/preprocessor/detail/is_nullary.hpp>
#include <boost/preprocessor/logical/compl.hpp>

#define LIBRARY_MACRO(id) \
BOOST_PP_EXPR_IIF( \
BOOST_PP_COMPL( \
BOOST_PP_IS_NULLARY( \
BOOST_PP_CAT(LIBRARY_MACRO_, id) \
) \
), \
X BOOST_PP_CAT(m_, X) \
) \
/**/
#define LIBRARY_MACRO_alpha ()

LIBRARY_MACRO(alpha) // expands to nothing
LIBRARY_MACRO(X) // expands to X m_X

Regards,
Paul Mensonides


Hmmm ... he said it needs to work in C ... does Boost compile as C
code?

--
Bob Hairgrove
No**********@Home.com
Mar 18 '06 #4
Bob Hairgrove wrote:
On Sat, 18 Mar 2006 00:53:06 -0800, "Paul Mensonides"

Regards,
Paul Mensonides


Hmmm ... he said it needs to work in C ... does Boost compile as C
code?


The pp-lib is both C and C++ code. The rest of Boost is not.

Regards,
Paul Mensonides
Mar 18 '06 #5
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

Mar 20 '06 #6
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

Mar 20 '06 #7
With the small change that the X in your macro should be replaced by
id, it works beautifully! Thank you very much.

--
Claus Tondering

Mar 20 '06 #8
cl*************@gmail.com wrote:

With the small change that the X in your macro should be replaced
by id, it works beautifully! Thank you very much.


I suppose we should be thankful that such an incomprehensible and
useless context free message, which was even stupidly cross-posted
to c.l.c++ and c.l.c, is short.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Mar 20 '06 #9

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

Similar topics

13
3553
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
3
1414
by: Christopher Benson-Manica | last post by:
If I have a symbol defined at preprocessing (assume it's all digits), how can I make that symbol into a string constant for compiling? -- Christopher Benson-Manica | I *should* know what I'm...
9
6559
by: ccwork | last post by:
Hi all, We can define some magic number with #define: #define OPERATION_1 0x00000001 #define OPERATION_2 0x00000002 #define OPERATION_3 0x00000003 .... We can also do that with enum: enum...
8
466
by: claus.tondering | last post by:
I need to write a macro that inserts someStruct m_someStruct; into another struct declaration. The problem is that if the programmer specifies one particluar struct (called alpha), nothing...
10
1614
by: Jon | last post by:
All, Yes, it's more of the famous 'what do I do about magic_quotes' questions. Anyways, here we go: I've been a PHP developer for about a year now, and have grown to detest magic_quotes for...
32
2734
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
13
464
by: josh | last post by:
Hi, I've read that in the future the C++ will not more support the preprocessor and now some preprocessor functionality have already been implemented in the language itself. What are that? ...
9
16109
by: Larry Hale | last post by:
I've heard tell of a Python binding for libmagic (file(1) *nixy command; see http://darwinsys.com/file/). Generally, has anybody built this and worked with it under Windows? The only thing I've...
7
3347
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
0
7202
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
7086
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...
1
6991
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
5578
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,...
1
5014
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...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
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...

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.