Ada Programming/Libraries/Ada.Streams/Example
This page gives a (fairly complex) example of usage of class-wide stream related attributes Class'Read, Class'Write, Class'Input, and Class'Output.
The problem
The problem we will consider is the following: suppose that two hosts communicate over a TCP connection, exchanging information about vehicles. Each vehicle is characterized by its type (a car, a truck, a bicycle, and so on), its maximum speed (in km/h, represented by an integer number) and a set of further parameters that depend on the vehicle type. For example, a car could have a parameter "number of passengers," while a truck could have a parameter "maximum load" (an integer number of kg). For the sake of simplicity we will suppose that every parameter is represented by an integer number.
The protocol used to communicate vehicle data over the wire is text-based and it is as follows
- The first octet is a character that denotes the vehicle type. For example 'c' is for "car," 't' is for "truck," 'b' is for "bicycle."
- Next it comes the vehicle speed, represented as an integer number encoded as "<len> i <value>" where
- <value> is the speed value, expressed as a number in base 10 with <len> digits
- <len> is the length of the <value> field, expressed as a number in base 10. This field can have trailing spaces
For example, the integer 256 would be encoded as "3i256".
- The speed value is followed by the list of vehicle-specific parameter, encoded with the same format of the speed field.
We would like to use the features of Ada streams to read and write vehicle information from and to any "medium" (e.g., a network link, a file, a buffer in memory) and we would like to use the object-oriented features of Ada in order to simplify the introduction of a new type of vehicle.
The solution
This is a sketch of the proposed solution
- We will create a hierarchy of objects to represent the vehicle types. More precisely, we will represent each vehicle as a descendant of an abstract type (Abstract_Vehicle)
- Reading from a stream will be done via function Abstract_Vehicle'Class'Input that will work as follows
- Writing to a stream will be done via procedure Abstract_Vehicle'Class'Output that will work as follows
- We will derive a new type Int from Integer and we will define for it new procedures Int'Read and Int'Write that will read and write variables of type Int encoded in the format "<len> i <value>" described above
- In order to allow for the introduction of new vehicle types (maybe by dynamically loading a library at runtime), at the step 2 of the Abstract_Vehicle'Class'Input function described we cannot use a
caseon the character read in order to determine the type of the object to be created. We will instead use the generic dispatching constructor provided by Ada (see 3.9 Tagged Types and Type Extensions (Annotated)). - Since the generic dispatching constructor requires the tag of the object to be created, we must be able to determine the tag that corresponds to a given character. We will achieve this by keeping an array of Ada.Tags.Tag indexed by character. A package defining a new vehicle will "register" itself in the initialization part of the package (that is, the sequence of statements that follows the
beginin the package body, see 7.2 Package Bodies (Annotated)) by writing the tag of the defined vehicle in the suitable position of that array.
Implementation
Streamable types
The first package that we are going to analyze is a package that defines a new integer type in order to assign to it attributes Read and Write that serialize integer values according to the format described above. The package specs are quite simple
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;
The new type is Int and the procedure assigned to attributes Read and Write are, respectively, Parse and Read. Also the body is quite simple
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;
The body of Streamable_Types should not require any special comment. Note how the access to the stream is done by dispatching through the primitive procedures Read and Write, allowing the package above to work with any type of stream.
Abstract Vehicles
The second package we are going to analyze is Vehicles that define an abstract tagged type Abstract_Vehicle that represents the "least common denominator" of all the possible vehicles.
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;
This package defines
- Function Input_Vehicle and procedure Output_Vehicle to be used, respectively, as class-wide input and output procedures
- Abstract constructor "Constructor" that every non-abstract type derived by Vehicle must override. This constructor will be called by Generic_Dispatching_Constructor in the body.
- Procedure Register_Name that associates a vehicle "name" (represented by a character in this simplified case) to the corresponding type (represented by its Tag). In the typical case this procedure will be called by the package that derives from Abstract_Vehicle in the body initialization part
The body of the package is
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;
Note the behavior of Input_Vehicle, the function that will play the role of class-wide input.
- First it reads the character associated to the next vehicle in the stream by using the stream-related function Character'Read.
- Successively it uses the character read to find the tags of the object to be created
- It creates the object by calling the specialized version of Generic_Dispatching_Constructor
- It "fills" the newly created object by calling the class-wide Read that will take care of calling the Read associated to the newly created object
Procedure Output_Vehicle is much simpler than Input_Vehicle since it does not need to use the Generic_Dispatching_Constructor. Just note the call to Abstract_Vehicle'Class'Write that in turn will call the Write function associated to the actual type of Item.
Finally, note that Abstract_Vehicle does not define the Read and Write attributes. Therefore, Ada will use their default implementation. For example, Abstract_Vehicle'Read will read the two Streamable_Types.Int value Speed and Weight by calling twice the procedure Streamable_Types.Int'Read. A similar remark apply to Abstract_Vehicle'Write.
Non-Abstract Vehicles
Car
The first non-abstract type derived from Abstract_Vehicle that we consider represents a car. In order to make the example a bit more rich, Car will be derived from an intermediate abstract type representing an engine-based vehicle. All engine-based vehicles will have a field representing the power of the engine (still an integer value, for the sake of simplicity). The spec file is as follows
packageVehicles.Engine_BasedistypeAbstract_Engine_BasedisabstractnewAbstract_Vehiclewithprivate;privatetypeAbstract_Engine_BasedisabstractnewAbstract_VehiclewithrecordPower : Streamable_Types.Int;endrecord;endVehicles.Engine_Based;
Note that also in this case we did not define any Read or Write procedure. Therefore, for example, Abstract_Engine_Based'Read will first call Streamable_Types.Int twice to read Speed and Weight (inherited from Abstract_Vehicle) from the stream, then it will call Streamable_Types.Int another time to read Power.
Note also that Abstract_Engine_Based does not override the abstract function Constructor of Abstract_Vehicle. This is not necessary since Abstract_Engine_Based is abstract.
The spec file of the package that defines the Car type is as follows
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;
No special remarks are needed about the spec file. Just note that Car defines a special Read procedure and that it overrides Construct, as required since Car is not abstract.
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;
The body of Vehicles.Engine_Based.Auto is quite simple too, just note that
- Procedure Parse (used as Car'Read) first calls Abstract_Engine_Based'Read to "fill" the part inherited from Abstract_Engine_Based, then it calls Streamable_Types.Int'Read to read the number of cylinders. Incidentally, note that this is equivalent to the default behavior, so it was not really necessary to define Parse. We did it just to make an example.
- Note the call to Register_Name in the body initialization part that associates the name 'c' with the tag of type Car (obtained via the attribute 'Tag). An interesting property of this solution is that the information about the "external name" 'c' of objects of type Car is knew only inside the package Vehicles.Engine_Based.Auto.
Bicycle
The spec file of 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;
The body of 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;
See also
Wikibook
- Ada Programming
- Ada Programming/Object Orientation
- Ada Programming/Input Output
- Ada Programming/Libraries/Ada.Streams