In this chapter we studied two important concepts in building reusable software components. Unconstrained array types allow us to define array types such that the bounds of a given array are left unspecified until the array variable is declared. Unconstrained array types facilitate writing general-purpose subprograms that deal with arrays, such as vector operations and sort procedures.
Generic definition allows us to create templates, or recipes, for subprograms and packages. These templates allow us to leave such things as parameter types, sizes, and operations unspecified until instantiation time. Once a generic template is compiled, multiple versions of it, called instances, can then be created, each with a single statement. The availability of generic definition and instantiation gives us the potential for building large and powerful libraries of reusable software components with much less effort and with much greater maintainability. In this chapter we saw a number of useful generic components for exchanging values, finding the maximum, sorting, and vector handling.
The new Ada constructs introduced in this chapter are described in Table 11.1.
Table 11.1 Summary of New Ada Constructs
Unconstrained Array Types
SUBTYPE Weeks IS Positive RANGE 1..52; Declares an array SUBTYPE Rainfall IS Float RANGE 0.0..500.0; type whose variables TYPE RainTable IS can be indexed by ARRAY (Weeks RANGE <>) OF Rainfall; any subrange of Weeks SecondQuarter: RainTable(14..26); and a variable with 13 elements
Generic Specification
GENERIC TYPE ValueType IS PRIVATE; Specifies a function TYPE IndexType IS (<>); to find the location TYPE ArrayType IS of the "largest"value ARRAY(IndexType RANGE <>) OF ValueType; in an array. WITH FUNCTION Compare(L,R: ValueType) RETURN Boolean; FUNCTION IndexOfMax(A: ArrayType) RETURN IndexType;
WITH FUNCTION Compare(L,R: ValueType) RETURN Boolean;
explain why it is legal to match this with an operator "<"
or ">"
at instantiation.
TYPE SomeParameterName IS LIMITED PRIVATE;
which allows any type, even a LIMITED PRIVATE
one, to
be supplied as a match at instantiation. Suppose we used one of these type
forms in a generic package specification. What limitations would this place on
the kinds of statements that could appear in the body of the package?
Search
(Program 8.14)
that looks through an array for a particular value. Revise Search
to make it generic, and write a test program for several instances.
TYPE SomeParameterName IS LIMITED PRIVATE;
which allows any type, even a LIMITED PRIVATE
one, to
be supplied as a match at instantiation. Suppose we used one of these type
forms in a generic package specification. What limitations would this place on
the kinds of statements that could appear in the body of the package?
Maximum_Array_Generic
for some interesting
instantiations.
Maximum_Array_Generic
is one
that finds the location of the "maximum" value in an array or slice,
rather than the value itself. Write such a function as a generic, then write a
generic sort program that uses it.
GenericArrayMaximum
is one that
finds the location of the "maximum" value in an array or slice, rather
than the value itself. Write such a function as a generic, then write a generic
sort program that uses it.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.