How to restrict 'some' code from getting compiled/built? (without #define)

Discussion in 'C++' started by Sachin Garg, Jun 6, 2008.

  1. Sachin Garg

    Sachin Garg Guest

    I need to build two executables from my code, one having all the code (and
    thus application features) and other not having it all. How to best manage
    the code that shouldn't go in one of the executables?

    My first thought is to use conditional compilation with #define and #ifdef
    etc but its getting messy, are there better ways to manage this?

    The problem in detail:

    #. There is a class A with virtual foo1 and foo2
    #. There are 'lots' of derived classes of A which implement foo1 and foo2
    #. Executable1 needs to use both foo1 and foo2
    #. Executable2 only needs to use foo1, never needs foo2

    How to make sure that all the foo2 code never gets into executable2? Lots of
    #ifdefs can be used for this but is there a better solution? Maybe some
    clever use of templates? Something else less complicated?

    Thanks
     
    Sachin Garg, Jun 6, 2008
    #1
    1. Advertising

  2. Sachin Garg

    Guest

    Re: How to restrict 'some' code from getting compiled/built? (without#define)

    On Jun 7, 1:37 am, "Sachin Garg" <> wrote:
    > I need to build two executables from my code, one having all the code (and
    > thus application features) and other not having it all. How to best manage
    > the code that shouldn't go in one of the executables?
    >
    > My first thought is to use conditional compilation with #define and #ifdef
    > etc but its getting messy, are there better ways to manage this?
    >
    > The problem in detail:
    >
    > #. There is a class A with virtual foo1 and foo2
    > #. There are 'lots' of derived classes of A which implement foo1 and foo2
    > #. Executable1 needs to use both foo1 and foo2
    > #. Executable2 only needs to use foo1, never needs foo2
    >
    > How to make sure that all the foo2 code never gets into executable2? Lots of
    > #ifdefs can be used for this but is there a better solution? Maybe some
    > clever use of templates? Something else less complicated?
    >
    > Thanks


    Hm, don't know if that's what you want, but here's how it can be done:

    template <bool Use>
    class foo2
    {
    //implementation in case of it's not needed
    }

    template <>
    class foo2<true>
    {
    //implementation in case of it's needed
    }

    template <bool Use>
    class foo2derived : public foo2<Use>
    {
    //implementation in case of it's not needed
    }

    template <>
    class foo2derived : public foo2<true>
    {
    //implementation in case of it's needed
    }

    Usage:

    const bool UseFoo2 = ...;

    foo2derived<UseFoo2> f2d; //will compile the needed version of
    foo2derived
     
    , Jun 6, 2008
    #2
    1. Advertising

  3. Re: How to restrict 'some' code from getting compiled/built? (without#define)

    On 2008-06-06 23:37, Sachin Garg wrote:
    > I need to build two executables from my code, one having all the code (and
    > thus application features) and other not having it all. How to best manage
    > the code that shouldn't go in one of the executables?
    >
    > My first thought is to use conditional compilation with #define and #ifdef
    > etc but its getting messy, are there better ways to manage this?
    >
    > The problem in detail:
    >
    > #. There is a class A with virtual foo1 and foo2
    > #. There are 'lots' of derived classes of A which implement foo1 and foo2
    > #. Executable1 needs to use both foo1 and foo2
    > #. Executable2 only needs to use foo1, never needs foo2
    >
    > How to make sure that all the foo2 code never gets into executable2? Lots of
    > #ifdefs can be used for this but is there a better solution? Maybe some
    > clever use of templates? Something else less complicated?


    Instead of preventing the code from being in both executables it might
    be easier to just prevent foo2 from being called, perhaps by using
    #ifdefs to select which kinds of input the program accepts.

    --
    Erik Wikström
     
    Erik Wikström, Jun 6, 2008
    #3
  4. Sachin Garg

    Sachin Garg Guest

    <> wrote in message
    news:...
    > On Jun 7, 1:37 am, "Sachin Garg" <> wrote:
    >> I need to build two executables from my code, one having all the code
    >> (and
    >> thus application features) and other not having it all. How to best
    >> manage
    >> the code that shouldn't go in one of the executables?
    >>
    >> My first thought is to use conditional compilation with #define and
    >> #ifdef
    >> etc but its getting messy, are there better ways to manage this?
    >>
    >> The problem in detail:
    >>
    >> #. There is a class A with virtual foo1 and foo2
    >> #. There are 'lots' of derived classes of A which implement foo1 and foo2
    >> #. Executable1 needs to use both foo1 and foo2
    >> #. Executable2 only needs to use foo1, never needs foo2
    >>
    >> How to make sure that all the foo2 code never gets into executable2? Lots
    >> of
    >> #ifdefs can be used for this but is there a better solution? Maybe some
    >> clever use of templates? Something else less complicated?
    >>
    >> Thanks

    >
    > Hm, don't know if that's what you want, but here's how it can be done:
    >
    > template <bool Use>
    > class foo2
    > {
    > //implementation in case of it's not needed
    > }
    >
    > template <>
    > class foo2<true>
    > {
    > //implementation in case of it's needed
    > }
    >
    > template <bool Use>
    > class foo2derived : public foo2<Use>
    > {
    > //implementation in case of it's not needed
    > }
    >
    > template <>
    > class foo2derived : public foo2<true>
    > {
    > //implementation in case of it's needed
    > }
    >
    > Usage:
    >
    > const bool UseFoo2 = ...;
    >
    > foo2derived<UseFoo2> f2d; //will compile the needed version of
    > foo2derived


    Thanks, this helps.
     
    Sachin Garg, Jun 7, 2008
    #4
  5. Sachin Garg

    Sachin Garg Guest

    "Erik Wikström" <> wrote in message
    news:YWi2k.8131$...
    > On 2008-06-06 23:37, Sachin Garg wrote:
    >> I need to build two executables from my code, one having all the code
    >> (and
    >> thus application features) and other not having it all. How to best
    >> manage
    >> the code that shouldn't go in one of the executables?
    >>
    >> My first thought is to use conditional compilation with #define and
    >> #ifdef
    >> etc but its getting messy, are there better ways to manage this?
    >>
    >> The problem in detail:
    >>
    >> #. There is a class A with virtual foo1 and foo2
    >> #. There are 'lots' of derived classes of A which implement foo1 and foo2
    >> #. Executable1 needs to use both foo1 and foo2
    >> #. Executable2 only needs to use foo1, never needs foo2
    >>
    >> How to make sure that all the foo2 code never gets into executable2? Lots
    >> of
    >> #ifdefs can be used for this but is there a better solution? Maybe some
    >> clever use of templates? Something else less complicated?

    >
    > Instead of preventing the code from being in both executables it might
    > be easier to just prevent foo2 from being called, perhaps by using
    > #ifdefs to select which kinds of input the program accepts.


    I have done this, wanted to also prevent unused code from getting in the
    exe.
     
    Sachin Garg, Jun 7, 2008
    #5
  6. Sachin Garg

    James Kanze Guest

    Re: How to restrict 'some' code from getting compiled/built? (without#define)

    On Jun 6, 11:37 pm, "Sachin Garg" <> wrote:
    > I need to build two executables from my code, one having all
    > the code (and thus application features) and other not having
    > it all. How to best manage the code that shouldn't go in one
    > of the executables?


    > My first thought is to use conditional compilation with
    > #define and #ifdef etc but its getting messy, are there better
    > ways to manage this?


    > The problem in detail:


    > #. There is a class A with virtual foo1 and foo2
    > #. There are 'lots' of derived classes of A which implement foo1 and foo2
    > #. Executable1 needs to use both foo1 and foo2
    > #. Executable2 only needs to use foo1, never needs foo2


    > How to make sure that all the foo2 code never gets into
    > executable2? Lots of #ifdefs can be used for this but is there
    > a better solution? Maybe some clever use of templates?
    > Something else less complicated?


    Generally, if a class has two different interfaces, it's two
    different classes. You'd have to be more concrete; the usual
    solution for conditionally including functions and/or objects is
    to put the conditional objects in separate source files, and
    either link the corresponding object files in or not.

    In your case, it looks to me like you have two separate
    hierachies: one with just foo1, and one with both. This could
    be done using multiple inheritance, something like:

    class Base
    {
    public:
    virtual void foo1() = 0 ;
    } ;

    class Derived : public virtual Base
    {
    public:
    virtual void foo1() ;
    } ;

    and:

    class BaseOptional : public virual Base
    {
    public:
    virtual void foo2() = 0 ;
    } ;

    class DerivedOptional : public virtual Derived
    {
    public:
    virtual void foo2() ;
    } ;

    Put the ...Optional in a separate directory and library.

    Personally, however, I'd reconsider the design before I adopted
    such complexity. Couldn't two separate hierarchies be made to
    work as well, if not better?

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 7, 2008
    #6
  7. Sachin Garg

    Sachin Garg Guest

    Re: How to restrict 'some' code from getting compiled/built? (without #define)

    "Sam" <> wrote in message
    news:-scan.com...
    Sachin Garg writes:
    > > The problem in detail:
    > >
    > > #. There is a class A with virtual foo1 and foo2
    > > #. There are 'lots' of derived classes of A which implement foo1 and
    > > foo2
    > > #. Executable1 needs to use both foo1 and foo2
    > > #. Executable2 only needs to use foo1, never needs foo2
    > >
    > > How to make sure that all the foo2 code never gets into executable2?
    > > Lots of
    > > #ifdefs can be used for this but is there a better solution?

    >
    > Yes: organizing your code.
    >
    > Put all code derived from foo2 into separate translation units. Do not
    > include those translation units in your second executable.


    Hmmm, worth a try.

    ps. There is maybe something wrong with your news reader, your message came
    in a 'text attachment', while the actual post was empty (atleast thats how
    it came in outlook express). You might want to check this out.
     
    Sachin Garg, Jun 7, 2008
    #7
  8. Sachin Garg

    Sachin Garg Guest

    "James Kanze" <> wrote in message
    news:...
    > On Jun 6, 11:37 pm, "Sachin Garg" <> wrote:
    > > I need to build two executables from my code, one having all
    > > the code (and thus application features) and other not having
    > > it all. How to best manage the code that shouldn't go in one
    > > of the executables?
    > >
    > > My first thought is to use conditional compilation with
    > > #define and #ifdef etc but its getting messy, are there better
    > > ways to manage this?
    > >
    > > The problem in detail:
    > >
    > > #. There is a class A with virtual foo1 and foo2
    > > #. There are 'lots' of derived classes of A which implement foo1 and
    > > foo2
    > > #. Executable1 needs to use both foo1 and foo2
    > > #. Executable2 only needs to use foo1, never needs foo2
    > >
    > > How to make sure that all the foo2 code never gets into
    > > executable2? Lots of #ifdefs can be used for this but is there
    > > a better solution? Maybe some clever use of templates?
    > > Something else less complicated?

    >
    > Generally, if a class has two different interfaces, it's two
    > different classes. You'd have to be more concrete; the usual
    > solution for conditionally including functions and/or objects is
    > to put the conditional objects in separate source files, and
    > either link the corresponding object files in or not.
    >
    > In your case, it looks to me like you have two separate
    > hierachies: one with just foo1, and one with both. This could
    > be done using multiple inheritance, something like:
    >
    > [code snipped]
    >
    > Put the ...Optional in a separate directory and library.
    >
    > Personally, however, I'd reconsider the design before I adopted
    > such complexity. Couldn't two separate hierarchies be made to
    > work as well, if not better?


    Duh, I was

    Back to the drawing board :)
     
    Sachin Garg, Jun 7, 2008
    #8
  9. Sachin Garg

    Sachin Garg Guest

    "Sachin Garg" <> wrote in message
    news:g2es9j$fpb$...
    >
    > "James Kanze" <> wrote in message
    > news:...
    >> On Jun 6, 11:37 pm, "Sachin Garg" <> wrote:
    >> > I need to build two executables from my code, one having all
    >> > the code (and thus application features) and other not having
    >> > it all. How to best manage the code that shouldn't go in one
    >> > of the executables?
    >> >
    >> > My first thought is to use conditional compilation with
    >> > #define and #ifdef etc but its getting messy, are there better
    >> > ways to manage this?
    >> >
    >> > The problem in detail:
    >> >
    >> > #. There is a class A with virtual foo1 and foo2
    >> > #. There are 'lots' of derived classes of A which implement foo1 and
    >> > foo2
    >> > #. Executable1 needs to use both foo1 and foo2
    >> > #. Executable2 only needs to use foo1, never needs foo2
    >> >
    >> > How to make sure that all the foo2 code never gets into
    >> > executable2? Lots of #ifdefs can be used for this but is there
    >> > a better solution? Maybe some clever use of templates?
    >> > Something else less complicated?

    >>
    >> Generally, if a class has two different interfaces, it's two
    >> different classes. You'd have to be more concrete; the usual
    >> solution for conditionally including functions and/or objects is
    >> to put the conditional objects in separate source files, and
    >> either link the corresponding object files in or not.
    >>
    >> In your case, it looks to me like you have two separate
    >> hierachies: one with just foo1, and one with both. This could
    >> be done using multiple inheritance, something like:
    >>
    >> [code snipped]
    >>
    >> Put the ...Optional in a separate directory and library.
    >>
    >> Personally, however, I'd reconsider the design before I adopted
    >> such complexity. Couldn't two separate hierarchies be made to
    >> work as well, if not better?

    >
    > Duh, I was
    >
    > Back to the drawing board :)


    Hit 'send' a bit too soon :p

    I was probably trying to use this conditional compilation on top of existing
    design to patch up for this new requirement. You are right that a fresh
    rethink on the design can give more elegant solutions.

    Back to the drawing board :)

    Thanks.
     
    Sachin Garg, Jun 7, 2008
    #9
    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. ad
    Replies:
    2
    Views:
    746
  2. Klaus Schneider
    Replies:
    1
    Views:
    581
    Rolf Magnus
    Dec 2, 2004
  3. theotyflos
    Replies:
    3
    Views:
    506
    Thomas Matthews
    Feb 19, 2004
  4. Sweety
    Replies:
    6
    Views:
    4,115
    CBFalconer
    Apr 5, 2004
  5. lander
    Replies:
    5
    Views:
    625
    bruce barker
    Mar 5, 2008
Loading...

Share This Page