{
    This file is part of chepersy
    Copyright (c) 2004-2008 by Anton Rzheshevski (chebmaster@mail.ru),

    See the file COPYING.CPS, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
//Изначально этот модуль создавался потому, что FreePascal 1.06 не поддерживал
// динамические массивы, а потом, когда в 1.9.2 их-таки добавили, то
// наработанный набор дополнительных фич (Add, Last, SetRect, и т.д.)
// оказался настолько удобным, что не стал отказываться...

// They promise to add generics in 2.10.
// Until then, the implementation is ugly. But it works.
{
  TAOP = array of pointer;
  TAOC = array of cardinal;
  TAOI = array of integer;
  TAOS = array of string;
  TAOB = array of LongBool;
  TAOO = array of TObject;
  T2DAOI = array of array of Integer;
  T2DAOS = array of array of String;
  T2DAOO = array of array of TObject;
}
  TDyna = Class(TObject)
    Constructor Create;
    Destructor Destroy; Override;
    procedure Resize(NewLen: integer);
    procedure Increment;
    procedure Decrement;
    procedure ClearMem; virtual; abstract;
    procedure SetRect(a, b: integer); virtual;
    procedure Save(); virtual; abstract;
    procedure Load(Lsz: integer); virtual; abstract;
  Protected
    s, sm, es: integer;
    CurrentMmb: integer;
    function GetSize: integer;
    function GetHigh: integer;
    procedure _Resize(L: integer); virtual; abstract;
    function _ReadDPTR: pointer; virtual; abstract;
  Public
    property High: integer read GetHigh;
    property Length: integer read s write Resize;
    property DPTR:pointer read _ReadDPTR;
  End;
  CDyna = Class of TDyna;
  TAOS = Class(TDyna)
    Function Add(i: AnsiString): integer; virtual;
    procedure ClearMem; override;
  Protected
    m: packed array of AnsiString;
    function _Read(ind: cardinal): AnsiString;
    procedure _Write(ind: cardinal; p: AnsiString);
    function _ReadDPTR: pointer; override;
    function _ReadLast: AnsiString;
    function _ReadNext: AnsiString;
    function _ReadFirst: AnsiString;
    procedure _WriteLast(p: AnsiString);
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:AnsiString read _Read write _Write; DEFAULT;
    property Last:AnsiString read _ReadLast write _WriteLast;
    property First: AnsiString read _ReadFirst;
    property Next: AnsiString read _ReadNext;
  End;
  T2DAOS = Class(TDyna)
    procedure SetRect(z, b: integer); OVERRIDE;
    procedure ClearMem; OVERRIDE;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TAOS;
    procedure _Write(ind: cardinal; p: TAOS);
    function _ReadLast: TAOS;
    procedure _WriteLast(z: TAOS);
    function _ReadDPTR: pointer; override;
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TAOS read _Read write _Write; DEFAULT;
    property Last:TAOS read _ReadLast write _WriteLast;
  End;
  TAOW = Class(TDyna)
    Function Add(i: WideString): integer; virtual;
    procedure ClearMem; override;
  Protected
    m: packed array of WideString;
    function _Read(ind: cardinal): WideString;
    procedure _Write(ind: cardinal; p: WideString);
    function _ReadDPTR: pointer; override;
    function _ReadLast: WideString;
    function _ReadNext: WideString;
    function _ReadFirst: WideString;
    procedure _WriteLast(p: WideString);
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:WideString read _Read write _Write; DEFAULT;
    property Last:WideString read _ReadLast write _WriteLast;
    property First: WideString read _ReadFirst;
    property Next: WideString read _ReadNext;
  End;
  T2DAOW = Class(TDyna)
    procedure SetRect(z, b: integer); OVERRIDE;
    procedure ClearMem; OVERRIDE;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TAOW;
    procedure _Write(ind: cardinal; p: TAOW);
    function _ReadLast: TAOW;
    procedure _WriteLast(z: TAOW);
    function _ReadDPTR: pointer; override;
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TAOW read _Read write _Write; DEFAULT;
    property Last:TAOW read _ReadLast write _WriteLast;
  End;
  TAOF = Class(TDyna)
    Function Add(i: float): integer; virtual;
    procedure ClearMem; override;
  Protected
    m: packed array of float;
    function _Read(ind: cardinal): float;
    procedure _Write(ind: cardinal; p: float);
    function _ReadDPTR: pointer; override;
    function _ReadLast: float;
    function _ReadNext: float;
    function _ReadFirst: float;
    procedure _WriteLast(p: float);
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:float read _Read write _Write; DEFAULT;
    property Last:float read _ReadLast write _WriteLast;
    property First: float read _ReadFirst;
    property Next: float read _ReadNext;
  End;
  T2DAOF = Class(TDyna)
    procedure SetRect(z, b: integer); OVERRIDE;
    procedure ClearMem; OVERRIDE;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TAOF;
    procedure _Write(ind: cardinal; p: TAOF);
    function _ReadLast: TAOF;
    procedure _WriteLast(z: TAOF);
    function _ReadDPTR: pointer; override;
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TAOF read _Read write _Write; DEFAULT;
    property Last:TAOF read _ReadLast write _WriteLast;
  End;
  TAOC = Class(TDyna)
    Function Add(i: cardinal): integer; virtual;
    procedure ClearMem; override;
  Protected
    m: packed array of cardinal;
    function _Read(ind: cardinal): cardinal;
    procedure _Write(ind: cardinal; p: cardinal);
    function _ReadDPTR: pointer; override;
    function _ReadLast: cardinal;
    function _ReadNext: cardinal;
    function _ReadFirst: cardinal;
    procedure _WriteLast(p: cardinal);
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:cardinal read _Read write _Write; DEFAULT;
    property Last:cardinal read _ReadLast write _WriteLast;
    property First: cardinal read _ReadFirst;
    property Next: cardinal read _ReadNext;
  End;
  T2DAOC = Class(TDyna)
    procedure SetRect(z, b: integer); OVERRIDE;
    procedure ClearMem; OVERRIDE;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TAOC;
    procedure _Write(ind: cardinal; p: TAOC);
    function _ReadLast: TAOC;
    procedure _WriteLast(z: TAOC);
    function _ReadDPTR: pointer; override;
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TAOC read _Read write _Write; DEFAULT;
    property Last:TAOC read _ReadLast write _WriteLast;
  End;
  TAOI = Class(TDyna)
    Function Add(i: integer): integer; virtual;
    procedure ClearMem; override;
  Protected
    m: packed array of integer;
    function _Read(ind: cardinal): integer;
    procedure _Write(ind: cardinal; p: integer);
    function _ReadDPTR: pointer; override;
    function _ReadLast: integer;
    function _ReadNext: integer;
    function _ReadFirst: integer;
    procedure _WriteLast(p: integer);
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:integer read _Read write _Write; DEFAULT;
    property Last:integer read _ReadLast write _WriteLast;
    property First: integer read _ReadFirst;
    property Next: integer read _ReadNext;
  End;
  T2DAOI = Class(TDyna)
    procedure SetRect(z, b: integer); OVERRIDE;
    procedure ClearMem; OVERRIDE;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TAOI;
    procedure _Write(ind: cardinal; p: TAOI);
    function _ReadLast: TAOI;
    procedure _WriteLast(z: TAOI);
    function _ReadDPTR: pointer; override;
    procedure _Resize(L: integer); override;
  Public
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TAOI read _Read write _Write; DEFAULT;
    property Last:TAOI read _ReadLast write _WriteLast;
  End;
  TAOMO = Class(TDyna)
    Function Add(i: TManagedObject): integer; virtual;
    procedure ClearMem; override;
  Protected
    a: array of pointer;
    function _Read(ind: cardinal): TManagedObject;
    procedure _Write(ind: cardinal; p: TManagedObject);
    function _ReadDPTR: pointer; override;
    function _ReadLast: TManagedObject;
    function _ReadNext: TManagedObject;
    function _ReadFirst: TManagedObject;
    procedure _WriteLast(p: TManagedObject);
    procedure _Resize(L: integer); override;
  Public
    Container: boolean;
    procedure Save(); override;
    procedure Load(Lsz: integer); override;
    property D[index: cardinal]:TManagedObject read _Read write _Write; DEFAULT;
    property Last:TManagedObject read _ReadLast write _WriteLast;
    property First: TManagedObject read _ReadFirst;
    property Next: TManagedObject read _ReadNext;
  End;
Procedure SafeFree(var o: TObject);

