comp.lang.ada
 help / color / mirror / Atom feed
From: Aitor Alcrudo Sangros <aitor.alcrudo@gmail.com>
Subject: Re: working with diferent instances of the same generic interface
Date: Wed, 9 Sep 2015 04:08:49 -0700 (PDT)
Date: 2015-09-09T04:08:49-07:00	[thread overview]
Message-ID: <adaff0f1-2522-42ad-b51b-78952da33e9c@googlegroups.com> (raw)
In-Reply-To: <87y4ggm71k.fsf@adaheads.sparre-andersen.dk>

El miércoles, 9 de septiembre de 2015, 8:38:18 (UTC+2), Jacob Sparre Andersen  escribió:
> Aitor Alcrudo Sangros wrote:
> 
> > 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.
> 
> Your examples don't compile.  Please post a compilable version.
> 
> Also.  It appears to me that you can omit using access types completely.
> 
> Greetings,
> 
> Jacob
> -- 
> There only exist 10 kinds of people: Those who know binary
> numbers and those who don't know binary numbers.

Hi, I'm not really specifically tring to make that code work as is. What I want to know is how can I work with different instances of the same generic interface, doing things like iterating over a collection of them. This is a minimal example of how I am (unsuccessfully ) tring to do so. Anyway this code compiles, then has a runtime exception.

with Ada.Containers.Vectors;
with Ada.Text_IO;   Use Ada.Text_IO;
procedure test is       
   
   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; 
  -- use Intro;
   
   
   --ELEMENT A
   type SomeData is new Intro.Data_type with record 
     a :float; 
   end record; 
	
   package Readable_Some is new Intro.Readable( 
      readable_data =>SomeData); 
   package Reporting_Some is new Intro.Reporting(Readable_Some); 
   type Element_type_A is new Readable_Some.Readable_Element 
                                     and Reporting_Some.Reporting_Element 
   with record 
      data :SomeData := SomeData'(a => 0.0 ); 
   end record; 
   type Element_type_A_Access is access all Element_type_A'Class;   
   overriding function Report(This : Element_Type_A ) return Intro.state is ( Intro.KO);
   function Get_Values (this : Element_type_A ) return SomeData'Class is
      (this.data);

   
   -- ELEMENT B   
   type OtherData is new Intro.Data_type with record 
     a :float; 
   end record; 

   package Readable_Other is new Intro.Readable( 
      readable_data =>OtherData); 
   package Reporting_Other is new Intro.Reporting(Readable_Other); 

   type Element_type_B is new Readable_Other.Readable_Element 
                                     and Reporting_Other.Reporting_Element 
   with record 
      data :OtherData; 
   end record;   
   type Element_type_B_Access is access all Element_type_B'Class; 
   function Get_Values (this : Element_type_B ) return OtherData'Class is
      (this.data);
   function Report(This : Element_type_B ) return Intro.state is ( Intro.KO);
   
   --MANAGER --
   package Readable_basic is new Intro.Readable(Intro.Data_type ); 
   package Reporting_basic is new Intro.Reporting(Readable_basic);
   use Reporting_basic;

    package Element_Vector is new
       Ada.Containers.Vectors (Natural,Reporting_basic.Reporting_Element_Access);
     use Element_Vector;
   
   type Manager is tagged record  
     elements :  Element_Vector.Vector;
   end record;
   procedure Add_Element(this : in out Manager ;
                         Element : in Reporting_Element_Access ) is
   begin
      this.elements.Append(Element);
   end Add_Element;
   
   Man : Manager;
   Elm1_ptr : Element_type_A_Access := new Element_type_A;
   Elm2_ptr : Element_type_B_Access := new Element_type_B;
begin  
   Add_Element(Man,Reporting_basic.Reporting_Element_Access(Elm1_ptr));
   Add_Element(Man,Reporting_basic.Reporting_Element_Access(Elm2_ptr));
   put_Line("hello world");
   
end test;

The problem might be in how I am instancing the generic for manager (readable_basic and reporting_basic), but I can't use a box association or unconstrained type in there.

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

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-08 15:10 working with diferent instances of the same generic interface Aitor Alcrudo Sangros
2015-09-08 17:11 ` G.B.
2015-09-09  6:38 ` Jacob Sparre Andersen
2015-09-09 11:08   ` Aitor Alcrudo Sangros [this message]
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