looking for a clever preprocesser solution

Discussion in 'C++' started by Mark P, Dec 21, 2005.

  1. Mark P

    Mark P Guest

    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?
     
    Mark P, Dec 21, 2005
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 21, 2005
    #2
    1. Advertising

  3. Mark P

    Mark P Guest

    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
     
    Mark P, Dec 21, 2005
    #3
  4. Mark P

    Old Wolf Guest

    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
     
    Old Wolf, Dec 22, 2005
    #4
  5. In article <QVkqf.40710$>, Mark P
    <> 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
     
    Francis Glassborow, Dec 22, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    3
    Views:
    955
    Grant Wagner
    Jul 15, 2003
  2. Brian J. Sayatovic

    Clever serialization broken by CORBA RMI

    Brian J. Sayatovic, Sep 30, 2004, in forum: Java
    Replies:
    0
    Views:
    370
    Brian J. Sayatovic
    Sep 30, 2004
  3. A
    Replies:
    0
    Views:
    272
  4. Scott Rifkin

    clever programming solution wanted

    Scott Rifkin, Jul 15, 2004, in forum: Python
    Replies:
    3
    Views:
    339
    David Eppstein
    Jul 15, 2004
  5. JimLad
    Replies:
    3
    Views:
    1,072
    JimLad
    Nov 30, 2009
Loading...

Share This Page