comp.lang.ada
 help / color / mirror / Atom feed
From: Aitor Alcrudo Sangros <aitor.alcrudo@gmail.com>
Subject: working with diferent instances of the same generic interface
Date: Tue, 8 Sep 2015 08:10:38 -0700 (PDT)
Date: 2015-09-08T08:10:38-07:00	[thread overview]
Message-ID: <625c79d2-f5ed-4cd4-80ea-ea310e381017@googlegroups.com> (raw)

Hi, my google-fu can't find how to do this. I think it should be doable but I'm not 100% sure. I'm thankful if anyone helps.

I have a generic interface some of my tagged records instance, similar to:

package Intro is
   type Data_type is interface;

   generic
      type Readable_Data is abstract new Data_type with private;
   package Readable is
      type Readable_Element is interface;
      type Readable_Element_Access is access all Readable_Element'Class ;
      function Get_Values (this : Readable_Element ) return Readable_Data'Class
                         is abstract;
   end Readable;

   type state is (OK,KO);

     generic
      with package Readable_Reporting is new Readable(<>);
      use Readable_Reporting ;
   package Reporting is
      type Reporting_Element is interface and Readable_Element ;
      type Reporting_Element_Access is access all Reporting_Element'Class;
      function Report (This :Reporting_Element  ) return state is Abstract;
      overriding function Get_Values (this : Reporting_Element ) return
          Readable_Data'Class is abstract;
   end Reporting;
end Intro;

This has worked with no problem. 
I instance it like this
   SomeData is new Data_type with record
     a :float;
   end record;

   package Readable is new Intro.Readable(
      readable_data =>SomeData);
   package Reporting is new Intro.Reporting(Readable);

   type Element_type_A is new Readable.Readable_Element
     				and Reporting.Reporting_Element 
   						with record
      data :float;
   end record;
   type Element_type_A_Access is access all Element_type_A 'Class;

Now, I have something like this

package Reporter is
   package Readable_basic is new Intro.Readable(Data_type );
   package Reporting_basic is new Intro.Reporting(Readable);
   use Reporting;
   type manager is tagged private;
   procedure Add_Element( This : in out manager ;
                         Element : in Reporting_Element_Access );
private
   package Element_Vector is new
     Ada.Containers.Vectors (Natural,Reporting_Element_Access );
   use Element_Vector ;

   type manager  is tagged record
      Element_List : Element_Vector.Vector;
   end record;
end Reporter ;

this compiles.

Now I try to use it elsewhere

procedure try is
   man :manager ;
   elm1 : Element_type_A_Access ;
   elm2 : Element_type_B_Access ;
begin
   man.add(Reporter.Reporting_basic.Reporting_Element_Access(elm1)  );
   man.add(Reporter.Reporting_basic.Reporting_Element_Access(elm2)  );
end;

This compiles, but produces a runtime exception with invalid interface conversion.

Is there any way to get this to work?.




































             reply	other threads:[~2015-09-08 15:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-08 15:10 Aitor Alcrudo Sangros [this message]
2015-09-08 17:11 ` working with diferent instances of the same generic interface G.B.
2015-09-09  6:38 ` Jacob Sparre Andersen
2015-09-09 11:08   ` Aitor Alcrudo Sangros
2015-09-09 12:17     ` Egil H H
2015-09-09 12:41       ` Aitor Alcrudo Sangros
2015-09-09 13:10         ` Egil H H
2015-09-09 14:50           ` Aitor Alcrudo Sangros
2015-09-09 16:48             ` Egil H H
2015-09-09 19:04               ` Aitor Alcrudo Sangros
2015-09-09 19:16                 ` Dmitry A. Kazakov
2015-10-01 16:45                   ` Aitor Alcrudo Sangros
2015-10-01 19:29                     ` Dmitry A. Kazakov
2015-10-01 20:09                       ` Randy Brukardt
2015-09-13 18:22             ` Florian Weimer
2015-09-10 12:22     ` Jacob Sparre Andersen
replies disabled

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