Changeset 342


Ignore:
Timestamp:
Apr 3, 2012, 7:35:26 AM (13 years ago)
Author:
chronos
Message:
  • Added: Update locking support for TGList.
  • Modified: Enhanced TGStream methods.
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Generic/GenericList.inc

    r312 r342  
    2424    FItems: array of TGListItem;
    2525    FCount: TGListIndex;
     26    FUpdateCount: NativeInt;
     27    FOnUpdate: TNotifyEvent;
    2628    function Get(Index: TGListIndex): TGListItem;
    2729    function GetCapacity: TGListIndex;
     
    7274    procedure Sort(Compare: TGListSortCompare);
    7375    procedure SetArray(Values: array of TGListItem);
     76    procedure BeginUpdate;
     77    procedure EndUpdate;
     78    procedure Update;
    7479    property Count: TGListIndex read FCount write SetCount;
    7580    property Capacity: TGListIndex read GetCapacity write SetCapacity;
     
    107112    I := I + 1;
    108113  end;
     114  Update;
    109115end;
    110116
     
    118124    I := I + 1;
    119125  end;
     126  Update;
    120127end;
    121128
     
    130137    I := I + 1;
    131138  end;
     139  Update;
    132140end;
    133141
     
    248256    I := I + 1;
    249257  end;
     258  Update;
    250259end;
    251260
     
    294303  InsertCount(Index, 1);
    295304  FItems[Index] := Item;
     305  Update;
    296306end;
    297307
     
    319329  if Index < FCount then
    320330    System.Move(FItems[Index], FItems[Index + ACount], (FCount - ACount - Index) * SizeOf(TGListItem));
     331  Update;
    321332end;
    322333
     
    409420  //Delete(CurIndex);
    410421  //Insert(NewIndex, Temp);
     422  Update;
    411423end;
    412424
     
    434446    end;
    435447  end;
     448  Update;
    436449end;
    437450
     
    469482    I := I + 1;
    470483  end;
     484  Update;
    471485end;
    472486
     
    475489  if FCount > 1 then
    476490    QuickSort(0, FCount - 1, Compare);
     491  Update;
    477492end;
    478493
     
    486501    I := I + 1;
    487502  end;
     503  Update;
    488504end;
    489505
     
    498514    I := I + 1;
    499515  end;
     516end;
     517
     518procedure TGList.BeginUpdate;
     519begin
     520  Inc(FUpdateCount);
     521end;
     522
     523procedure TGList.EndUpdate;
     524begin
     525  Dec(FUpdateCount);
     526  Update;
     527end;
     528
     529procedure TGList.Update;
     530begin
     531  if Assigned(FOnUpdate) and (FUpdateCount = 0) then FOnUpdate(Self);
    500532end;
    501533
     
    530562  Result := FCount - 1;
    531563  FItems[Result] := Item;
     564  Update;
    532565end;
    533566
     
    545578    J := J + 1;
    546579  end;
     580  Update;
    547581end;
    548582
     
    560594    J := J + 1;
    561595  end;
     596  Update;
    562597end;
    563598
     
    575610  System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TGListItem));
    576611  SetCapacityOptimized(Capacity - 1);
     612  Update;
    577613end;
    578614
     
    586622    I := I + 1;
    587623  end;
     624  Update;
    588625end;
    589626
     
    597634    I := I + 1;
    598635  end;
     636  Update;
    599637end;
    600638
     
    610648  FItems[Index1] := FItems[Index2];
    611649  FItems[Index2] := Temp;
     650  Update;
    612651end;
    613652
  • Generics/TemplateGenerics/Generic/GenericRectangle.inc

    r333 r342  
    1111  function GetTopRight: TGRectanglePoint;
    1212  function GetWidth: TGRectangleDimension;
     13  function GetEmpty: Boolean;
    1314  procedure SetBottom(const AValue: TGRectangleDimension);
    1415  procedure SetBottomLeft(const AValue: TGRectanglePoint);
     
    2122  procedure SetTopLeft(const AValue: TGRectanglePoint);
    2223  procedure SetTopRight(const AValue: TGRectanglePoint);
    23 
    2424  procedure SetWidth(const AValue: TGRectangleDimension);
     25  procedure SetEmpty(const AValue: Boolean);
    2526public
    2627  FLeft: TGRectangleDimension;
     
    5253
    5354  property Size: TGRectanglePoint read GetSize write SetSize;
     55  property Empty: Boolean read GetEmpty write SetEmpty;
    5456end;
    5557
     
    199201procedure TGRectangle.Intersect(Rect1, Rect2: TGRectangle);
    200202begin
    201   Left := Max(Rect1.Left, Rect2.Left);
    202   Top := Max(Rect1.Top, Rect2.Top);
    203   Right := Min(Rect1.Right, Rect2.Right);
    204   Bottom := Min(Rect1.Bottom, Rect2.Bottom);
     203  if Rect1.Empty or Rect2.Empty then Empty := True
     204  else begin
     205    Left := Max(Rect1.Left, Rect2.Left);
     206    Top := Max(Rect1.Top, Rect2.Top);
     207    Right := Min(Rect1.Right, Rect2.Right);
     208    Bottom := Min(Rect1.Bottom, Rect2.Bottom);
     209  end;
    205210end;
    206211
    207212procedure TGRectangle.IntersectWith(Rect: TGRectangle);
    208213begin
    209   Left := Max(Left, Rect.Left);
    210   Top := Max(Top, Rect.Top);
    211   Right := Min(Right, Rect.Right);
    212   Bottom := Min(Bottom, Rect.Bottom);
     214  if Empty or Rect.Empty then Empty := True
     215  else begin
     216    Left := Max(Left, Rect.Left);
     217    Top := Max(Top, Rect.Top);
     218    Right := Min(Right, Rect.Right);
     219    Bottom := Min(Bottom, Rect.Bottom);
     220  end;
    213221end;
    214222
    215223procedure TGRectangle.Union(Rect1, Rect2: TGRectangle);
    216224begin
    217   Left := Min(Rect1.Left, Rect2.Left);
    218   Top := Min(Rect1.Top, Rect2.Top);
    219   Right := Max(Rect1.Right, Rect2.Right);
    220   Bottom := Max(Rect1.Bottom, Rect2.Bottom);
     225  if Rect1.Empty then Assign(Rect2)
     226  else
     227  if Rect2.Empty then Assign(Rect1)
     228  else begin
     229    Left := Min(Rect1.Left, Rect2.Left);
     230    Top := Min(Rect1.Top, Rect2.Top);
     231    Right := Max(Rect1.Right, Rect2.Right);
     232    Bottom := Max(Rect1.Bottom, Rect2.Bottom);
     233  end;
    221234end;
    222235
    223236procedure TGRectangle.UnionWith(Rect: TGRectangle);
    224237begin
    225   Left := Min(Left, Rect.Left);
    226   Top := Min(Top, Rect.Top);
    227   Right := Max(Right, Rect.Right);
    228   Bottom := Max(Bottom, Rect.Bottom);
     238  if Empty then Assign(Rect)
     239  else
     240  if not Rect.Empty then begin
     241    Left := Min(Left, Rect.Left);
     242    Top := Min(Top, Rect.Top);
     243    Right := Max(Right, Rect.Right);
     244    Bottom := Max(Bottom, Rect.Bottom);
     245  end;
     246end;
     247
     248function TGRectangle.GetEmpty: Boolean;
     249begin
     250  Result := (Bottom <= Top) or (Right <= Left);
     251end;
     252
     253procedure TGRectangle.SetEmpty(const AValue: Boolean);
     254begin
     255  Top := 0;
     256  Bottom := 0;
     257  Left := 0;
     258  Right := 0;
    229259end;
    230260
  • Generics/TemplateGenerics/Generic/GenericStream.inc

    r312 r342  
    1313  procedure Write(Item: TGStreamItem); virtual; abstract;
    1414  procedure WriteArray(Item: array of TGStreamItem); virtual; abstract;
     15  procedure WriteStream(Stream: TGStream; Count: TGStreamIndex); virtual; abstract;
    1516  function Read: TGStreamItem; virtual; abstract;
    1617  function ReadArray(Count: TGStreamIndex): TGStreamItemArray; virtual; abstract;
     18  function ReadStream(Stream: TGStream; Count: TGStreamIndex): TGStreamIndex; virtual; abstract;
    1719  function Insert(Count: TGStreamIndex): TGStreamIndex; virtual; abstract;
    1820  function Remove(Count: TGStreamIndex): TGStreamIndex; virtual; abstract;
  • Generics/TemplateGenerics/Specialized/SpecializedList.pas

    r304 r342  
    9595  procedure AddStream(Stream: TStream);
    9696  procedure AddStreamPart(Stream: TStream; ItemCount: TGListIndex);
     97  procedure WriteBuffer(var Buffer; Count: Integer);
     98  procedure ReadBuffer(var Buffer; Count: Integer);
    9799end;
    98100
     
    417419end;
    418420
     421procedure TListByte.WriteBuffer(var Buffer; Count: Integer);
     422begin
     423
     424end;
     425
     426procedure TListByte.ReadBuffer(var Buffer; Count: Integer);
     427begin
     428
     429end;
     430
    419431end.
  • Generics/TemplateGenerics/Specialized/SpecializedStream.pas

    r312 r342  
    66
    77uses
    8   Classes, SysUtils, SpecializedList;
     8  Classes, SysUtils, SpecializedList, DateUtils;
    99
    1010type
     
    2929{$DEFINE TGStreamItem := Byte}
    3030{$DEFINE TGStreamList := TListStreamByte}
    31 {$DEFINE TGStream := TStreamByte}
     31{$DEFINE TGStream := TBaseStreamByte}
    3232{$DEFINE TGStreamSortCompare := TStreamByteSortCompare}
    3333{$DEFINE TGStreamToStringConverter := TStreamByteToStringConverter}
     
    4949{$I 'GenericStream.inc'}
    5050
     51  TStreamByte = class(TBaseStreamByte)
     52    function ReadBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
     53    function WriteBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
     54  end;
     55
     56  { TMemoryStreamByte }
     57
     58  TMemoryStreamByte = class(TStreamByte)
     59  private
     60    FList: TListByte;
     61    FOwnsList: Boolean;
     62    FPosition: Integer;
     63  public
     64    procedure Assign(Source: TBaseStreamByte); override;
     65    procedure Write(Item: Byte); override;
     66    procedure WriteArray(Values: array of Byte); override;
     67    procedure WriteList(List: TListByte);
     68    function WriteBuffer(var Buffer; Count: Integer): Integer; override;
     69    procedure WriteStream(Stream: TBaseStreamByte; Count: Integer); override;
     70    function Read: Byte; override;
     71    function ReadArray(Count: Integer): TStreamByteItemArray; override;
     72    function ReadList(List: TListByte; Count: Integer): Integer;
     73    function ReadBuffer(var Buffer; Count: Integer): Integer; override;
     74    function ReadStream(Stream: TBaseStreamByte; Count: Integer): Integer; override;
     75    function Insert(Count: Integer): Integer; override;
     76    function Remove(Count: Integer): Integer; override;
     77    function Seek(Offset: Integer; Origin: TSeekOrigin = soCurrent): Integer; override;
     78    constructor Create; override; overload;
     79    constructor Create(AList: TListByte); overload;
     80    destructor Destroy; override;
     81    property OwnsList: Boolean read FOwnsList write FOwnsList;
     82    property List: TListByte read FList;
     83  end;
     84
     85
     86implementation
     87
    5188{ TMemoryStreamByte }
    5289
    53 TMemoryStreamByte = class(TStreamByte)
    54 private
    55   FList: TListByte;
    56   FPosition: Integer;
    57 public
    58   procedure Assign(Source: TStreamByte); override;
    59   procedure Write(Item: Byte); override;
    60   procedure WriteArray(Values: array of Byte); override;
    61   procedure WriteList(List: TListByte);
    62   function Read: Byte; override;
    63   function ReadArray(Count: Integer): TStreamByteItemArray; override;
    64   function ReadList(List: TListByte; Count: Integer): Integer;
    65   function Insert(Count: Integer): Integer; override;
    66   function Remove(Count: Integer): Integer; override;
    67   function Seek(Offset: Integer; Origin: TSeekOrigin = soCurrent): Integer; override;
    68   constructor Create; override;
    69   destructor Destroy; override;
    70   property List: TListByte read FList;
    71 end;
    72 
    73 
    74 implementation
    75 
    76 { TMemoryStreamByte }
    77 
    78 procedure TMemoryStreamByte.Assign(Source: TStreamByte);
     90procedure TMemoryStreamByte.Assign(Source: TBaseStreamByte);
    7991begin
    8092  inherited;
     
    106118end;
    107119
     120procedure TMemoryStreamByte.WriteStream(Stream: TBaseStreamByte; Count: Integer);
     121begin
     122
     123end;
     124
     125function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
     126begin
     127
     128end;
     129
    108130function TMemoryStreamByte.Read: Byte;
    109131begin
     
    125147end;
    126148
     149function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
     150begin
     151
     152end;
     153
     154function TMemoryStreamByte.ReadStream(Stream: TBaseStreamByte; Count: Integer
     155  ): Integer;
     156begin
     157
     158end;
     159
    127160function TMemoryStreamByte.Insert(Count: Integer): Integer;
    128161begin
     
    154187  inherited;
    155188  FList := TListByte.Create;
     189  OwnsList := True;
     190end;
     191
     192constructor TMemoryStreamByte.Create(AList: TListByte);
     193begin
     194  inherited Create;
     195  FList := AList;
     196  OwnsList := False;
    156197end;
    157198
    158199destructor TMemoryStreamByte.Destroy;
    159200begin
    160   FList.Free;
     201  if OwnsList then FList.Free;
    161202  inherited Destroy;
    162203end;
     
    181222{$DEFINE TGStreamItem := Byte}
    182223{$DEFINE TGStreamList := TListStreamByte}
    183 {$DEFINE TGStream := TStreamByte}
     224{$DEFINE TGStream := TBaseStreamByte}
    184225{$DEFINE TGStreamSortCompare := TStreamByteSortCompare}
    185226{$DEFINE TGStreamToStringConverter := TStreamByteToStringConverter}
     
    200241{$DEFINE IMPLEMENTATION}
    201242{$I 'GenericStream.inc'}
     243
     244
     245
    202246end.
  • Generics/TemplateGenerics/TemplateGenerics.lpk

    r334 r342  
    1111      <SearchPaths>
    1212        <IncludeFiles Value="Generic"/>
    13         <OtherUnitFiles Value="Specialized;Generic"/>
     13        <OtherUnitFiles Value="Specialized;Generic;Additional"/>
    1414        <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
    1515      </SearchPaths>
     
    2929    <Description Value="Generic classes implemented as templates."/>
    3030    <Version Minor="4"/>
    31     <Files Count="26">
     31    <Files Count="27">
    3232      <Item1>
    3333        <Filename Value="ReadMe.txt"/>
     
    8888      <Item15>
    8989        <Filename Value="Generic\GenericRectangle.inc"/>
     90        <UnitName Value="GenericRectangle"/>
    9091      </Item15>
    9192      <Item16>
     
    133134        <UnitName Value="SpecializedRectangle"/>
    134135      </Item26>
     136      <Item27>
     137        <Filename Value="Additional\UBinarySerializer.pas"/>
     138        <UnitName Value="UBinarySerializer"/>
     139      </Item27>
    135140    </Files>
    136141    <Type Value="RunAndDesignTime"/>
  • Generics/TemplateGenerics/TemplateGenerics.pas

    r333 r342  
    1111  SpecializedQueue, SpecializedSet, SpecializedPoint, SpecializedMatrix,
    1212  SpecializedBitmap, SpecializedStream, SpecializedRectangle,
    13   LazarusPackageIntf;
     13  UBinarySerializer, LazarusPackageIntf;
    1414
    1515implementation
  • PersistentData

    • Property svn:ignore set to
      lib
Note: See TracChangeset for help on using the changeset viewer.