GNAT.Spitbol.Table Example


-- GNAT.Spitbol.Table Example
--
-- Example of how to create a String to Object map

with GNAT.Spitbol;

package GSTable is
   subtype Inches is Positive  range 1 .. 144;

   type Object is
      record
         ID     : Natural;
         Height : Inches := 1;
      end record;

   Null_Object : constant Object := (ID => 0, Height => 1);

   function To_String (This : in Object) return String;

   function "=" (A, B : in Object) return Boolean;

   package String_To_Obj_Map is new GNAT.Spitbol.Table(Value_Type => Object,
                                                        Null_Value => Null_Object,
end GSTable;

package body GSTable is

   function To_String (This : in Object) return String is
   begin
      return "ID =" & Natural'Image(This.ID) &
        " Height =" & Inches'Image(This.Height);
   end To_String;

   function "=" (A, B : in Object) return Boolean is
   begin
      return A.ID = B.ID;
   end "=";

end GSTable;

with GSTable;
with GNAT.Spitbol;

procedure String_Map is
   use GSTable.String_To_Obj_Map;

   Test_Map : Table(10);
begin
   Set(Test_Map, "Bob",
       (ID => 401, Height=> 65));
   Set(Test_Map, "Sue",
       (ID => 123, Height=> 62));
   Set(Test_Map, "Marvin",
       (ID => 321, Height=> 76));
   Set(Test_Map, "George",
       (ID => 555, Height=> 68));

   declare
      use GNAT.Spitbol;

      Test_Array : Table_Array := Convert_To_Array(Test_Map);
   begin
      for N in Test_Array'First .. Test_Array'Last loop
         Put_Line(S(Test_Array(N).Name));

         -- Note the use of the GNAT.Spitbol."&" functions
         -- to convert integer types to strings
         Put_Line("ID     = " & Test_Array(N).Value.ID);
         Put_Line("Height = " & Test_Array(N).Value.Height);
      end loop;
   end;

end String_Map;


Contributed by: David Botton
Contributed on: May 19, 1999
License: Public Domain

Back