You are not logged in.
I am pretty sure casting an unitialized result to an object will fail. I even tested it
function TServiceFactoryServer.CreateInstance: TInterfacedObject;
begin
if fImplementationClassWithCustomCreate then
result := TInterfacedObjectWithCustomCreate(result).Create else
result := fImplementationClass.Create;
end;
What you probably mean is this:
type
TInterfacedObjectWithCustomCreateClass=class of TInterfacedObjectWithCustomCreate;
...
function TServiceFactoryServer.CreateInstance: TInterfacedObject;
begin
if fImplementationClass.InheritsFrom(TInterfacedObjectWithCustomCreate) then
result := TInterfacedObjectWithCustomCreateClass(fImplementationClass).Create
else
result := fImplementationClass.Create;
end;
My guess is you wont need the fImplementationClassWithCustomCreate private field either.
Last edited by h.hasenack (2012-11-06 09:05:24)
Offline
COme to think of it...
TServiceFactoryServer.CreateInstance
could very well return a TObject, there is no reason I can think of to make it return a TInterfacedObject... do you?
Offline
1. fImplementationClassWithCustomCreate is here to speed up the process.
Calling InheritsFrom() is slower.
2. We expect it to be an TInterfaceObject to be able to access the reference count directly.
We could use any TObject, and then call the IInterface members, but I did not want it, since some users may be confused and declare their implementation class as TObject, whereas we need them to implement interfaces.
So it was designed to avoid runtime errors.
Offline
1) Is good reasoning, I agree to that.
2) I mean the return value of type of TServiceFactoryServer.CreateInstance to be TObject rather than TInterfacedObject. The runtime error is avoided by the constructor insisting on a TInterfacedClass anyway.
Dont get me wrong, point 2 is very minor, now I feel like I'm nagging you . I also see now the point of reference counting you do with FSharedinstance. AMOF : just keep it as it is.
Except of course for the error mentioned earlier
Last edited by h.hasenack (2012-11-06 10:06:05)
Offline
I've fixed the issue thanks to your feedback.
See http://synopse.info/fossil/info/646d312153
I should have taken better notice of the compiler warning!
Offline