Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   looking for a clever preprocesser solution (http://www.velocityreviews.com/forums/t450652-looking-for-a-clever-preprocesser-solution.html)

Mark P 12-21-2005 10:45 PM

looking for a clever preprocesser solution
 
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?

Victor Bazarov 12-21-2005 11:33 PM

Re: looking for a clever preprocesser solution
 
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

Mark P 12-21-2005 11:59 PM

Re: looking for a clever preprocesser solution
 
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

Old Wolf 12-22-2005 12:44 AM

Re: looking for a clever preprocesser solution
 
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


Francis Glassborow 12-22-2005 10:36 AM

Re: looking for a clever preprocesser solution
 
In article <QVkqf.40710$tV6.18537@newssvr27.news.prodigy.net> , Mark P
<usenet@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


All times are GMT. The time now is 01:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.