[ Table of Contents ]
This page provides the first of a series of example packages that implement push-down stacks that are progressively more useful. The final example of the series occurs in the next chapter, where exception handlers are added to make the package more robust or fault-tolerant.
The package Char_Stack, illustrated here, is an example of what is called an "abstract state machine" in [Booch87], an "object manager" in [Shumate89], or an abstract data object (ADO) in [Cohen96] and [Feldman96]. |
The package declaration declares the procedure Push and the function Pop, making both
available to clients. The package body contains an array data structure, called Space,
used to implement a push-down stack of characters. A test procedure called Test_Stack,
which begins to illustrate use of the package and then fails, is provided as well.
Note that when the client calls Push or Pop, it employs a dotted notation, such as:
CS.Push or
CS.Pop,
illustrating the general form "object.operation" where the package name CS
represents the object called. (Strictly speaking, an Ada package is not an object, which
is why this kind of package is referred to as an "object manager" in [Shumate89].)
Source Code Listing
-------------------------------------------------------- ----------------------- Char_Stack --------------------- -- This package creates a last-in/first-out (LIFO) -- "stack of characters" limited to a maximum -- size of eight characters. -------------------------------------------------------- package Char_Stack is procedure Push(C : in Character); function Pop return Character; end Char_Stack; -------------------------------------------------------- package body Char_Stack is type Space_Type is array (1..8) of Character; Space : Space_Type; Index : Natural := 0; -------------------------------------- procedure Push (C : in Character) is begin Index := Index + 1; Space(Index) := C; end Push; -------------------------------------- function Pop return Character is begin Index := Index - 1; return Space(Index + 1); end Pop; -------------------------------------- end Char_Stack; -------------------------------------------------------- -------------------------------------------------------- ----------------------- Test_Stack --------------------- -- This procedure pushes 4 characters onto the stack, -- then pops them, then pushes 8 characters, then asks -- the user to enter one more character. When it tries -- to push the 9th one, an exception is raised and the -- program crashes. -------------------------------------------------------- with Ada.Text_IO; with Char_Stack; procedure Test_Stack is package CS renames Char_Stack; package Tio renames Ada.Text_IO; Char : Character; begin CS.Push('A'); CS.Push('B'); CS.Push('C'); CS.Push('D'); Tio.Put_Line(CS.Pop & CS.Pop & CS.Pop & CS.Pop); -- displays DCBA CS.Push('A'); CS.Push('B'); CS.Push('C'); CS.Push('D'); CS.Push('E'); CS.Push('F'); CS.Push('G'); CS.Push('H'); Tio.Put_Line("Type a character"); Tio.Get(Char); CS.Push(Char); -- raises Constraint_Error -- exception, which will be -- unhandled and program will -- fail (Index reaches 9) end Test_Stack; -------------------------------------------------------- |
This package, though easy to read and understand, is a very limited, non-flexible, and non-reusable piece of code. The maximum size of the stack is fixed at eight elements, and the type of elements held there is fixed at Character. Furthermore, the package is not robust. The program will fail when an attempt is made to Pop an element from an empty stack, or to Push an element onto a full stack containing eight characters. (The latter is what happens when the Test_Stack procedure given above is run.)
Despite the limitations discussed above, there are many situations where a package is ideal for creating a one-of-a-kind abstract data object. (See, for example, the Scoreboard package in Section 2.12.)
Related Topics
2.7 Importance and Uses of Packages | 2.10 Abstract (ADT) Stack |
2 11 Generic Units | 2.12 Nested Packages |
[ Back to top of page ]