comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank" <franjoe@frisurf.no>
Subject: Access to T'Class vs T'Class as parameter to subprogram
Date: 3 Apr 2003 08:28:24 +0100
Date: 2003-04-03T08:28:24+01:00	[thread overview]
Message-ID: <3e8be295$1@news.wineasy.se> (raw)

Hi!
Regarding GNAT3.15p/Ada95/WindowsXP

In the code below I have done the following:

I have created a tagged type in Basis_Types package, that is abstract
(Abstract_Type). I this basis_types there is a subprogram Do_Something_2
that takes a stream and a pointer to a classwide of Abstract_Type.
I have added subprogram for 'Read attribute to this type.

In Extension_Type I have extended Basis_Type with Extension_Type. I have
added subprograms for 'Read for this new type.

In Initiator.adb I create a pointer to a Extension_Types.Abstract_Type.
Now I try to call "Do_Something_2" with this "pointer to class of extended
abstract_type" This does not compile, because
the compiler expects "pointer to class of basis abstract_type". I can easily
convert the pointer to the expected type, but then the wrong subprogram for
the 'Read attribute is called.

I have done the same approach with Abstract_Type in the two packages. When I
call "Do_Something" with Extension_Type.Abstract_Type from Initiator.adb
this compiles
and the correct subprogram for the 'Read attribute is executed. In
"Do_Something" the formal parameters in Basis_Types are a stream and a
Basis_Types.Abstract_Type'Class.

My issue is: Could I expect that the access-types to a classwide object
behaves as the classwide object themselves?

Does anyone have any comments in addition to what the compiler says? The
compiler says: no :-)

Frank



---------
with Basis_Types;
with Extension_Types;

with Ada.Float_Text_IO;
with Ada.Text_IO;
with Ada.Streams.Stream_IO;

procedure Initiator is

  Object : Extension_Types.Abstract_Type;
  Ptr_Object : Extension_Types.Pointer_To_Abstract_Type;

  Read_File: Ada.Streams.Stream_IO.File_Type;
  Input_Stream : Ada.Streams.Stream_IO.Stream_Access;
begin

  Ada.Streams.Stream_IO.Open(Read_File, Ada.Streams.Stream_IO.In_File,
"Initiator.adb");
  Input_Stream := Ada.Streams.Stream_IO.Stream(Read_File);



-- This works as I expect
  Basis_Types.Do_Something ( Input_Stream, Object);
  Ada.Text_IO.Put_Line("");



-- This I cant make work or I have misunderstood something:-)
  Ptr_Object := new Extension_Types.Abstract_Type;
  Basis_Types.Do_Something_2 ( Input_Stream, Ptr_Object);
-- This compiles but doesnt help me: Basis_Types.Do_Something_2 (
Input_Stream, Basis_Types.Pointer_To_Class_Abstract_Type(Ptr_Object));


  Ada.Streams.Stream_IO.Close(Read_File);
  Ada.Text_IO.Put_Line("");
end Initiator;



-----
with Ada.Streams; use Ada.Streams;

with Basis_Types;

package Extension_Types is


  type Abstract_Type is new Basis_Types.Abstract_Type with null record;
  type Pointer_To_Abstract_Type is access all Abstract_type;
  type Pointer_To_Class_Abstract_Type is access all Abstract_Type'Class;

  procedure Read_Abstract_Type (P_Stream : access Root_Stream_Type'Class;
P_Item : out Abstract_Type);
  for Abstract_Type'Read use Read_Abstract_Type;

  procedure Read_Pointer_To_Class_Abstract_Type (P_Stream : access
Root_Stream_Type'Class;
                                                 P_Item : out
Pointer_To_Class_Abstract_Type);
  for Pointer_To_Class_Abstract_Type'Read use
Read_Pointer_To_Class_Abstract_Type;

end Extension_Types;

-------
with Ada.Text_IO;
with Ada.Streams; use Ada.Streams;

package body Extension_Types is

  procedure Read_Abstract_Type (P_Stream : access Root_Stream_Type'Class;
P_Item : out Abstract_Type) is
  begin
    Ada.Text_IO.Put_Line("Extension_Types.Read_Abstract_Type");
  end Read_Abstract_Type;

  procedure Read_Pointer_To_Class_Abstract_Type (P_Stream : access
Root_Stream_Type'Class;
                                 P_Item : out
Pointer_To_Class_Abstract_Type) is
  begin

Ada.Text_IO.Put_Line("Extension_Types.Read_Pointer_To_Class_Abstract_Type");
  end Read_Pointer_To_Class_Abstract_Type;
end Extension_Types;



----
with Ada.Streams; use Ada.Streams;

package Basis_Types is

  type Abstract_Type is abstract tagged private;
  type Pointer_To_Abstract_Type is access all Abstract_type;
  type Pointer_To_Class_Abstract_Type is access all Abstract_Type'Class;

  procedure Read_Abstract_Type (P_Stream : access Root_Stream_Type'Class;
                                 P_Item : out Abstract_Type);
  for Abstract_Type'Read use Read_Abstract_Type;


  procedure Read_Pointer_To_Class_Abstract_Type (P_Stream : access
Root_Stream_Type'Class;
                                                 P_Item : out
Pointer_To_Class_Abstract_Type);
  for Pointer_To_Class_Abstract_Type'Read use
Read_Pointer_To_Class_Abstract_Type;


  procedure Do_Something ( P_Stream : access Root_Stream_Type'Class;
                             P_Data : out Abstract_Type'Class);
--
  procedure Do_Something_2 ( P_Stream : access Root_Stream_Type'Class;
                             P_Data : out Pointer_To_Class_Abstract_Type);

private

  type Abstract_Type is abstract tagged
    record
      A_Attribute : Integer;
    end record;


end Basis_Types;


-----
with Ada.Text_IO;
with Ada.Streams; use Ada.Streams;

package body Basis_Types is


  procedure Read_Abstract_Type (P_Stream : access Root_Stream_Type'Class;
                                 P_Item : out Abstract_Type) is
  begin
    Ada.Text_IO.Put_Line("Basis_Types.Read_Abstract_Type");
  end Read_Abstract_Type;

  procedure Read_Pointer_To_Class_Abstract_Type (P_Stream : access
Root_Stream_Type'Class;
                                 P_Item : out
Pointer_To_Class_Abstract_Type) is
  begin
    Ada.Text_IO.Put_Line("Basis_Types.Read_Pointer_To_Class_Abstract_Type");
  end Read_Pointer_To_Class_Abstract_Type;

  procedure Do_Something ( P_Stream : access Root_Stream_Type'Class;
                             P_Data : out Abstract_Type'Class) is
  begin
    Ada.Text_IO.Put_Line("Basis_Types.Do_Something");

    Abstract_Type'Class'Read(P_Stream, P_Data);

  end Do_Something;

  procedure Do_Something_2 ( P_Stream : access Root_Stream_Type'Class;
                             P_Data : out Pointer_To_Class_Abstract_Type) is
  begin
    Ada.Text_IO.Put_Line("Basis_Types.Do_Something_2");

    Pointer_To_Class_Abstract_Type'Read(P_Stream, P_Data);

  end Do_Something_2;

end Basis_Types;






                 reply	other threads:[~2003-04-03  7:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed
replies disabled

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