Skip to content

Commit afbe277

Browse files
committed
Introduced TPyClassWrapper<T: class> a generic wrapper for pascal classes
1 parent 02dd57b commit afbe277

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

Source/WrapDelphi.pas

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,21 @@ TPyDelphiObject = class (TPyInterfacedObject, IFreeNotificationSubscriber)
587587
end;
588588
TPyDelphiObjectClass = class of TPyDelphiObject;
589589

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+
590605
{ This class will simply hold a Python object in its Value property.
591606
This is required for Delphi var parameters because Python won't let you
592607
replace a parameter value with another one, so, we will provide a container
@@ -842,7 +857,7 @@ TPyDelphiWrapper = class(TEngineClient, IFreeNotificationSubscriber)
842857
procedure Finalize; override;
843858
procedure DefineVar(const AName : string; const AValue : Variant); overload;
844859
procedure DefineVar(const AName : string; AValue : TObject); overload;
845-
procedure RegisterDelphiWrapper(AWrapperClass : TPyDelphiObjectClass);
860+
function RegisterDelphiWrapper(AWrapperClass : TPyDelphiObjectClass): TPythonType;
846861
function RegisterHelperType(APyObjectClass : TPyObjectClass) : TPythonType;
847862
function RegisterFunction(AFuncName : PAnsiChar; AFunc : PyCFunction; ADocString : PAnsiChar ): PPyMethodDef; overload;
848863
function RegisterFunction(AFuncName : PAnsiChar; AFunc : TDelphiMethod; ADocString : PAnsiChar ): PPyMethodDef; overload;
@@ -3144,6 +3159,25 @@ function TPyDelphiMethodObject.Call(ob1, ob2: PPyObject): PPyObject;
31443159
end;
31453160
{$ENDIF}
31463161

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+
31473181
function TPyDelphiMethodObject.Repr: PPyObject;
31483182
begin
31493183
with GetPythonEngine do
@@ -3790,8 +3824,8 @@ procedure TPyDelphiWrapper.Notify(ADeletedObject: TObject);
37903824
fEventHandlerList.Delete(i);
37913825
end;
37923826

3793-
procedure TPyDelphiWrapper.RegisterDelphiWrapper(
3794-
AWrapperClass: TPyDelphiObjectClass);
3827+
function TPyDelphiWrapper.RegisterDelphiWrapper(
3828+
AWrapperClass: TPyDelphiObjectClass): TPythonType;
37953829
var
37963830
RegisteredClass : TRegisteredClass;
37973831
Index: Integer;
@@ -3814,6 +3848,8 @@ procedure TPyDelphiWrapper.RegisterDelphiWrapper(
38143848
end;
38153849

38163850
fClassRegister.Add(RegisteredClass);
3851+
Result := RegisteredClass.PythonType;
3852+
38173853
if AWrapperClass.DelphiObjectClass.InheritsFrom(TPersistent) then
38183854
Classes.RegisterClass(TPersistentClass(AWrapperClass.DelphiObjectClass));
38193855
end;

0 commit comments

Comments
 (0)