Previous | Table of Contents | Next |
10.4.3.7. Implementation of the Instruments Hierarchy
I return now to the implementations of the various instrument packages.
1 with Ada.Text_IO; use Ada.Text_IO; 2 package body HB.Instruments is 3 4 procedure Set_Name(I: in out Instrument; S: String) is 5 begin 6 I.Name (1..SLength) := S; 7 end Set_Name; 8 9 procedure Display_Value(I: Instrument) is 10 begin 11 New_Line; 12 Put(I.Name); 13 Put(: ); 14 end Display_Value; 15 16 end HB.Instruments;
The body of HB.Instruments is straightforward and contains nothing new. Note that Display_Value just displays the string name of the instrument. Next, consider the body of HB.Instruments.Basic.
1 with Ada.Text_IO; use Ada.Text_IO; 2 with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 3 package body HB.Instruments.Basic is 4 5 procedure Display_Value(S: Speedometer) is 6 begin 7 Display_Value(Instrument (S)); 8 Put(S.Value, 1); 9 Put( Miles per Hour); 10 end Display_Value; 11 12 procedure Set_Value 13 (S: in out Speedometer; V: Speeds) is 14 begin 15 S.Value := V; 16 end Set_Value; 17 18 procedure Display_Value(G: Gauge) is 19 begin 20 Display_Value(Instrument (G)); 21 Put(G.Value, 1); 22 Put( %); 23 end Display_Value; 24 25 procedure Display_Value(G: Graphic_Gauge) is 26 Lg: constant Integer := G.Size * G.Value / 100; 27 S1: constant String(1 .. Lg) := (others => G.Fill); 28 S2: constant String(Lg + 1 .. G.Size) 29 := (others => G.Empty); 30 begin 31 Display_Value(Instrument (G)); 32 Put(<); 33 Put(S1); 34 Put(S2); 35 Put(>); 36 end Display_Value; 37 38 end HB.Instruments.Basic;
The code here is entirely familiar except for one new detail. In line 7,
Display_Value(Instrument (S));
calls the Display_Value defined for Instrument. To make this happen, its actual parameter is converted to an Instrument value. What happens to the extension component that turned Instrument into Speedometer? It is effectively stripped off in this up-conversion process. In programming languages supporting OOP, up-conversions are common and down-conversions are disallowed. In the package, lines 20 and 31 do similar up-conversions to Instrument.
Given the preceding discussion, HB.Instruments.Clocks contains much detail but no new concepts at all; it is included just for completeness.
1 with Ada.Text_IO; use Ada.Text_IO; 2 with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 3 package body HB.Instruments.Clocks is 4 5 procedure Display_Value(C: Clock) is 6 begin 7 Display_Value(Instrument (C)); 8 if C.Hours < 10 then 9 Put(0); 10 end if; 11 Put(C.Hours,1); 12 Put(:); 13 if C.Minutes < 10 then 14 Put(0); 15 end if; 16 Put(C.Minutes,1); 17 Put(:); 18 if C.Seconds < 10 then 19 Put(0); 20 end if; 21 Put(C.Seconds,1); 22 end Display_Value; 23 24 procedure Increment 25 (C: in out Clock; Inc: Integer:=1) is 26 nInc: Integer; 27 begin 28 C.Seconds :=(C.Seconds + Inc) mod 60; 29 nInc :=(C.Seconds + Inc) / 60; 30 C.Minutes :=(C.Minutes + nInc) mod 60; 31 nInc :=(C.Minutes + nInc) / 60; 32 C.Hours :=(C.Hours + nInc) mod 24; 33 end Increment; 34 35 procedure Init(C: in out Clock; 36 H: Twenty_Four := 0; 37 M, S: Sixty := 0) is 38 begin 39 C.Seconds := S; 40 C.Minutes := M; 41 C.Hours := H; 42 end Init; 43 44 procedure Display_Value(C: Chronometer) is 45 V: Integer; 46 begin 47 Display_Value(Instrument (C)); 48 V := C.Seconds + C.Minutes * 60 + C.Hours * 3600; 49 Put(<<); 50 Put(V, 1); 51 Put(>>); 52 end Display_Value; 53 54 end HB.Instruments.Clocks;
Previous | Table of Contents | Next |