@@ -587,6 +587,21 @@ TPyDelphiObject = class (TPyInterfacedObject, IFreeNotificationSubscriber)
587
587
end ;
588
588
TPyDelphiObjectClass = class of TPyDelphiObject;
589
589
590
+
591
+ { Generic wrapper for pascal classes
592
+ Can be used from unit wrappers as follows:
593
+ APyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TMyClass>);
594
+ or at runtime (e.g. inside the FormCreate handler:
595
+ PyDelphiWrapper1.RegisterDelphiWrapper(TPyClassWrapper<TMyClass>).Initialize;}
596
+ TPyClassWrapper<T: class > = class (TPyDelphiObject)
597
+ function GetDelphiObject : T;
598
+ procedure SetDelphiObject (const Value : T);
599
+ public
600
+ class function DelphiObjectClass : TClass; override;
601
+ // Properties
602
+ property DelphiObject: T read GetDelphiObject write SetDelphiObject;
603
+ end ;
604
+
590
605
{ This class will simply hold a Python object in its Value property.
591
606
This is required for Delphi var parameters because Python won't let you
592
607
replace a parameter value with another one, so, we will provide a container
@@ -842,7 +857,7 @@ TPyDelphiWrapper = class(TEngineClient, IFreeNotificationSubscriber)
842
857
procedure Finalize ; override;
843
858
procedure DefineVar (const AName : string; const AValue : Variant); overload;
844
859
procedure DefineVar (const AName : string; AValue : TObject); overload;
845
- procedure RegisterDelphiWrapper (AWrapperClass : TPyDelphiObjectClass);
860
+ function RegisterDelphiWrapper (AWrapperClass : TPyDelphiObjectClass): TPythonType ;
846
861
function RegisterHelperType (APyObjectClass : TPyObjectClass) : TPythonType;
847
862
function RegisterFunction (AFuncName : PAnsiChar; AFunc : PyCFunction; ADocString : PAnsiChar ): PPyMethodDef; overload;
848
863
function RegisterFunction (AFuncName : PAnsiChar; AFunc : TDelphiMethod; ADocString : PAnsiChar ): PPyMethodDef; overload;
@@ -3144,6 +3159,25 @@ function TPyDelphiMethodObject.Call(ob1, ob2: PPyObject): PPyObject;
3144
3159
end ;
3145
3160
{ $ENDIF}
3146
3161
3162
+ { TPyClassWrapper<T> }
3163
+
3164
+ class function TPyClassWrapper <T>.DelphiObjectClass: TClass;
3165
+ begin
3166
+ Result := T;
3167
+ end ;
3168
+
3169
+ function TPyClassWrapper <T>.GetDelphiObject: T;
3170
+ begin
3171
+ Result := T(inherited DelphiObject)
3172
+ end ;
3173
+
3174
+ procedure TPyClassWrapper <T>.SetDelphiObject(const Value : T);
3175
+ begin
3176
+ inherited DelphiObject := Value ;
3177
+ end ;
3178
+
3179
+ { TPyDelphiMethodObject }
3180
+
3147
3181
function TPyDelphiMethodObject.Repr : PPyObject;
3148
3182
begin
3149
3183
with GetPythonEngine do
@@ -3790,8 +3824,8 @@ procedure TPyDelphiWrapper.Notify(ADeletedObject: TObject);
3790
3824
fEventHandlerList.Delete(i);
3791
3825
end ;
3792
3826
3793
- procedure TPyDelphiWrapper.RegisterDelphiWrapper (
3794
- AWrapperClass: TPyDelphiObjectClass);
3827
+ function TPyDelphiWrapper.RegisterDelphiWrapper (
3828
+ AWrapperClass: TPyDelphiObjectClass): TPythonType ;
3795
3829
var
3796
3830
RegisteredClass : TRegisteredClass;
3797
3831
Index: Integer;
@@ -3814,6 +3848,8 @@ procedure TPyDelphiWrapper.RegisterDelphiWrapper(
3814
3848
end ;
3815
3849
3816
3850
fClassRegister.Add(RegisteredClass);
3851
+ Result := RegisteredClass.PythonType;
3852
+
3817
3853
if AWrapperClass.DelphiObjectClass.InheritsFrom(TPersistent) then
3818
3854
Classes.RegisterClass(TPersistentClass(AWrapperClass.DelphiObjectClass));
3819
3855
end ;
0 commit comments