Ada 编程/库/Ada.Streams/示例
此页面提供了一个(相当复杂)的关于类范围流相关属性Class'Read、Class'Write、Class'Input 和 Class'Output 用法的示例。
我们将要考虑的问题如下:假设两台主机通过 TCP 连接进行通信,交换关于车辆的信息。每辆车都以其类型(汽车、卡车、自行车等)、最大速度(以公里/小时为单位,用整数表示)和一组取决于车辆类型的其他参数来表征。例如,汽车可以有一个“乘客数量”参数,而卡车可以有一个“最大载重”(以公斤为单位的整数)参数。为简单起见,我们假设每个参数都用整数表示。
用于通过网络传输车辆数据的协议是基于文本的,如下所示
- 第一个字节是一个字符,表示车辆类型。例如,'c' 表示“汽车”,'t' 表示“卡车”,'b' 表示“自行车”。
- 接下来是车辆速度,表示为一个整数,编码为“<len> i <value>”,其中
- <value> 是速度值,以 10 为基数的数字表示
- <len> 是 <value> 字段的长度,以 10 为基数的数字表示。此字段可以有尾随空格
例如,整数 256 将编码为“3i256”。
- 速度值后面跟着车辆特定参数的列表,使用与速度字段相同的格式进行编码。
我们希望使用 Ada 流的功能从任何“介质”(例如,网络链接、文件、内存中的缓冲区)读取和写入车辆信息,并且我们希望使用 Ada 的面向对象功能来简化引入新类型车辆的操作。
这是提出的解决方案的草图
- 我们将创建一个对象层次结构来表示车辆类型。更准确地说,我们将把每辆车表示为抽象类型 (Abstract_Vehicle) 的后代
- 从流中读取将通过函数 Abstract_Vehicle'Class'Input 完成,该函数的工作方式如下
- 写入流将通过过程 Abstract_Vehicle'Class'Output 完成,该过程的工作方式如下
- 我们将从 Integer 派生一个新的类型 Int,并为它定义新的过程 Int'Read 和 Int'Write,它们将读取和写入上面描述的格式“<len> i <value>”中编码的 Int 类型变量
- 为了允许引入新的车辆类型(可能通过在运行时动态加载库),在上面描述的 Abstract_Vehicle'Class'Input 函数的步骤 2 中,我们不能使用
case来读取字符以确定要创建的对象的类型。相反,我们将使用 Ada 提供的泛型分派构造函数(参见 3.9 带标记类型和类型扩展 (带注释的))。 - 由于泛型分派构造函数要求创建的对象的标记,因此我们必须能够确定对应于给定字符的标记。我们将通过保留一个由字符索引的 Ada.Tags.Tag 数组来实现这一点。定义新车辆的包将在包的初始化部分(即,在
begin之后的语句序列,参见 7.2 包体 (带注释的))“注册”自身,方法是在该数组的适当位置写入定义的车辆的标记。
我们将要分析的第一个包是一个定义新整数类型的包,以便为它分配属性 Read 和 Write,这些属性根据上面描述的格式序列化整数值。包规范非常简单
withAda.Streams;packageStreamable_TypesisuseAda;typeIntisnewInteger;procedurePrint (Stream :notnullaccessStreams.Root_Stream_Type'Class; Item : Int);procedureParse (Stream :notnullaccessStreams.Root_Stream_Type'Class; Item :outInt);forInt'ReaduseParse;forInt'WriteusePrint; Parsing_Error :exception;endStreamable_Types;
新类型是 Int,分配给属性 Read 和 Write 的过程分别是 Parse 和 Read。主体也非常简单
withAda.Strings.Fixed;packagebodyStreamable_TypesisuseStreams; -- --------- -- Print -- -- ---------procedurePrint (Stream :notnullaccessRoot_Stream_Type'Class; Item : Int)isValue : String := Strings.Fixed.Trim (Int'Image (Item), Strings.Left); Len : String := Integer'Image (Value'Length); Complete : String := Len & 'i' & Value; Buffer : Stream_Element_Array (Stream_Element_Offset (Complete'First) .. Stream_Element_Offset (Complete'Last));beginforIinBuffer'RangeloopBuffer (I) := Stream_Element (Character'Pos (Complete (Integer (I))));endloop; Stream.Write (Buffer);endPrint; ----------- -- Parse -- -----------procedureParse (Stream :notnullaccessRoot_Stream_Type'Class; Item :outInt)is-- Variables needed to read from Stream. Buffer : Stream_Element_Array (1 .. 1); Last : Stream_Element_Offset; -- Convenient constants Zero :constantStream_Element := Stream_Element (Character'Pos ('0')); Nine :constantStream_Element := Stream_Element (Character'Pos ('9')); Space :constantStream_Element := Stream_Element (Character'Pos (' '));procedureSkip_SpacesisbeginloopStream.Read (Buffer, Last);exitwhenBuffer (1) /= Space;endloop;endSkip_Spaces;procedureRead_Length (Len :outInteger)isbeginifnot(Buffer (1)inZero .. Nine)thenraiseParsing_Error;endif; Len := 0;loopLen := Len * 10 + Integer (Buffer (1) - Zero); Stream.Read (Buffer, Last);exitwhennot(Buffer (1) in Zero .. Nine);endloop;endRead_Length;procedureRead_Value (Item :outInt; Len :inInteger)isbeginItem := 0;forIin1 .. LenloopStream.Read (Buffer, Last);ifnot(Buffer (1)inZero .. Nine)thenraiseParsing_Error;endif; Item := 10 * Item + Int (Buffer (1) - Zero);endloop;endRead_Value; Len : Integer := 0;beginSkip_Spaces; Read_Length (Len);ifCharacter'Val (Integer (Buffer (1))) /= 'i'thenraiseParsing_Error;endif; Read_Value(Item, Len);endParse;endStreamable_Types;
Streamable_Types 的主体不需要任何特殊注释。请注意,如何通过分派原始过程 Read 和 Write 来访问流,从而允许上面的包与任何类型的流一起工作。
我们将要分析的第二个包是 Vehicles,它定义了一个抽象带标记类型 Abstract_Vehicle,表示所有可能车辆的“最小公分母”。
withAda.Streams;withAda.Tags;withStreamable_Types;packageVehiclesistypeAbstract_Vehicleisabstracttaggedprivate;functionInput_Vehicle (Stream :notnullaccessAda.Streams.Root_Stream_Type'Class)returnAbstract_Vehicle'Class;procedureOutput_Vehicle (Stream :notnullaccessAda.Streams.Root_Stream_Type'Class; Item : Abstract_Vehicle'Class);forAbstract_Vehicle'Class'InputuseInput_Vehicle;forAbstract_Vehicle'Class'OutputuseOutput_Vehicle; -- "Empty" type. The Generic_Dispatching_Constructor expects -- as parameter the type of the parameter of the constructor. -- In this case no parameter is needed, so we define this -- "placeholder type"typeParameter_Recordisnullrecord; -- Abstract constructor to be overriden by non-abstract -- derived types. It is needed by Generic_Dispatching_ConstructorfunctionConstructor (Name :notnullaccessParameter_Record)returnAbstract_Vehicleisabstract;private-- This procedure must be called by the packages that derive -- non-abstract type from Abstract_Vehicle in order to associate -- the vehicle "name" with the tag of the corresponding objectprocedureRegister_Name (Name : Character; Object_Tag : Ada.Tags.Tag);typeKmhisnewStreamable_Types.Int;typeKgisnewStreamable_Types.Int; -- Data shared by all the vehiclestypeAbstract_VehicleisabstracttaggedrecordSpeed : Kmh; Weight : Kg;endrecord;endVehicles;
此包定义了
- 函数 Input_Vehicle 和过程 Output_Vehicle 分别用作类范围输入和输出过程
- 抽象构造函数“Constructor”,每个从 Vehicle 派生的非抽象类型都必须覆盖它。此构造函数将在主体中的 Generic_Dispatching_Constructor 中被调用。
- 过程 Register_Name 将车辆“名称”(在本简化示例中由字符表示)与其对应的类型(由其标记表示)关联起来。在典型情况下,此过程将在从 Abstract_Vehicle 派生的包的体初始化部分被调用
包的主体是
withAda.Tags.Generic_Dispatching_Constructor;packagebodyVehiclesis-- Array used to map vehicle "names" to Ada Tags Name_To_Tag :array(Character)ofAda.Tags.Tag := (others=> Ada.Tags.No_Tag); -- Used as class-wide 'Input functionfunctionInput_Vehicle (Stream :notnullaccessAda.Streams.Root_Stream_Type'Class)returnAbstract_Vehicle'ClassisfunctionConstruct_VehicleisnewAda.Tags.Generic_Dispatching_Constructor (T => Abstract_Vehicle, Parameters => Parameter_Record, Constructor => Constructor); Param :aliasedParameter_Record; Name : Character;useAda.Tags;begin-- Read the vehicle "name" from the stream Character'Read (Stream, Name); -- Check if the name was associated with a tagifName_To_Tag (Name) = Ada.Tags.No_Tag thenraiseConstraint_Error;endif; -- Use the specialization of Generic_Dispatching_Constructor -- defined above to create an object of the correct typedeclareResult : Abstract_Vehicle'Class := Construct_Vehicle (Name_To_Tag (Name), Param'Access);begin-- Now Result is an object of the type associated with -- Name. Call the class-wide Read to fill it with the data -- read from the stream. Abstract_Vehicle'Class'Read (Stream, Result);returnResult;end;endInput_Vehicle;procedureOutput_Vehicle (Stream :notnullaccessAda.Streams.Root_Stream_Type'Class; Item : Abstract_Vehicle'Class)isuseAda.Tags;begin-- The first thing to be written on Stream is the -- character that identifies the type of Item -- We determine it by simply looping over Name_To_TagforNameinName_To_Tag'RangeloopifName_To_Tag (Name) = Item'Tagthen-- Found! Write the character to the stream, then -- use the class-wide Write to finish writing the -- description of Item to the stream Character'Write (Stream, Name); Abstract_Vehicle'Class'Write (Stream, Item); -- We did our duty, we can go backreturn;endif;endloop; -- Note: If we arrive here, we did not find the tag of -- Item in Name_To_Tag.raiseConstraint_Error;endOutput_Vehicle;procedureRegister_Name (Name : Character; Object_Tag : Ada.Tags.Tag)isbeginName_To_Tag (Name) := Object_Tag;endRegister_Name;endVehicles;
请注意 Input_Vehicle 的行为,该函数将扮演类范围输入的角色。
- 首先,它使用流相关的函数 Character'Read 读取与流中下一个车辆关联的字符。
- 随后,它使用读取的字符查找要创建的对象的标记
- 它通过调用 Generic_Dispatching_Constructor 的特定版本来创建对象。
- 它通过调用类范围内的 Read 来“填充”新创建的对象,该 Read 将负责调用与新创建的对象关联的 Read。
过程 Output_Vehicle 比 Input_Vehicle 简单得多,因为它不需要使用 Generic_Dispatching_Constructor。只需注意对 Abstract_Vehicle'Class'Write 的调用,该调用将依次调用与 Item 的实际类型关联的 Write 函数。
最后,请注意 Abstract_Vehicle 没有定义 Read 和 Write 属性。因此,Ada 将使用它们的默认实现。例如,Abstract_Vehicle'Read 将通过两次调用过程 Streamable_Types.Int'Read 来读取两个 Streamable_Types.Int 值 Speed 和 Weight。类似的说明适用于 Abstract_Vehicle'Write。
我们考虑的第一个从 Abstract_Vehicle 派生的非抽象类型表示一辆汽车。为了使示例更丰富一些,Car 将从表示引擎类车辆的中间抽象类型派生。所有引擎类车辆都将有一个字段表示引擎的功率(为简单起见,仍然是整数)。规范文件如下所示
packageVehicles.Engine_BasedistypeAbstract_Engine_BasedisabstractnewAbstract_Vehiclewithprivate;privatetypeAbstract_Engine_BasedisabstractnewAbstract_VehiclewithrecordPower : Streamable_Types.Int;endrecord;endVehicles.Engine_Based;
请注意,在这种情况下,我们也没有定义任何 Read 或 Write 过程。因此,例如,Abstract_Engine_Based'Read 将首先调用 Streamable_Types.Int 两次以从流中读取 Speed 和 Weight(从 Abstract_Vehicle 继承),然后它将再次调用 Streamable_Types.Int 以读取 Power。
另请注意,Abstract_Engine_Based 没有覆盖 Abstract_Vehicle 的抽象函数 Constructor。这是不需要的,因为 Abstract_Engine_Based 是抽象的。
定义 Car 类型的包的规范文件如下所示
packageVehicles.Engine_Based.AutoisuseAda.Streams;typeCarisnewAbstract_Engine_Basedwithprivate;procedureParse (Stream :notnullaccessRoot_Stream_Type'Class; Item :outCar);forCar'ReaduseParse;privatetypeCarisnewAbstract_Engine_BasedwithrecordCilinders : Streamable_Types.Int;endrecord;overridingfunctionConstructor (Param :notnullaccessParameter_Record)returnCar;endVehicles.Engine_Based.Auto;
关于规范文件,无需特别说明。只需注意 Car 定义了一个特殊的 Read 过程,并且它覆盖了 Construct,因为 Car 不是抽象的。
packagebodyVehicles.Engine_Based.AutoisprocedureParse (Stream :notnullaccessRoot_Stream_Type'Class; Item :outCar)isbeginAbstract_Engine_Based'Read (Stream, Abstract_Engine_Based (Item)); Streamable_Types.Int'Read (Stream, Item.Cilinders);endParse;overridingfunctionConstructor (Param :notnullaccessParameter_Record)returnCarisResult : Car;pragmaWarnings(Off, Result);beginreturnResult;endConstructor;beginRegister_Name('c', Car'Tag);endVehicles.Engine_Based.Auto;
Vehicles.Engine_Based.Auto 的主体也非常简单,只需注意
- 过程 Parse(用作 Car'Read)首先调用 Abstract_Engine_Based'Read 来“填充”从 Abstract_Engine_Based 继承的部分,然后调用 Streamable_Types.Int'Read 来读取气缸数量。顺便说一句,请注意这等同于默认行为,因此实际上没有必要定义 Parse。我们只是为了举例。
- 请注意主体初始化部分中对 Register_Name 的调用,该调用将名称“c”与 Car 类型的标记(通过属性“Tag”获得)关联起来。此解决方案的一个有趣的特性是,关于 Car 类型对象的“外部名称”'c' 的信息仅在包 Vehicles.Engine_Based.Auto 内部才知道。
Vehicles.Bicycles 的规范文件
withAda.Streams;packageVehicles.BicyclesisuseAda.Streams;typeBicycleisnewAbstract_Vehiclewithprivate;procedureParse (Stream :notnullaccessRoot_Stream_Type'Class; Item :outBicycle);forBicycle'ReaduseParse;privatetypeWheel_CountisnewStreamable_Types.Intrange1 .. 3;typeBicycleisnewAbstract_VehiclewithrecordWheels : Wheel_Count;endrecord;overridingfunctionConstructor (Name :notnullaccessParameter_Record)returnBicycle;endVehicles.Bicycles;
Vehicles.Bicycles 的主体
packagebodyVehicles.BicyclesisuseAda.Streams;procedureParse (Stream :notnullaccessRoot_Stream_Type'Class; Item :outBicycle)isbeginAbstract_Vehicle'Read (Stream, Abstract_Vehicle (Item)); Wheel_Count'Read (Stream, Item.Wheels);endParse;overridingfunctionConstructor (Name :notnullaccessParameter_Record)returnBicycleisResult : Bicycle;pragmaWarnings(Off, Result);beginreturnResult;endConstructor;beginRegister_Name ('b', Bicycle'Tag);endVehicles.Bicycles;
