Recent

Author Topic: Interfaces and TFPGMaps  (Read 571 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 569
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Interfaces and TFPGMaps
« on: November 21, 2022, 05:54:32 pm »
Good evening,
I have this
Code: Pascal  [Select][+][-]
  1. unit sample
  2.  
  3. interface
  4.  
  5. type
  6.   IIntf = interface
  7.         ['{00000000-0000-0000-0000-000000000000}']
  8.      function MakeSomething(inputValue: Integer): Integer;
  9.   end;
  10.  
  11.   TImpl = class(TInterfacedObject, IIntf)
  12.      // Implementation of IIntf
  13.      function MakeSomething(inputValue: Integer): Integer;
  14.      // Other methods
  15.      function MakeSomethingElse(inputValue: Integer): Integer;
  16.   end;
  17.  
  18. implementation
  19.  
  20. procedure TImpl.MakeSomething(inputValue: Integer): Integer;
  21. begin
  22.   Result := inputValue + 1;
  23. end;
  24.  
  25. procedure TImpl.MakeSomethingElse(inputValue: Integer): Integer;
  26. begin
  27.   Result := inputValue + 2;
  28. end;
  29.  
  30. end.
  31.  

Then I have a situation in which I need a generic map <string, IIntf>, because I need using clients not to see the extra methods available on the implementations of IIntf.

Reading the documentation I understand that in this case I must use a TFPGMapInterfacedObjectData (because I am dealing with refcounted material) not a TFPGMap. Is this correct?

Then I also have another situation in which I need a generic map <string, TImpl>, because I need using clients to see all methods available on TImpl.

In this case should I use a TFPGMap<string, TImpl> or should I use a TFPGMapInterfacedObjectData<string, TImpl>?

The point is: is TFPGMapInterfacedObjectData necessary everytime we are using TInterfacedObject as a base class? or does it depend on what we put in the data part of the map?

Thanks

« Last Edit: November 21, 2022, 05:56:50 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 569
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Interfaces and TFPGMaps
« Reply #1 on: November 21, 2022, 10:33:45 pm »
Reading more carefully documentation

https://www.freepascal.org/docs-html/current/rtl/fgl/tfpgmapinterfacedobjectdata.html

seems that I must use in both cases TFPGMapInterfacedObjectData.

Just please some guru to confirm.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018