By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

looking for a clever preprocesser solution

P: n/a
I have a header file and want to expose only a limited portion of that
file to SWIG (all you need to know is that when SWIG processes a file
the preprocessor macro SWIG is defined). Otherwise I want to expose the
entire file. Here's a schematic 5 line example; let's suppose I only
want to expose lines 2 and 4.

line 1
line 2
line 3
line 4
line 5

One option is:

#ifndef SWIG
line 1
#endif

line 2

#ifndef SWIG
line 3
#endif

line 4

#ifndef SWIG
line 5
#endif

This works, but what I don't like about it is that's it's phrased in the
"negative" and highlights all the things that aren't exposed to SWIG.

What I'd rather do is something like:

#ifdef SWIG
"ignore everything except labelled code"

line 1

// label on
line 2
// label off

line 3

// label on
line 4
// label off

line 5
#endif

Is there any clever trick to make this work?
Dec 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mark P wrote:
I have a header file and want to expose only a limited portion of that
file to SWIG (all you need to know is that when SWIG processes a file
the preprocessor macro SWIG is defined). Otherwise I want to expose the
entire file. Here's a schematic 5 line example; let's suppose I only
want to expose lines 2 and 4.

line 1
line 2
line 3
line 4
line 5

One option is:

#ifndef SWIG
line 1
#endif

line 2

#ifndef SWIG
line 3
#endif

line 4

#ifndef SWIG
line 5
#endif

This works, but what I don't like about it is that's it's phrased in the
"negative" and highlights all the things that aren't exposed to SWIG.
So, flip it. You can always make negative positive by using 'not'...
What I'd rather do is something like:

#ifdef SWIG
"ignore everything except labelled code"

line 1

// label on
line 2
// label off

line 3

// label on
line 4
// label off

line 5
#endif

Is there any clever trick to make this work?


No. Your "label on" is still going to be an #ifdef, and "label off" --
#endif. So, you can do

#ifndef SWIG
#define MORETHANJUSTSWIG
#endif

#ifdef MORETHANJUSTSWIG
line 1
#endif

line 2

#ifdef MORETHANJUSTSWIG
line 3
#endif

line 4

#ifdef MORETHANJUSTSWIG
line 5
#endif

Now, it's only partially negative. It still "highlights" something, just
like your "label on" and "label off" do. My question is, so damn what?

If you can, extract your lines 2 and 4 into a separate file and include it
here, and hide your 'ifdef SWIG' there.

All in all, if you need to conditionally compile something, you're stuck
with #ifdef or #ifndef or #if, and nothing else is going to help you. Get
over it.

V
Dec 21 '05 #2

P: n/a
Victor Bazarov wrote:
Mark P wrote:

This works, but what I don't like about it is that's it's phrased in
the "negative" and highlights all the things that aren't exposed to SWIG.

So, flip it. You can always make negative positive by using 'not'...

Is there any clever trick to make this work?

No. Your "label on" is still going to be an #ifdef, and "label off" --
#endif. So, you can do

#ifndef SWIG
#define MORETHANJUSTSWIG
#endif

#ifdef MORETHANJUSTSWIG
line 1
#endif

line 2

#ifdef MORETHANJUSTSWIG
line 3
#endif

line 4

#ifdef MORETHANJUSTSWIG
line 5
#endif

Now, it's only partially negative. It still "highlights" something,
just like your "label on" and "label off" do. My question is, so damn
what?


By negative I didn't mean ifdef vs. ifndef, I meant that I had to wrap
all the non-SWIG stuff instead of the SWIG stuff. In practice, the
portions that are meant to be exposed to SWIG are small, and the
portions that aren't to be exposed are large, so it's easier to
comprehend IMO if only the SWIG portions are wrapped. In any event, I
gather that there's no clever fix here.

Thanks,
Mark
Dec 22 '05 #3

P: n/a
Mark P wrote:

#ifndef SWIG
line 1
#endif

line 2

#ifndef SWIG
line 3
#endif

line 4

#ifndef SWIG
line 5
#endif

This works, but what I don't like about it is that's it's phrased in the
"negative" and highlights all the things that aren't exposed to SWIG.


This is just a whitespace issue, surely?

#ifndef SWIG
.......
.......
line 1

#endif
line 2
#ifndef SWIG

line 3

#endif
line 4
#ifndef SWIG

line 5
.........
........
#endif

Dec 22 '05 #4

P: n/a
In article <QV*******************@newssvr27.news.prodigy.net> , Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> writes
I have a header file and want to expose only a limited portion of that
file to SWIG (all you need to know is that when SWIG processes a file
the preprocessor macro SWIG is defined). Otherwise I want to expose
the entire file. Here's a schematic 5 line example; let's suppose I
only want to expose lines 2 and 4.

line 1
line 2
line 3
line 4
line 5

One option is:

#ifndef SWIG
line 1
#endif

line 2

#ifndef SWIG
line 3
#endif

line 4

#ifndef SWIG
line 5
#endif

This works, but what I don't like about it is that's it's phrased in
the "negative" and highlights all the things that aren't exposed to
SWIG.

What I'd rather do is something like:

#ifdef SWIG
"ignore everything except labelled code"


Perhaps ifdef/ifndef is the wrong choice of preprocessor directive, try:

#define SWIG 0

#if SWIG
line 1
#endif

line2

#if SWIG
line 3
#endif

line4

#if SWIG
line 5
#endif

Now you can switch suppression on and off by changing the value
associated with SWIG between 0 and 1

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Dec 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.