comp.lang.ada
 help / color / mirror / Atom feed
From: "jason.height@gmail.com" <jason.height@gmail.com>
Subject: Loosing tagged type via linked list
Date: Fri, 8 Feb 2008 17:05:58 -0800 (PST)
Date: 2008-02-08T17:05:58-08:00	[thread overview]
Message-ID: <aaa2346c-2135-48cb-9cb3-3e19dfb0e9c4@i29g2000prf.googlegroups.com> (raw)

Hi All,

I have an interesting problem where i need to store a bunch of
procedure calls (for graphics displays) and the associated parameters
for later calling (much like a macro call).

I am attempting this in Ada95, with my own Linked_List container impl,
which seems to be destroying the type information. If anyone knows a
Ada95 list implementation that can store items of different types
(derived from a base type) then please let me know.

Ideally i would like the following method to always print B_Type:
--------------------------------------------------------------------------------------------
with Text_Io;
with Ada.Tags; use Ada.Tags;
with Linked_List;

procedure Test is
  type A_Type is tagged record
    null;
  end record;
  type B_Type is new A_Type with record
    Int : Integer;
  end record;

  A : A_Type;
  B : B_Type := (Int => 1);

  package MyList is new Linked_List(A_Type);
  The_List : MyList.List_Type;
  Itr : MyList.List_Iterator;

  procedure DoPrint(Input : A_Type'Class) is
  begin
    Text_Io.Put_Line("Tag is "&Expanded_Name(Input'Tag));
  end;
  procedure DoPrintTypeCast(Input : A_Type) is
  begin
    DoPrint(Input);
  end;
begin

  --These all print TEST.B_Type
  DoPrint(B);
  DoPrint(A_Type(B));
  DoPrintTypeCast(A_Type(B));

  --This prints TEST.A_Type!
  --BAD List Impl?
  MyList.Add(The_List, A_Type(B));
  Itr := MyList.First(The_List);
  DoPrint(MyList.Value(Itr));
end;
------------------------------------------------------------------------------------

Here is the list spec. I was hoping that someone could spot my trouble
right off. I must admit i am more at home with Java and C#, where the
containers are built into the platform, so this one has had me stumped
for a few days.

------------------------------------------------------------------------------------------------------------------------------
with Ada.Finalization; use Ada.Finalization;
generic
   type Item_Type is tagged private;
package Linked_List is
   type List_Type is new Limited_Controlled with private;
   type List_Iterator is private;

   function First (List : List_Type) return List_Iterator;

   function Value (Iterator : List_Iterator) return Item_Type'Class;

   procedure Add (List : in out List_Type; Item : in Item_Type);

private
   type Item_Record;
   type Item_Access is access Item_Record;

   type Item_Record is record
      Item : Item_Type;
      Next : Item_Access := null;
      Pred : Item_Access := null;
   end record;
   type List_Header is record
      First : Item_Access := null;
      Last  : Item_Access := null;
      Count : Natural := 0;
   end record;
   type List_Access is access List_Header;

   type List_Type is new Limited_Controlled with record
      List : List_Access := new List_Header;
   end record;

   procedure Finalize (Object : in out List_Type);

   type List_Iterator is record
      List    : List_Access := null;
      Current : Item_Access := null;
   end record;
end Linked_List;
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks in advance
Jason



             reply	other threads:[~2008-02-09  1:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-09  1:05 jason.height [this message]
2008-02-09  3:48 ` Loosing tagged type via linked list Georg Bauhaus
2008-02-09 11:40   ` Simon Wright
2008-02-10 17:11     ` Simon Wright
2008-02-09  9:38 ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox