3 -- Grant of Unlimited Rights
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
7 -- unlimited rights in the software and documentation contained herein.
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
9 -- this public release, the Government intends to confer upon all
10 -- recipients unlimited rights equal to those held by the Government.
11 -- These rights include rights to use, duplicate, release or disclose the
12 -- released technical data and computer software in whole or in part, in
13 -- any manner and for any purpose whatsoever, and to have or permit others
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
27 -- Check that body of the generic parent package can depend on one of
28 -- its own public generic children.
31 -- A scenario is created that demonstrates the potential of adding a
32 -- public generic child during code maintenance without distubing a large
33 -- subsystem. After child is added to the subsystem, a maintainer
34 -- decides to take advantage of the new functionality and rewrites
37 -- Declare a bag abstraction in a generic package. Declare a public
38 -- generic child of this package which adds a generic procedure to the
39 -- original subsystem. In the parent body, instantiate the public
40 -- child. Then instantiate the procedure as a child instance of the
41 -- public child instance.
43 -- In the main program, declare an instance of parent. Check that the
44 -- operations in both parent and child packages perform as expected.
48 -- 06 Dec 94 SAIC ACVC 2.0
52 -- Simulates bag application.
55 type Element
is private;
56 with function Image
(E
: Element
) return String;
60 type Bag
is limited private;
62 procedure Add
(E
: in Element
; To_The_Bag
: in out Bag
);
64 function Bag_Image
(B
: Bag
) return string;
68 type Bag
is access Node_Type
;
72 The_Element
: Element
;
74 -- Other components in real application, i.e.,
75 -- The_Count : positive;
82 --==================================================================--
84 -- More operations on Bag.
88 -- Parameters go here.
90 package CA11020_0
.CA11020_1
is
92 -- ... Other declarations.
94 generic -- Generic iterator procedure.
95 with procedure Use_Element
(E
: in Element
);
97 procedure Iterate
(B
: in Bag
); -- Called once per element in the bag.
99 -- ... Various other operations.
101 end CA11020_0
.CA11020_1
;
103 --==================================================================--
105 package body CA11020_0
.CA11020_1
is
107 procedure Iterate
(B
: in Bag
) is
109 -- Traverse each element in the bag.
114 while Elem
/= null loop
115 Use_Element
(Elem
.The_Element
);
121 end CA11020_0
.CA11020_1
;
123 --==================================================================--
125 with CA11020_0
.CA11020_1
; -- Public generic child package.
127 package body CA11020_0
is
129 ----------------------------------------------------
130 -- Parent's body depends on public generic child. --
131 ----------------------------------------------------
133 -- Instantiate the public child.
135 package MS
is new CA11020_1
;
137 function Bag_Image
(B
: Bag
) return string is
139 Buffer
: String (1 .. 10_000
);
142 -----------------------------------------------------
144 -- Will be called by the iterator.
146 procedure Append_Image
(E
: in Element
) is
147 Im
: constant String := Image
(E
);
149 begin -- Append_Image
150 if Last
/= 0 then -- Insert a comma.
152 Buffer
(Last
) := ',';
155 Buffer
(Last
+ 1 .. Last
+ Im
'Length) := Im
;
156 Last
:= Last
+ Im
'Length;
160 -----------------------------------------------------
162 -- Instantiate procedure Iterate as a child of instance MS.
164 procedure Append_All
is new MS
.Iterate
(Use_Element
=> Append_Image
);
170 return Buffer
(1 .. Last
);
174 -----------------------------------------------------
176 procedure Add
(E
: in Element
; To_The_Bag
: in out Bag
) is
178 -- Not a real bag addition.
180 Index
: Bag
:= To_The_Bag
;
183 -- ... Error-checking code omitted for brevity.
186 To_The_Bag
:= new Node_Type
' (The_Element => E,
189 -- Goto the end of the list.
191 while Index.Next /= null loop
195 -- Add element to the end of the list.
197 Index.Next := new Node_Type' (The_Element
=> E
,
205 --==================================================================--
207 with CA11020_0
; -- Bag application.
213 -- Instantiate the bag application for integer type and attribute
216 package Bag_Of_Integers
is new CA11020_0
(Integer, Integer'Image);
218 My_Bag
: Bag_Of_Integers
.Bag
;
222 Report
.Test
("CA11020", "Check that body of the generic parent package " &
223 "can depend on one of its own public generic children");
225 -- Add 10 consecutive integers to the bag.
227 for I
in 1 .. 10 loop
228 Bag_Of_Integers
.Add
(I
, My_Bag
);
231 if Bag_Of_Integers
.Bag_Image
(My_Bag
)
232 /= " 1, 2, 3, 4, 5, 6, 7, 8, 9, 10" then
233 Report
.Failed
("Incorrect results");