Recent

Author Topic: Different compiler directive under compile? [SOLVED by marcov]  (Read 3872 times)

totya

  • Hero Member
  • *****
  • Posts: 720
Hi!

I have two own component:

T_A and T_B

T_A = class(TListBox)
T_B = class(TComponent)

T_A use T_B extensively as subcomponent. But few things overcomplicated in this way, so the ideal solution:

T_A = class(T_B, TListBox) but it isn't supported in Delphi/Lazarus.

I'm thinking on directive, because I need similar of this:

{$IFDEF *COMPILE* T_B}
  T_B = class(TComponent)
  ...
  end;
{$END}
{$IFDEF *COMPILE* T_A}
  T_B = class(TListBox)
  ...
  end;
{$END}

but I don't know similar directive :) Any idea of this? Thanks!
« Last Edit: June 26, 2018, 11:28:08 pm by totya »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Different compiler directive under compile?
« Reply #1 on: June 25, 2018, 10:57:11 am »
I really don't understand what do you want, but as what you say in the last part:

Code: Pascal  [Select][+][-]
  1. {$IFDEF COMPILE_T_B}
  2.   T_B = class(TComponent)
  3.   ...
  4.   end;
  5. {$ENDIF}
  6. {$IFDEF COMPILE_T_A}
  7.   T_B = class(TListBox)
  8.   ...
  9.   end;
  10. {$ENDIF}
  11.  

That should work(?).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #2 on: June 25, 2018, 11:22:14 am »
Yes, my english is not too good... :)

My idea, the compiler compile the sections under {$DEFINE...$ENDIF}, depend on what TYPE (T_A class or T_B class) compiled...

If the compiler compile T_A class, then compile between {IFDEF *COMPILE* T_A}...{$ENDIF}
If the compiler compile T_B class, then compile between {IFDEF *COMPILE* T_B}...{$ENDIF}

if have a package, which contanin T_A and T_B. In package options, I can set only one directive at once, so your COMPILE_T_A/COMPILE_T_B is unusable.

I tried two packages (because I can set two different directive in packages), but I got warnings because packages uses same files, and package install is halted.

Thnaks! :)
« Last Edit: June 25, 2018, 11:24:35 am by totya »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Different compiler directive under compile?
« Reply #3 on: June 25, 2018, 11:27:58 am »
I tried two packages (because I can set two different directive in packages), but I got warnings because packages uses same files, and package install is halted.

Move the code to an inc file. Make two units that just do $define and include the inc file.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #4 on: June 25, 2018, 11:31:57 am »
I tried two packages (because I can set two different directive in packages), but I got warnings because packages uses same files, and package install is halted.

Move the code to an inc file. Make two units that just do $define and include the inc file.

Thanks for the answer! I use inc files already for the implementations, like as Lazarus source. But I don't understand your idea, can u wrote a small example for me? Thanks...

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Different compiler directive under compile?
« Reply #5 on: June 25, 2018, 02:20:51 pm »
Hi!

I have two own component:

T_A and T_B

T_A = class(TListBox)
T_B = class(TComponent)

T_A use T_B extensively as subcomponent. But few things overcomplicated in this way, so the ideal solution:

T_A = class(T_B, TListBox) but it isn't supported in Delphi/Lazarus.

I'm thinking on directive, because I need similar of this:

{$IFDEF *COMPILE* T_B}
  T_B = class(TComponent)
  ...
  end;
{$END}
{$IFDEF *COMPILE* T_A}
  T_B = class(TListBox)
  ...
  end;
{$END}

but I don't know similar directive :) Any idea of this? Thanks!
Yeah, multiple inheritance isn't supported in Delphi, but you can always use old clunky solution - class composition:
Code: Pascal  [Select][+][-]
  1. //Replace this
  2. T_B = class(TComponent)
  3. public
  4.   function ToString:String;override;
  5. end;
  6.  
  7. T_A = class(TListBox, T_B)
  8. end;
  9. //with this
  10. T_A = class(TListBox)
  11. protected
  12.   F_B:T_B;
  13. public
  14.   function ToString:String;override;
  15. end;
  16.  
  17. //Wrapper. Yeah, clunky, slow, memory inefficient, but works.
  18.  
  19. function T_A.ToString:String;
  20. begin
  21.   Result := F_B.ToString;
  22. end;
  23.  
« Last Edit: June 25, 2018, 02:36:08 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #6 on: June 25, 2018, 02:35:45 pm »
 %)

I'm sorry, but your idea is absolutely wrong here, because these are components...

If I drop your T_A to the form, certainly I can't see the ListBox.

Code: Pascal  [Select][+][-]
  1. type
  2.   T_B = class(TComponent)
  3.   end;
  4.  
  5. type
  6.   T_A = class(T_B)
  7.     protected
  8.       FListBox: TListBox;
  9.   end;

But thanks for the... thinking :)

ps.: I think the "new" compiler directive (depends on compiled class) is not a bad idea, and I suppose it isn't hard to implement (for advanced programmers - not me).

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Different compiler directive under compile?
« Reply #7 on: June 25, 2018, 02:44:00 pm »
%)

I'm sorry, but your idea is absolutely wrong here, because these are components...

If I drop your T_A to the form, certainly I can't see the ListBox.

Code: Pascal  [Select][+][-]
  1. type
  2.   T_B = class(TComponent)
  3.   end;
  4.  
  5. type
  6.   T_A = class(T_B)
  7.     protected
  8.       FListBox: TListBox;
  9.   end;

But thanks for the... thinking :)

ps.: I think the "new" compiler directive (depends on compiled class) is not a bad idea, and I suppose it isn't hard to implement (for advanced programmers - not me).
First of all, you should say, what do you try to do. Because may be there are better solutions, than multiple inheritance. As I understand, you have the same problem, as me. My problem is:
Code: Pascal  [Select][+][-]
  1. TAbstractList = class;
  2. TAbstractPairList = class(TAbstractList);
  3.  
  4. TList = class(TAbstractList);//Implementation
  5. TPairList = class(TAbstractPairList, TList);//Oooops, I can't do it - have to implement TList part from scratch
  6.  
and only possible solution is
Code: Pascal  [Select][+][-]
  1. TPairList = class(TAbstractPairList)//TAbstractPairList already contains TAbstractList
  2. protected
  3.   FListImplementation:TList;
  4. public
  5.   procedure SomeTAbstractListRoutineINeedToOverrideThatIsActuallyImplementedInTList;override;
  6. end;
  7.  
  8. //Wrapper
  9.  
  10. procedure TPairList.SomeTAbstractListRoutineINeedToOverrideThatIsActuallyImplementedInTList;
  11. begin
  12.   FListImplementation.SomeTAbstractListRoutineINeedToOverrideThatIsActuallyImplementedInTList;
  13. end;
  14.  
« Last Edit: June 25, 2018, 02:45:46 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #8 on: June 25, 2018, 02:58:39 pm »
Quote
you should say, what do you try to do

I need to learn Englsh.. :) So, this problem isn't really big probem, because my code works, but I don't like the code, because seems to me slightly overcomplicated (because I can't use inheritance), so I try other way to implement things.
I wrote it in the first message what I try to do. But again, other words:

I have two component:

T_A = class(TListBox)
T_B = class(TComponent)

T_B must TComponent, because I don't want to see the Listbox.
T_A must inherited from ListBox, because this is a ListBox, with advanced functions.

The ideal solution:
T_A = class(T_B) because then I can use inheritance. But, see above, T_B must TComponent. ;)

« Last Edit: June 25, 2018, 03:11:35 pm by totya »

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Different compiler directive under compile?
« Reply #9 on: June 25, 2018, 03:27:38 pm »
Quote
you should say, what do you try to do

I need to learn Englsh.. :) So, this problem isn't really big probem, because my code works, but I don't like the code, because seems to me slightly overcomplicated (because I can't use inheritance), so I try other way to implement things.
I wrote it in the first message what I try to do. But again, other words:

I have two component:

T_A = class(TListBox)
T_B = class(TComponent)

T_B must TComponent, because I don't want to see the Listbox.
T_A must inherited from ListBox, because this is a ListBox, with advanced functions.

The ideal solution:
T_A = class(T_B) because then I can use inheritance. But, see above, T_B must TComponent. ;)
As I understand, you have some special code in T_B and want to reuse it in T_A's implementation, but can't, so you have to copy-paste implementation to T_A. This is exactly my problem. And only possible solution, if you don't want to copy-paste code twice - to have T_B object inside T_A and redirect all method calls to it. See my first post - I've changed it to look more clear. ToString is implemented in T_B, but we also want to use it in T_A.
« Last Edit: June 25, 2018, 03:31:05 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #10 on: June 25, 2018, 03:34:57 pm »
Quote
As I understand, you have some special code in T_B

Not exactly, T_A use all of code from T_B.  (T_A use T_B as subcomponent now, as I wrote before).

Thanks for the asnwer! :)


Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Different compiler directive under compile?
« Reply #11 on: June 25, 2018, 03:55:22 pm »
Not exactly, T_A use all of code from T_B.  (T_A use T_B as subcomponent now, as I wrote before).

Thanks for the asnwer! :)
Ehhh, I wanted to suggest C++ Builder, cuz C++ supports multiple inheritance, but this wouldn't work
Code: C++  [Select][+][-]
  1. class T_B:public virtual TComponent {};
  2. class T_A:public TListBox, public virtual T_B {};
  3. //Wouldn't work, cuz TControl should be declared this way for it to work and it isn't
  4. class TControl:public virtual TComponent {};
  5. //It's declared this way, that doesn't allow T_B to replace TComponent implementation inside T_A
  6. class TControl:public TComponent {};
  7.  
« Last Edit: June 25, 2018, 03:58:11 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Different compiler directive under compile?
« Reply #12 on: June 26, 2018, 11:27:47 pm »
I tried two packages (because I can set two different directive in packages), but I got warnings because packages uses same files, and package install is halted.

Move the code to an inc file. Make two units that just do $define and include the inc file.

I understood your post at the moment, and works, thanks  :o


 

TinyPortal © 2005-2018