Access to dynamic array with enumeration type

I am working on the standard data class of our software. Here is a minimal example of the current state:

  TDataOne = (Width, Length, Height);
  TDataTwo = (Diameter, Weight);

  TDatStruct = class;

  TDatSubStructOne = class(TDatStruct)
    public
      myData : array[TDataOne] of double;
      procedure writeToFile(AFileName : string);
  end;
  TDatSubStructTwo = class(TDatStruct)
    public
      myData : array[TDataTwo] of double;
      procedure writeToFile(AFileName : string);
  end;

To reduce code redundancy, I would like to introduce a data structure with a common write function in TDatStruct. Problems (at least for me as an amateur):

  • mydata has a variable size depending on the substructure and the bases on different types of transfers
  • I still want to access mydataenumerated type in substructures
  • I do not want to pass substructure data as a parameter to the general write function due to the number of different arrays in the real code

( 3), , - , :

writeToFile(AFileName : string; writeData : array of double);

? ?

+4
2

mydata

Delphi Low High
.

mydata

succ pred, .

-

... array of double, .

writeToFile :

procedure writeArrayToFile(const AFileName : string; const writeData : array of double);
var
  i: integer;
begin
  for i:= low(writeData) to High(writeData) do begin
    WriteADoubleToFile(AFilename, writeData[i]);
  end; {for i}
end;

const, , , .

, :

procedure TDatStruct.writeToFile(const AFileName : string);
begin
  WriteArrayToFile(FileName, GetMyData^);
end;

, TDatStruct , , .

type 
  TMyArray = array[0..0] of double;
  PMyArray = ^TMyArray; 

.....
 TDatStruct = class
 protected 
   function GetMyData: PMyArray; virtual; abstract;  
 public
   procedure WriteToFile(const Filename: string); //no need for virtual  
 end;

WriteArrayToFile .

, writeToFile " ", , .

(double/string ..).
.

? ?

.
, .
, Ord.
256 . , , .

function FixedArrayToDynamicArray(const input: array of double): TArray<double>;  
begin
  //translate a fixed array to a dynamic array.  
  SetLength(Result, High(input)); 
  Move(input[0], Result[0], SizeOf(Double) * High(input));
end;

pre-generics :

type 
  TDoubleArray = array of double; 

function FixedArrayToDynamicArray(const input: array of double): TDoubleArray;  
... same from here on. 
+4

, , .

, WriteToFile TDatStruct :

TDatStruct = class
  public
    procedure WriteToFile(AFileName: string); virtual; abstract;

-:

TDatStructOne = class (TDatStruct)
  ...
  public
    procedure WriteToFile(AFileName: string); override;

, TDatStruct, TDatStructOne TDatStructTwo, , , "", : .

, , . :

TDatStructOne = class (TDatStruct)
  public
    Width: Double;
    Length: Double;
    Height: Double;
    procedure WriteToFile(AFileName: string); override;
end;

TDatStructTwo = class (TDatStruct)
  public
    Diameter: Double;
    Weight: Double;
    procedure WriteToFile(AFileName: string); override;
end;

WriteToFile .

, :

  • , , (, , ..).
  • , TDatStructOne TDatStructTwo ( )
  • , . " ". obj.SaveToFile(FileName), .
0

All Articles