Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2016-11

FPC SetDynArrayProp

7 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ondrej Pokorny

Hello!

What is the correct way to set a published dynamic array property in FPC? In Delphi, you use SetDynArrayProp.

Is SetObjectProp safe (packages\fcl-web\src\base\restbase.pp uses this approach)? See the attached project.

Ondrej

Reply by email

silvioprog
to Ondrej Pokorny
Ondrej Pokorny wrote 9 lines
Hello! What is the correct way to set a published dynamic array property in FPC? In Delphi, you use SetDynArrayProp. Is SetObjectProp safe (packages\fcl-web\src\base\restbase.pp uses this approach)? See the attached project. Ondrej

It seems a great time to including the
tkDynArray/GetDynArrayProp()/SetDynArrayProp()
to RTL.

Could you open an issue?

Silvio Clécio

Reply by email

silvioprog
to Ondrej Pokorny
Ondrej Pokorny wrote 10 lines
On 28.11.2016 17:37, Ondrej Pokorny wrote: > Is SetObjectProp safe? > OK, no it is not - it doesn't increment the reference count for properties without a setter: property Data: TBytes write FBytes; Ondrej

Yes, and it seems you can't handle properties setting field, eg: "property Data: TBytes read FData write FData".

An workaround just for testing:

procedure SetDynArrayProp(AInstance: TObject; APropInfo: PPropInfo; const
AValue);
begin
  // Caution: it works only with fields! But we can implement the full
version checking methods too, getting some ideas from SetRawInterfaceProp
  CopyArray(PPointer(Pointer(AInstance)+PtrUInt(APropInfo^.SetProc)),
@AValue, APropInfo^.PropType, 1);
end;
...
//    {$IFDEF FPC}
    SetDynArrayProp(O, PropInfo, A);
//    {$ELSE}
//    SetDynArrayProp(O, PropInfo, A);
//    {$ENDIF}
...

Anyway including (Get)SetDynArrayProp() to typinfo solve the problem because it can check if property's set is a field or a method.

Silvio Clécio

Reply by email

silvioprog
to silvioprog

On Mon, Nov 28, 2016 at 3:25 PM, silvioprog wrote: [...]

Show quoted text 5 lines
Yes, and it seems you can't handle properties setting field, eg: "property Data: TBytes read FData write FData". An workaround just for testing:

*A workaround just for testing

Silvio Clécio

Reply by email

silvioprog
to silvioprog: Yes, and it seems you can't handle properties setting field, eg: "property Data: TBytes…

On Mon, Nov 28, 2016 at 3:25 PM, silvioprog wrote: [...]

Anyway including (Get)SetDynArrayProp() to typinfo solve the problem because it can check if property's set is a field or a method.

This version (inspired by FPC's SetRawInterfaceProp()) can handle property (indexed or not) setting fields or methods:

procedure SetDynArrayProp(AInstance: TObject; APropInfo: PPropInfo; const
AValue: Pointer);
type
  TSetPointerProcIndex=procedure(index:longint;const i:Pointer) of object;
  TSetPointerProc=procedure(i:Pointer) of object;
var
  AMethod: TMethod;
begin
  case (APropInfo^.PropProcs shr 2) and 3 of
    ptField:
      CopyArray(PPointer(Pointer(AInstance)+PtrUInt(APropInfo^.SetProc)),
@AValue, APropInfo^.PropType, 1);
    ptStatic,
    ptVirtual:
      begin
        if ((APropInfo^.PropProcs shr 2) and 3)=ptStatic then
          AMethod.Code:=APropInfo^.SetProc
        else
AMethod.Code:=PCodePointer(Pointer(AInstance.ClassType)+PtrUInt(APropInfo^.SetProc))^;
        AMethod.Data:=AInstance;
        if ((APropInfo^.PropProcs shr 6) and 1)0 then
          TSetPointerProcIndex(AMethod)(APropInfo^.Index,AValue)
        else
          TSetPointerProc(AMethod)(AValue);
      end;
  end;
end;

It can be a starting for implementing it at typinfo definitively.

Silvio Clécio

Reply by email