Recent

Author Topic: local casting to interface  (Read 1178 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
local casting to interface
« on: November 13, 2023, 04:10:03 pm »
My idea was of using inteface in order to call from unrelated class by local casting but it doesn't work.

Is there a way to get this working?

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$INTERFACES CORBA}
  4.  
  5. type
  6.  
  7.   IExample = interface
  8.     procedure SomeMethod();
  9.   end;
  10.  
  11.   TExampleImplementation = class(TObject, IExample)
  12.     public
  13.       procedure SomeMethod();
  14.   end;
  15.  
  16. { TExampleImplementation }
  17.  
  18. procedure TExampleImplementation.SomeMethod();
  19. begin
  20.   Writeln('In TExampleImplementation.SomeMethod();');
  21. end;
  22.  
  23. var
  24.   o: TObject;
  25. begin
  26.   o := TExampleImplementation.Create();
  27.   TExampleImplementation(o).SomeMethod(); // Works, but is fairly useless
  28.   IExample(o).SomeMethod(); // <<-- This is the one needed, but gives error: Class or Object types "TObject" and "IExample" are not related
  29.   o.Destroy();
  30. end.
  31.  
« Last Edit: November 13, 2023, 04:48:55 pm by Чебурашка »
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

Zvoni

  • Hero Member
  • *****
  • Posts: 3368
Re: local casting to interface
« Reply #1 on: November 13, 2023, 04:30:45 pm »
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$INTERFACES CORBA}
  4.  
  5. type
  6.  
  7.   IExample = interface
  8.     procedure SomeMethod();
  9.   end;
  10.  
  11.   TExampleImplementation = class(TObject, IExample)
  12.     public
  13.       procedure SomeMethod();
  14.   end;
  15.  
  16. { TExampleImplementation }
  17.  
  18. procedure TExampleImplementation.SomeMethod();
  19. begin
  20.   Writeln('In TExampleImplementation.SomeMethod();');
  21. end;
  22.  
  23. var
  24. //THIS ONE
  25.   o: IExample;
  26. begin
  27.   o := TExampleImplementation.Create();
  28.   o.SomeMethod(); // <<-- This is the one needed, but gives error: Class or Object types "TObject" and "IExample" are not related
  29.   //o.Destroy();
  30. end.                            
No idea about Freeing/Destroying
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: local casting to interface
« Reply #2 on: November 13, 2023, 04:42:49 pm »
Hi
Yes! Have a look here:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$INTERFACES CORBA}
  4. const { this is instrumental }
  5.   SGUIDIExample = '{128CE0DE-B11C-4FEB-89AA-38EC3E79F120}';
  6. type
  7.  
  8.   IExample = interface[SGUIDIExample]
  9.     procedure SomeMethod();
  10.   end;
  11.  
  12.   TExampleImplementation = class(TObject, IExample)
  13.     public
  14.       procedure SomeMethod();
  15.   end;
  16.  
  17. { TExampleImplementation }
  18.  
  19. procedure TExampleImplementation.SomeMethod();
  20. begin
  21.   Writeln('In TExampleImplementation.SomeMethod();');
  22. end;
  23.  
  24. var
  25.   ie: IExample; { we need a variable of the right type }
  26.   o: TObject;
  27. begin
  28.   o := TExampleImplementation.Create();
  29.   TExampleImplementation(o).SomeMethod(); // Works, but is fairly useless
  30.   { if "o" supports/implements IExample, we get a hold of it and use it... }
  31.   if o.GetInterface(SGUIDIExample,ie) then ie.SomeMethod()
  32.   else writeln(o.Classname,' does not support IExample');
  33.   o.Destroy();
  34. end.
  35.  
The nice thing about it all, is, "GetInterface" is implemented in TObject  :D
Ps.: Checkout the implementation of "TPersistent"  ;)
edit: typo
HTH
Regards Benny
« Last Edit: November 13, 2023, 04:46:17 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: local casting to interface
« Reply #3 on: November 13, 2023, 04:45:32 pm »
Sorry, my very bad. Is was simply:

Code: Pascal  [Select][+][-]
  1.   (o as IExample).SomeMethod();
  2.  

« Last Edit: November 13, 2023, 04:49:14 pm by Чебурашка »
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

Zvoni

  • Hero Member
  • *****
  • Posts: 3368
Re: local casting to interface
« Reply #4 on: November 13, 2023, 04:45:50 pm »
Hi
Yes! Have a look here:
The nice thing about it all, is, "GetInterface" is implemented in TObject  :D
Ps.: Checkout the implementation of "TPersistence"  ;)
HTH
Regards Benny
So i was actually pretty close... *g*
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: local casting to interface
« Reply #5 on: November 13, 2023, 04:54:48 pm »
{$INTERFACES CORBA}

AFAIK with CORBA way user must handle memory management, so without destroy at end there's a memleak.
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

cdbc

  • Hero Member
  • *****
  • Posts: 2772
    • http://www.cdbc.dk
Re: local casting to interface
« Reply #6 on: November 13, 2023, 05:15:07 pm »
Hi
You are correct in this:
Quote
AFAIK with CORBA way user must handle memory management, so without destroy at end there's a memleak.
However, you can play a little with corba-interfaces, e.g.:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$INTERFACES CORBA}
  4. const { this is instrumental }
  5.   SGUIDIExample = '{128CE0DE-B11C-4FEB-89AA-38EC3E79F120}';
  6. type
  7.  
  8.   IExample = interface[SGUIDIExample]
  9.     function Obj: TObject;
  10.     procedure SomeMethod();
  11.   end;
  12.  
  13.   TExampleImplementation = class(TObject, IExample)
  14.     private
  15.       function Obj: TObject;
  16.     public
  17.       procedure SomeMethod();
  18.   end;
  19.  
  20. { TExampleImplementation }
  21. function TExampleImplementation.Obj: TObject;
  22. begin
  23.   Result:= Self;
  24. end;
  25.  
  26. procedure TExampleImplementation.SomeMethod();
  27. begin
  28.   Writeln('In TExampleImplementation.SomeMethod();');
  29. end;
  30.  
  31. var
  32.   ie: IExample; { we need a variable of the right type }
  33.   o: TObject;
  34. begin
  35.   o := TExampleImplementation.Create();
  36.   TExampleImplementation(o).SomeMethod(); // Works, but is fairly useless
  37.   { if "o" supports/implements IExample, we get a hold of it and use it... }
  38.   if o.GetInterface(SGUIDIExample,ie) then ie.SomeMethod()
  39.   else writeln(o.Classname,' does not support IExample');
  40.  
  41.   { now you have a choice, use either "o" or "ie" NOT both }
  42.   ie.Obj.Free; // <--- option 1 \o/\o/\o/
  43.   ie:= nil; { no dangling dead references. DON'T TOUCH "o" after this }
  44.   ...or...
  45.   o.Destroy(); // <--- option 2
  46.   //
  47. end.
  48.  
With the extra function, you can send the interface through an entire framework and free it when it's done its job, you don't need to hold on to the creating object.
Works to/from libraries also, I use only corba interfaces in my Plugin Framework, that way I have finer control over the memory-management.
Have fun  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018