comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: How to get generic formal parameter type into base class
Date: Fri, 12 Oct 2012 01:52:38 +0200
Date: 2012-10-12T01:52:38+02:00	[thread overview]
Message-ID: <50775bc6$0$6560$9b4e6d93@newsspool4.arcor-online.net> (raw)
In-Reply-To: <b57d53cc-32c9-4c88-a53a-99494f5409fc@m4g2000yqf.googlegroups.com>

On 11.10.12 22:25, kevin andrew wrote:

> FYI, you can do this with C++ template classes, and though I have
> never tried, Java has added templates as well.

Like the C++ version, this establishes the required
relationship between Int and Char; C++'s protected
visibility is achieved with the help of a child package.

package Typedef is
    
    subtype Value_T is Integer;
    
end Typedef;

with Typedef; use Typedef;

generic
    type T is new Value_T;
package BaseClass is
    
    type BaseClass is tagged private;
    procedure Decode (Item : in out BaseClass; Value: Value_T) is null;
    function GetValue (Item : BaseClass) return T;
private
    type BaseClass is tagged record
       Data : T;
       Value : Value_T;
    end record;
end BaseClass;

package body Baseclass is
    Default : T;
    function GetValue (Item : BaseClass) return T is
    begin
       return Default;
    end GetValue;
    
end Baseclass;

with Typedef; use Typedef;
generic
    type T is new Value_T;
package BaseClass.NewClass is
    type NewClass is new BaseClass with private;
    overriding procedure Decode (Item : in out NewClass; Value: Value_T);
    overriding function GetValue (Item : NewClass) return Standard.BaseClass.T;
private
    type NewClass is new BaseClass with null record;
end BaseClass.NewClass;

package body BaseClass.NewClass is
    overriding procedure Decode (Item : in out NewClass; Value: Value_T) is
    begin
       Item.Value := Value;
       Item.Data := STandard.BaseClass.T(Value);
    end Decode;
    
    overriding function GetValue (Item : NewClass) return Standard.BaseClass.T is
    begin
       return Item.Data;
    end Getvalue;
end BaseClass.NewClass;

    
with Typedef; use Typedef;
with BaseClass.NewClass;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure main is
    
    subtype Int is Integer range Integer'Range;
    subtype Char is Integer range -128 .. 127;
    
    package BaseClassInstance is new BaseClass(Int);
    package NewClassInt is new BaseClassInstance.NewClass(Int);
    IntClass: NewClassInt.NewClass;
    
    package BaseClassInstance2 is new BaseClass(Char);
    package NewClassChar is new BaseClassInstance2.NewClass(Char);
    CharClass: NewClassChar.NewClass;
    
    Value : Value_T := 4;
begin
    IntClass.Decode(Value);
    declare
       I : Integer := IntClass.GetValue;
    begin
       Value := 97;
       CharClass.Decode(Value);
       declare
          Ch: Char := CharClass.GetValue;
          use Ada.Text_IO, Ada.Integer_Text_IO;
       begin
          Default_Width := 0;
          Put ("int="); Put(I); Put("  ch="); Put (Character'Val(Ch)); New_Line;
       end;
    end;
end Main;





  parent reply	other threads:[~2012-10-16  1:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-05 22:23 How to get generic formal parameter type into base class kevin.miscellaneous
2012-10-05 23:38 ` Georg Bauhaus
2012-10-06  3:28 ` Adam Beneschan
2012-10-06 20:03   ` kevin andrew
2012-10-07  3:06     ` Adam Beneschan
2012-10-11 20:25       ` kevin andrew
2012-10-11 23:28         ` Georg Bauhaus
2012-10-11 23:52         ` Georg Bauhaus [this message]
2012-10-07  7:42     ` 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