DocAda(tm) is a productivity tool of KSCE

Prev | Up | ____ | Back | Forward
About DocAda Light: Preface / Preliminary / Help / TOC / Copyright
DocAda Online at the Ada Home: Complete RM95 / Updates / News

12.8 Example of a Generic Package

The following example provides a possible formulation of stacks by means of a generic package. The size of each stack and the type of the stack elements are provided as generic formal parameters.

Examples

(blank)

   
       generic
          Size : Positive;
          type Item is private;
       package Stack is
          procedure Push(E : in  Item);
          procedure Pop (E : out Item);
          Overflow, Underflow : exception;
       end Stack;

       package body Stack is

          type Table is array (Positive range <>) of Item;
          Space : Table(1 .. Size);
          Index : Natural := 0;

          procedure Push(E : in Item) is
          begin
             if Index >= Size then
                raise Overflow;
             end if;
             Index := Index + 1;
             Space(Index) := E;
          end Push;

          procedure Pop(E : out Item) is
          begin
             if Index = 0 then
                raise Underflow;
             end if;
             E := Space(Index);
             Index := Index - 1;
          end Pop;

       end Stack;

Instances of this generic package can be obtained as follows:

    
       package Stack_Int  is new Stack(Size => 200, Item => Integer);
       package Stack_Bool is new Stack(100, Boolean);

Thereafter, the procedures of the instantiated packages can be called as follows:

    
       Stack_Int.Push(N);
       Stack_Bool.Push(True);

Alternatively, a generic formulation of the type Stack can be given as follows (package body omitted):

    
       generic
          type Item is private;
       package On_Stacks is
          type Stack(Size : Positive) is limited private;
          procedure Push(S : in out Stack; E : in  Item);
          procedure Pop (S : in out Stack; E : out Item);
          Overflow, Underflow : exception;
       private
          type Table is array (Positive range <>) of Item;
          type Stack(Size : Positive) is
             record
                Space : Table(1 .. Size);
                Index : Natural := 0;
             end record;
       end On_Stacks;

In order to use such a package, an instance has to be created and thereafter stacks of the corresponding type can be declared:

    
       declare
          package Stack_Real is new On_Stacks(Real); use Stack_Real;
          S : Stack(100);
       begin
          ...
          Push(S, 2.54);
          ...
       end;

About DocAda Light: Preface / Preliminary / Help / TOC / Copyright
DocAda Online at the Ada Home: Complete RM95 / Updates / News
Prev | Up | ____ | Back | Forward


Copyright © 1994-1997 by Kempe Software Capital Enterprises. All Rights Reserved. For comments on this DocAda(tm) hypertext document, please write to KSCE, at docada@ksce.com