looking for a clever preprocesser solution

M

Mark P

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?
 
V

Victor Bazarov

Mark said:
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
 
M

Mark P

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



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
 
O

Old Wolf

Mark said:
#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
 
F

Francis Glassborow

Mark P said:
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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top