Recent

Author Topic: How can I get names of CORBA-interfaces via rtti?  (Read 1499 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #15 on: July 21, 2026, 10:31:29 am »
Yes. Debug it....
Code: ASM  [Select][+][-]
  1. 000000010000171F 4889C1                   mov rcx,rax
  2. 0000000100001722 488D15771A0100           lea rdx,[rip+$00011A77]    # $00000001000131A0
  3. 0000000100001729 E8E22F0000               call +$00002FE2    # $0000000100004710 FPC_CLASS_AS_CORBAINTF
  4. 000000010000172E 488905DB580100           mov [rip+$000158DB],rax
It needs a compiler compiled in debug mode, though. Then you can see the string comparison show up.
You can also time it (with system.diagnostics.TStopwatch in trunk, maybe 3.2.4)
« Last Edit: July 21, 2026, 11:00:04 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

TheKeeper

  • New member
  • *
  • Posts: 8
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #16 on: July 21, 2026, 10:48:14 am »
Hah.
Good to know. The same for "is" as far as I can see.

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #17 on: July 21, 2026, 10:52:18 am »
 :) :D Told you so! (But is does not work for CORBA, afaik, just as)

Anyway, if the class and interface are compiled in {$M+} state you would get RTTI for it.
Since version 3.2.0:
https://wiki.freepascal.org/FPC_New_Features_3.2.0#Method_RTTI_for_interfaces
But it says: COM only. I don't know if this has changed.
Relies on Invoke() being implemented it seems.
All the more reason to drop CORBA. Use COM and TNoRefCountObject if you don't want refcounts. If it is not in 3.2.2 then use this:
Code: Pascal  [Select][+][-]
  1. {$interfaces COM}
  2. type
  3.  
  4. TNoRefCountObject =  class(TObject, IInterface)
  5. protected
  6.   function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};inline;
  7.   function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};inline;
  8.   function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};inline;
  9. end;
  10.  
  11. function TNoRefCountObject.QueryInterface( {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};inline;
  12. begin
  13.    if getinterface(iid,obj) then
  14.      result:=S_OK
  15.    else
  16.      result:=longint(E_NOINTERFACE);
  17. end;
  18.  
  19. function TNoRefCountObject._AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  20. begin
  21.    Result:=-1;
  22. end;
  23.  
  24. function TNoRefCountObject._Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  25. begin
  26.   Result:=-1;
  27. end;
Taken ad verbatum from the trunk sources, except explicit inlining.
This "overhead" is way less than a string comparison....
And this WILL get you RTTI!
« Last Edit: July 21, 2026, 11:29:48 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #18 on: July 21, 2026, 11:43:06 am »
Hi
I do it like this:
Code: Pascal  [Select][+][-]
  1. {$interfaces corba}
  2.   { this is the ancestor for my corba interfaces }
  3.   ICorba = interface['{3563A63E-8C1A-40E1-9574-3E4171BB3ACF}']
  4.     function Obj: TObject;
  5.   end;
This gives me access to the underlying class for e.g. '.Free;' and TObject implements 'GetInterface'  8-)
With this ancester I stear clear of @Thaddy's /pitfalls/  --  Mind you, our dear Thaddy is 231% -against- CORBA ~ tkInterfaceRaw, I've tried along with others to convince him, that both have their uses, alas to no avail  :D ;D :D

THIS is built using both types of interfaces and it works. So is THIS
So don't let Thaddy intimidate you, my code works with FPC 3.2.2 .. 3.3.1, he was showing you features from 3.3.1 ~ trunk.
I almost always agree with Thaddy, until we hit Interfaces, here our opinions differ totally  :P  ...and that's fine so, I wouldn't have it any other way  ;)
Regards Benny

edit: Thaddy made an addendum and he's completely right, that's the way to  turn off ref-counting in a safe way and I have contemplated it, but have a hard time, telling which intf among many, that is -not- refcounted, when they're all COM  %) %) %)
« Last Edit: July 21, 2026, 11:48:37 am 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

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #19 on: July 21, 2026, 01:00:06 pm »
Benny, you are cheating because that is the same as adding a free procedure to the interface... ;)
And I object to misusing GUIDS as a string. You know that: it is not a GUID it is a string.
I really do not understand people who use CORBA style interfaces.... (without a proper broker, which we don't have). It is slow.
Explain to me why COM is heavier: I just disproved that.... >:D >:D O:-)

Unless somebody is going through the effort to disproof me, which they can't (easlily).  ;D

For others: read the thread, check the code.
(BTW show me the RTTI, that is on subject)
« Last Edit: July 21, 2026, 01:22:06 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

TheKeeper

  • New member
  • *
  • Posts: 8
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #20 on: July 21, 2026, 02:01:08 pm »
Quote
I really do not understand people who use CORBA style interfaces....
I use it when I need no memory owner change, no cunning factory method, just interface.
Example:

Code: Pascal  [Select][+][-]
  1. {$interfaces corba}
  2. IMyInterface1 = interface
  3.   function JustPartOfFunctional(AArguments): ResultType;
  4. end;
  5.  
  6. IMyInterface2 = interface
  7.   function JustAnotherPartOfFunctional(AArguments): ResultType;
  8. end;
  9.  
  10. TMyClass = class(TObject, IMyInterface1, IMyInterface2)
  11. public
  12.   ...
  13.   function JustPartOfFunctional(AArguments): ResultType;
  14.   function JustAnotherPartOfFunctional(AArguments): ResultType;
  15.   ...
  16. end;

somewhere in code:

Code: Pascal  [Select][+][-]
  1. FMyClass: TMyClass;

Code: Pascal  [Select][+][-]
  1. some constructor();
  2. begin
  3.   ...
  4.   FMyClass := TMyClass.Create();
  5.   ...
  6. end;
  7.  
  8. some destructor;
  9. begin
  10.   ...
  11.   FreeAndNil(FMyClass);
  12.   ...
  13. end;
  14.  
  15. some method1(): IMyInterface1;
  16. begin
  17.   Result := FMyClass;
  18. end;
  19.  
  20. some method2(): IMyInterface2;
  21. begin
  22.   Result := FMyClass;
  23. end;

Something like that. Sorry, if I was not very good in explanation. No overhead, just proper incapsulation, nothing more.

cdbc

  • Hero Member
  • *****
  • Posts: 2931
    • http://www.cdbc.dk
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #21 on: July 21, 2026, 03:11:10 pm »
Hi
@TheKeeper: +1
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

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #22 on: July 21, 2026, 04:24:47 pm »
@TheKeeper: +1
@TheKeeper: -1
You, reluctantly, also  -1

Silly reasoning and the proof is in plain sight.

Sorry about answering the thread again, I should not do that.

Both of you do not understand "CORBA" in the context of FreePascal.
Use at your own peril. >:( >:(
I did my best. CHECK THE CODE and both of you know you are wrong.
Compile -al. Examine .s

If people don't want to listen or check, I am out.
(This is also a longstanding feud with Michaelis, who also refuses to understand)
And to get back on topic, Benny: NO RTTI. period.
« Last Edit: July 21, 2026, 04:50:04 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #23 on: July 21, 2026, 04:53:23 pm »
Because of that, I want CORBA removed. It serves no purpose.
Any "programmer" that knows only one programming language is not a programmer

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #24 on: July 21, 2026, 04:55:08 pm »
Something like that. Sorry, if I was not very good in explanation. No overhead, just proper incapsulation, nothing more.
And NO RTTI, and that was your question
Any "programmer" that knows only one programming language is not a programmer

PascalDragon

  • Hero Member
  • *****
  • Posts: 6421
  • Compiler Developer
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #25 on: July 23, 2026, 09:45:07 pm »
I have some class and I should know all corba and com interfaces, which are implemented by this class.

With COM-interfaces I have no problem:

Code: Pascal  [Select][+][-]
  1. InterfaceTablePtr := RttiType.AsInstance.MetaClassType.GetInterfaceTable();
  2. {$R-}
  3. for i := 0 to InterfaceTablePtr^.EntryCount - 1 do
  4.   if Assigned(InterfaceTablePtr^.Entries[i].IIDRef) then
  5.     Debug := InterfaceTablePtr^.Entries[i].IIDStrRef^^;
  6. {$R+}

But that's how TInterfaceEntry looks like:

Code: Pascal  [Select][+][-]
  1. tinterfaceentry = record
  2. private
  3.   function GetIID: pguid; inline;
  4.   function GetIIDStr: pshortstring; inline;
  5. public
  6.   property IID: pguid read GetIID;
  7.   property IIDStr: pshortstring read GetIIDStr;
  8. public
  9.   IIDRef      : ^pguid; { if assigned(IID) then Com else Corba}
  10.   VTable      : Pointer;
  11.   case integer of
  12.     1 : (
  13.       IOffset: sizeuint;
  14.     );
  15.     2 : (
  16.       IOffsetAsCodePtr: CodePointer;
  17.       IIDStrRef   : ^pshortstring; { never nil. Com: upper(GuidToString(IID^)) }
  18.       IType       : tinterfaceentrytype;
  19.     );
  20. end;

If I try to get IIDStr, I'll got GUID. But GUID for CORBA is useless because CORBA-interfaces are defined by their name.
How can I get name of corba interface by its enrty?

You check for IIDRef, but then use IIDStrRef. For COM interfaces both are set to the value of the GUID, for CORBA/raw interfaces IIDRef will be Nil and IIDStrRef will point to the string used in the interface's declaration.
Code: Pascal  [Select][+][-]
  1. program tintftest;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.   {$Interfaces Corba}
  7.   ITestCorba = interface
  8.     ['Foobar']
  9.     procedure Test1;
  10.   end;
  11.  
  12.   {$Interfaces Com}
  13.   ITestCom = interface
  14.     ['{6C56CBBE-4C7B-4CFE-868C-1CFD59E09929}']
  15.     procedure Test2;
  16.   end;
  17.  
  18. type
  19.   TTest = class(TInterfacedObject, ITestCorba, ITestCom)
  20.     procedure Test1;
  21.     procedure Test2;
  22.   end;
  23.  
  24. procedure TTest.Test1;
  25. begin
  26.  
  27. end;
  28.  
  29. procedure TTest.Test2;
  30. begin
  31.  
  32. end;
  33.  
  34. var
  35.   table: PInterfaceTable;
  36.   i: LongInt;
  37. begin
  38.   table := TTest.GetInterfaceTable;
  39.   for i := 0 to table^.EntryCount - 1 do begin
  40.     Writeln('Entry ', i, ': IID = ', Assigned(table^.Entries[i].IID), ', IIDStr = ', Assigned(table^.Entries[i].IIDStr));
  41.     if Assigned(table^.Entries[i].IIDStr) then
  42.       Writeln('   ', table^.Entries[i].IIDStr^);
  43.   end;
  44. end.

Output:

Code: [Select]
PS C:\fpc\git> .\testoutput\tintftest.exe
Entry 0: IID = FALSE, IIDStr = TRUE
   Foobar
Entry 1: IID = TRUE, IIDStr = TRUE
   {6C56CBBE-4C7B-4CFE-868C-1CFD59E09929}

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #26 on: July 24, 2026, 06:41:07 am »
Good code. Did not know that.
What i did know is that this works in the context of that example
Code: Pascal  [Select][+][-]
  1. writeln('CORBA :', supports(TTest,ITestCorba));
  2. writeln('CORBA :', Supports(TTest,'Foobar'));
  3. writeln('COM   :', Supports(TTest,ITestCom));
  4. writeln('COM   :', Supports(TTest,'{6C56CBBE-4C7B-4CFE-868C-1CFD59E09929}'));
I guess you can see that CORBA needs a shortstring comparison for that
Code: Pascal  [Select][+][-]
  1. if assigned(result^.iidstr) and (result^.iidstr^ = iidstr) then
But the GUID compare for COM also does not look optimal. I expected the comparison to be done over two qwords on 64 bit and maybe four dwords on 32 bit. Now the comparison visits every single TGUID field.(albeit binary compare)
This would be measurably faster on 64 bit and is endianness agnostic:
Code: Pascal  [Select][+][-]
  1. function GUIDEqual(const A, B: TGUID): Boolean; inline;
  2. type
  3.   TGUID64 = packed record
  4.     Lo, Hi: QWord;
  5.   end;
  6. begin
  7.   Result :=
  8.     (TGUID64(A).Lo = TGUID64(B).Lo)  // these are intrinsics
  9.     and
  10.     (TGUID64(A).Hi = TGUID64(B).Hi);
  11. end;
similar for 4 X dword, but there the speed gain would be less noticeable.

Unleashed:
Code: Pascal  [Select][+][-]
  1. Result := Uint128(a) = Uint128(b);

« Last Edit: July 24, 2026, 07:37:39 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

BildatBoffin

  • Jr. Member
  • **
  • Posts: 57
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #27 on: July 24, 2026, 09:07:59 am »
You guys are silly. CORBA interfaces are just a guarantee of an API. If you want a name you dont even need the RTTIs. Just define a base interface.

Code: Pascal  [Select][+][-]
  1.   IBaseInterface = interface
  2.     function iname(): string;
  3.   end;

and then you just derive everything from that.

Code: Pascal  [Select][+][-]
  1.   IProvidesomething = interface (IBaseInterface)
  2.     procedure provideSomthg();
  3.   end;
« Last Edit: July 24, 2026, 09:13:10 am by BildatBoffin »

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #28 on: July 24, 2026, 09:22:01 am »
That answer is a thorough misunderstanding of the subject.
Please re-read. The subject is about identifying an interface with standard methods. Preferably RTTI.
PascalDragon provided the authorative answer. I merely looked at what happens under the hood and found a micro-optimization with noticeable speed gain for COM, not CORBA.
« Last Edit: July 24, 2026, 09:28:39 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

BildatBoffin

  • Jr. Member
  • **
  • Posts: 57
Re: How can I get names of CORBA-interfaces via rtti?
« Reply #29 on: July 24, 2026, 09:37:08 am »
I totally get how you (non-nominative) are thinking. I wanted to show how things can be much more simple.

 

TinyPortal © 2005-2018