[ Table of Contents ]
Example Program with Dynamic Allocation of Tagged Objects
This program withs in the same tagged type hierarchy as the previous program and the generic linked list package illustrated in an earlier chapter. A generic instance called Vehicle_Lists_Pkg is declared. During execution a set of vehicles are dynamically allocated and the access values corresponding to a sub-set of them are placed on the list. A loop is then used to traverse the list, commanding those vehicles to report positions. Finally, three of the vehicles are commanded to deploy weapons. |
The Report_Position and Deploy_Weapon operations in the code listed below are dynamically dispatched because the controlling actual parameters (VP(i).all) are objects of the classwide type, Vehicle'Class.
Source Code Listing
---------------------------------------------------------- -- This procedure tests the derivation class rooted at Vehicle -- by dynamically allocating 12 vehicles, putting some of the -- corresponding access values on a linked list, traversing the -- list to report on positions, and then deploying a few -- weapons. ---------------------------------------------------------- with Vehicles, Vehicles.Airplanes, Vehicles.Ships; use Vehicles, Vehicles.Airplanes, Vehicles.Ships; with Vehicles.Airplanes.Fighters, Vehicles.Airplanes.Bombers; use Vehicles.Airplanes.Fighters, Vehicles.Airplanes.Bombers; with Vehicles.Ships.Cruisers, Vehicles.Ships.Submarines; use Vehicles.Ships.Cruisers, Vehicles.Ships.Submarines; with Doubly_Linked_Lists_Generic; procedure Test_Dynamic_Vehicles is type Vptr_Type is access all Vehicle'Class; VP : array (1..100) of Vptr_Type; -- ample supply of pointers VPX : Vptr_Type; package Vehicle_Lists_Pkg is -- generic instantiation new Doubly_Linked_Lists_Generic(Vptr_Type); use Vehicle_Lists_Pkg; Vehicle_List : List; -- create list Current : Position; -- create pointer begin -- dynamic allocation of 12 Vehicles for I in 1..3 loop VP(I) := new Fighter; end loop; for I in 4..6 loop VP(I) := new Bomber; end loop; for I in 7..9 loop VP(I) := new Cruiser; end loop; for I in 10..12 loop VP(I) := new Submarine; end loop; -- initialization of 6 Vehicles -- (off Cape Canaveral, heading East) Rename_Reset(VP(1).all, "F1", 30, -75, 90); Rename_Reset(VP(2).all, "F2", 32, -75, 90); Rename_Reset(VP(4).all, "B1", 31, -75, 90); Rename_Reset(VP(7).all, "C1", 30, -77, 90); Rename_Reset(VP(8).all, "C2", 32, -75, 90); Rename_Reset(VP(10).all, "S1", 31, -75, 90); -- place pointers on list AddToRear(Vehicle_List, VP(1)); AddToRear(Vehicle_List, VP(2)); AddToRear(Vehicle_List, VP(4)); AddToRear(Vehicle_List, VP(7)); AddToRear(Vehicle_List, VP(8)); AddToRear(Vehicle_List, VP(10)); Current := First(Vehicle_List); -- traverse list and loop -- report positions VPX := Retrieve(Vehicle_List, Current); Report_Position(VPX.all); exit when Is_Last(Vehicle_List, Current); GoAhead(Vehicle_List, Current); end loop; Deploy_Weapon(VP(4).all); Deploy_Weapon(VP(7).all); Deploy_Weapon(VP(10).all); end Test_Dynamic_Vehicles; ---------------------------------------------------------- |
The above program produces the following output:
F1 Latitude = 30 Longitude = -75 F2 Latitude = 32 Longitude = -75 B1 Latitude = 31 Longitude = -75 C1 Latitude = 30 Longitude = -77 C2 Latitude = 32 Longitude = -75 S1 Latitude = 31 Longitude = -75 Bomber B1 releases bomb over target Cruiser C1 launches a cruise missile Submarine S1 fires a torpedo |
Related Topics
2.11 Generic Units | 4.10 Access Types - 1 |
5.3 Classwide Types and Dispatching | 5.4 Implementing the Operations |
[ Back to top of page ]