I am trying to define a generic, inherited TSingleton class. That's what I meant:
TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) strict private class var FInstance : RealClass; protected procedure InstanceInitialization;virtual; public destructor Destroy; override; class procedure Create; reintroduce; class function Instance : RealClass; class procedure InstanceFree; end;
The goal was to be able to "insert" a singleton pattern into the inheritance tree. so instead of declaring something like this:
TMySingletonComponent = class(TComponent) end;
And you need to implement a singleton pattern, I would declare something like this:
TMyGenericSingletonComponent = class(TSingleton<TMyGenericSingletonComponent,TComponent>) end;
Unfortunately this will not work. I get the following error (in D2010):
TSingleton<RealClass, InheritsFrom : class> = class(InheritsFrom) ///E2021 Class type required
Now I was wondering if this would work in Delphi XE? Is there any βclean hackβ that I could use to make this work in D2010? Are there any fundamental reasons why this may not work?
source share