1

我想做一个程序来接受通用约束数组,即 ecgReadings 和 eegReadings:

Types declarations:
   subtype ecgReadingsSize is Natural range 1..3;
   subtype eegReadingsSize is Natural range 1..10;
   subtype eegReading is Natural range 0..1; -- eegRReading is 0 or 1
   subtype ecgReading is Natural range 2..600; -- Max heart rate 220

   
   type ecgReadings is array (ecgReadingsSize) of ecgReading;
   type eegReadings is array (eegReadingsSize) of eegReading;
   type eegPartialSums is array (eegReadingsSize) of Natural;

尝试制定通用程序:

package iocontroller is

   
   generic 
      type ItemType is private;
      type Index_Type is (<>);  -- Any discrete type
      type Array_Type is array (Index_Type range <>) of ItemType;
  procedure startIO(inFileName : String; outFileName : String; List: 
  Array_Type);

测试通用产品

procedure Main is --change name

type MyArray is array (Natural range <>) of Natural;

procedure ecgIO is new startIO(
        ItemType => Natural,
        Index_Type => Natural,
        Array_Type => MyArray
    );
4

1 回答 1

4

我认为您只需要约束的泛型类型参数(请参见下面Array_TypeStart_IO示例)。

注意虽然这不是强制性的,但在这种情况下,您可能希望声明类型而不是子类型,以防止从例如ECG_Reading到的意外(隐式)转换EEG_Reading,即声明

type ECG_Reading_Index is range 1 .. 3;
type ECG_Reading       is range 2 .. 600;

代替

subtype ECG_Reading_Index is Natural range 1 .. 3;
subtype ECG_Reading       is Natural range 2 .. 600;

和 类似EEG_Reading_IndexEEG_Reading

但除此之外:

主文件

with IO_Controller;

procedure Main is
   
   subtype ECG_Reading_Index is Natural range 1 .. 3;
   subtype ECG_Reading       is Natural range 2 .. 600;
   type    ECG_Readings      is array (ECG_Reading_Index) of ECG_Reading;
   
   procedure Start_ECG_IO is new IO_Controller.Start_IO
     (Item_Type  => ECG_Reading,
      Index_Type => ECG_Reading_Index,
      Array_Type => ECG_Readings);   
   
   
   subtype EEG_Reading_Index is Natural range 1 .. 10;
   subtype EEG_Reading       is Natural range 0 .. 1;
   type    EEG_Readings      is array (EEG_Reading_Index) of EEG_Reading;
   
   procedure Start_EEG_IO is new IO_Controller.Start_IO
     (Item_Type  => EEG_Reading,
      Index_Type => EEG_Reading_Index,
      Array_Type => EEG_Readings);
   

   ECG : ECG_Readings := (others => <>);
   EEG : EEG_Readings := (others => <>);
   
begin
   Start_ECG_IO ("ecg_in", "ecg_out", ECG);
   Start_EEG_IO ("eeg_in", "eeg_out", EEG);
end Main;

io_controller.ads

package IO_Controller is   
   
   generic 
      type Item_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type) of Item_Type;   --  remove "range <>"
   procedure Start_IO
     (FileName_In  : String; 
      Filename_Out : String;
      List         : Array_Type);
 
end IO_Controller;
于 2020-07-05T14:41:19.307 回答