PDA

View Full Version : Corba interfaces



{MSX}
06-05-2005, 12:22 PM
Hi! I'm tring to use CORBA interfaces (that are interface that doesn't derive from IUnknown).
This interfaces work much like java ones, so that you dont' have to define IUnknown methods (addRef, etc, for reference counting) that are necessary for COM.

I'd like to just have an "interface" layer for my classes, i don't want the ref count stuff.

One problem i had is that the interfaces can't be freed by themselves..


type

INamed = interface
function GetName:string;
end;

var n:INamed;
begin
n:=TNamedItem.create;
n.free;
end;


this says that free is not defined in INamed. I can define it and effectively it works, but is it correct ?

Another thing: are this kind of interface only avaiable from freepascal or also in delphi ?

Thanks

savage
06-05-2005, 10:12 PM
Hi Nicola,


Hi! I'm tring to use CORBA interfaces (that are interface that doesn't derive from IUnknown).


You don't have to derive your class from IUnknown in Delphi or Kylix either, and just to clarify, as far as I am aware, they are not known as CORBA interfaces, they are just known as interfaces, while the Win32 variety are known as COM interfaces.



I'd like to just have an "interface" layer for my classes, i don't want the ref count stuff.

As mentioned you can also do this in Delphi/Kylix.



One problem i had is that the interfaces can't be freed by themselves..


I don't think this is a Pascal specific problem, it is just that Java and C# have garbage collectors that go round freeing the stuff for you when they go out of scope. Also since there is no such concept as an IObject base class for all interfaces, that would explain why there is no Free method.

In Delphi, and I assume FreePascal, if you want to reset the interface you would need to assign nil to it.


var
n : INamed;
begin
n := TNamedItem.create;
n.GetName;
n := nil;
end;


Without reference counting how will it know when to clean itself up?

IMHO, use interfaces, but use them for defining a cleaner object hierachy, but when it comes to your application/game use instances of the class instead.

for example:

type
IHuman = interface
function GetName:string;
end;

IMale = interface( IHuman )
function GetName:string;
function MaleChromosome:string;
end;

IFemale = interface( IHuman )
function GetName:string;
function FemaleChromosome:string;
end;

TChild = class( TObject, IMale, IFemale )
function GetName:string;
function MaleChromosome:string;
function FemaleChromosome:string;
end;

var
Kid : TChild ;
begin
Kid := TChild.Create;
try
Kid.GetName;
Kid.MaleChromosome;
Kid.FemaleChromosome;
finally
Kid.Free;
end;
end;


Anyway, just my 2 kubits worth.

Clootie
09-05-2005, 08:32 AM
1) "CORBA" FPC interfaces not supprted in Delphi
2) CORBA interfaces lacks of IUnknown base interface (they are initially just empty) -> not reference counted -> not managed by FPC compiler just because of that
3) even Delphi (standart) interfaces can't be freed - only "released" (in Delphi and FPC automagically by compiler then NIL is assigned to interface variable or variable goes out of scope)

----
So basically you have to think more about how you would like to design your app. And that you try to do with "CORBA" interfaces...

{MSX}
09-05-2005, 10:24 AM
You don't have to derive your class from IUnknown in Delphi or Kylix either, and just to clarify, as far as I am aware, they are not known as CORBA interfaces, they are just known as interfaces, while the Win32 variety are known as COM interfaces.


Unluckly as it turn out this is not true.. In delphi if you declare just "interface" you are really declaring "interface(IUnknown)". You cannot avoid that and COM is the only type of interface supported.

I call them CORBA interfaces becouse you can use them in FPC with the {$INTERFACES CORBA} directive, as opposed with {$INTERFACES COM} that is the default.

As Clootie says i've to choose among using corba interfaces and loosing delphi compatibility or review my design..
I think i'll avoid to drop delphi and kylix (even if i'm doing everithing in FPC now)..

I've noticed anyway that you can declare a "procedure free" on an interface and use that to free the underlying object. I've tested it and it effectively works (Destroy gets called).
So i've created this:


type IFreeable = interface
procedure free;
end;


So one can derive from it and free objects.
That seems to have been a waste of effort anyway :P

savage
09-05-2005, 11:52 AM
From the Delphi help...


Just as all objects descend, directly or indirectly, from TObject, all interfaces derive from the IInterface interface. IInterface provides for dynamic querying and lifetime management of the interface.
.
.
.
IInterface is equivalent to IUnknown. You should generally use IInterface for platform independent applications and reserve the use of IUnknown for specific programs that include Windows dependencies.

QueryInterface provides the means to obtain a reference to the different interfaces that an object supports. _AddRef and _Release provide lifetime memory management for interface references. The easiest way to implement these methods is to derive the implementing class from the System unit's TInterfacedObject.

Does FPC have a TInterfacedObject? hmm, maybe that wouldn't be useful either.

Though I like interfaces, I personally don't tend to use them.