#1 2017-11-29 11:16:48

wloochacz
Member
Registered: 2015-01-03
Posts: 45

Proposal for a new behavior for TObjectDynArrayWrapper/IObjectDynArray

Hello,
TObjectDynArrayWrapper/IObjectDynArray is really useful.
But, by how it works by design is not suitable for using in a server to populate DTO object arrays.
Why?
Because it always destroys the instances of objects on the array that it wraps.
However, the method on the server must pass the created objects, not destroy them when the wrapper is destroyed.

Therefore, I suggest adding the OwnObjects parameter to the TObjectDynArrayWrapper constructor.
This does not change the default behavior of the wrapper.

Something like that:

type
  TObjectDynArrayWrapper = class(TInterfacedObject, IObjectDynArray)
  private
    FOwnsObjects : Boolean;
  protected
    fValue: PPointer;
    fCount: integer;
  public
    /// initialize the wrapper with a one-dimension dynamic array of TObject
    /// If AOwnsObjetcs is True, by default, then wraaper destroy all objects on the list on destroy himself.
    constructor Create(var aValue; AOwnsObjetcs : Boolean = True);
    /// delete all TObject instances, and release the memory
    // - is not to be called for most use, thanks to reference-counting memory
    // handling, but can be handy for quick release
    procedure Clear;
  end;

implmentation

constructor TObjectDynArrayWrapper.Create(var aValue; AOwnsObjetcs : Boolean = True);
begin
  fValue := @aValue;
  FOwnsObjects := AOwnsObjetcs;
end;

procedure TObjectDynArrayWrapper.Clear;
var i: integer;
begin
  if fValue^ <> nil then
  begin
    // free all objects on aray if OwnsObjects is True
    if FOwnsObjects then
      for i := fCount - 1 downto 0 do
      try
        TObjectDynArray(fValue^)[i].Free;
      except
        on Exception do;
      end;

    TObjectDynArray(fValue^) := nil; // set capacity to 0
    fCount := 0;
  end
  else
    if fCount > 0 then
      raise ESynException.Create('You MUST define your IObjectDynArray field BEFORE the corresponding dynamic array');
end;

AB, what do you think?

Offline

#2 2018-02-15 16:44:24

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Proposal for a new behavior for TObjectDynArrayWrapper/IObjectDynArray

I am voting for the extension of the TObjectDynArrayWrapper class by the OwnsObjects parameter.
This change don't break the existing interface (default OwnsObjects is True) and extends class functionality significantly.

Who else supports this idea? :-)

Offline

#3 2018-02-19 03:39:36

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: Proposal for a new behavior for TObjectDynArrayWrapper/IObjectDynArray

Count me in, the OwnsObjects parameter would allow us to control the lifetime of the contained objects.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2018-02-19 09:44:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: Proposal for a new behavior for TObjectDynArrayWrapper/IObjectDynArray

Please see https://synopse.info/fossil/info/8b3827a511

Also note that a good old TDynArray wrapper is able to work with registered T*ObjArray values, as you expect.

Offline

#5 2018-02-19 15:09:00

wloochacz
Member
Registered: 2015-01-03
Posts: 45

Re: Proposal for a new behavior for TObjectDynArrayWrapper/IObjectDynArray

Thank You, AB smile

Offline

Board footer

Powered by FluxBB