comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Idiom for a class and an object in Ada
Date: Thu, 21 Oct 2004 18:02:32 -0400
Date: 2004-10-21T22:02:32+00:00	[thread overview]
Message-ID: <417831f8$0$36214$39cecf19@news.twtelecom.net> (raw)
In-Reply-To: e749549b.0410211131.6505da@posting.google.com


"Kevin Cline" <kevin.cline@gmail.com> wrote in message 
news:e749549b.0410211131.6505da@posting.google.com...
>
>  class AD_Converter
>  {
>  private:
>
>    AD_Converter(int line) {}
>  public:
>    static AD_Converter Noise;
>  };
>
>  AD_Converter AD_Converter::Noise(17);

Yes, indeed.  I should have made this more clear.

Actually, directly translating the C++ code above to Ada is a little tricky, 
since you can't declare object Noise until the full view of the type has 
been declared.

That's what motivated my use of a selector-style function to return a 
(constant) reference to the object declared in the body (or in the private 
part of the spec).  But the real issue is that a function returns a constant 
reference, so if you want to be able to use an operation to actually modify 
the object you need to do something else.

One way is to use the Rosen Trick:

package AD_Converts is
   type AD_Converter (<>) is limited private;
   procedure Op (ADC : AD_Converter);  --modifier

   function Noise return AD_Converter;
private
   type Handle (ADC : access AD_Converter) is limited null record;
   type AD_Converter is limited record
      H : Handle (ADC'Access);
   end record;
end;

package body AD_Converters is
   procedure Op (ADC : AD_Converter) is
      X : AD_Converter renames ADC.H.ADC.all;
   begin
      ... -- modify X as desired
   end;
...
end AD_Converters;

Part of the problem is that we were given a requirement that we can't use 
access types.  If we didn't have that requirement, then you do this:

package AD_Converter is
   type AD_Converter (<>) is limited private;
   procedure Modifier (ADC : access AD_Converter);
   procedure Selector (ADC : access constant AD_Converter);

   type ADC_Access is access all AD_Converter;
   for ADC_Access'Storage_Size use 0;

   Noise : constant ADC_Access;
private
   type AD_Converter is limited record ... end record;
   --no more Rosen Trick (sorry, Jean-Pierre...)

   Noise_ADC : aliased AD_Conveter;
   Noise : constant ADC_Access := Noise_ADC'Access;
end;


However, all we're really doing is using the AD_Converter type as a fancy 
way to identify a statically declared object.  We have simpler ways of doing 
that, especially if the "object" is just an array component:

package AD_Converters is
   type AD_Converter (<>) is limited private;
   procedure Modifier (ADC : in out AD_Converter);
   procedure Selector (ADC : in AD_Converter);

   Noise : constant AD_Converter;
private
   type AD_Converter is new Positive;
   Noise : constant AD_Converter := 1;
end;

package body AD_Converters is
   type Rep_Type is limited record ... end record;
   Objects : array (AC_Converter range 1 .. <whatever>) of Rep_Type;

   procedure Modify (ADC : in out AD_Converter) is
      Object : Rep_Type renames Objects (ADC);
   begin
       ...
   end;
...
end;






  reply	other threads:[~2004-10-21 22:02 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-18 11:47 Idiom for a class and an object in Ada Marin David Condic
2004-10-18 12:14 ` Martin Krischik
2004-10-18 19:40   ` Matthew Heaney
2004-10-19 12:59   ` Marin David Condic
2004-10-19 14:46     ` Martin Dowie
2004-10-19 15:55       ` Matthew Heaney
2004-10-19 18:31         ` Martin Dowie
2004-10-19 15:52     ` Matthew Heaney
2004-10-18 12:26 ` Marius Amado Alves
2004-10-19  2:09   ` Jeffrey Carter
2004-10-19  3:28     ` Matthew Heaney
2004-10-19 12:53       ` Marin David Condic
2004-10-19 14:44         ` Matthew Heaney
2004-10-19 15:01           ` Dmitry A. Kazakov
2004-10-19 15:40             ` Matthew Heaney
2004-10-20  7:58               ` Dmitry A. Kazakov
2004-10-20 12:31                 ` Marin David Condic
2004-10-20 13:53                   ` Dmitry A. Kazakov
2004-10-20 15:23                   ` Matthew Heaney
2004-10-21 12:24                     ` Marin David Condic
2004-10-21 17:15                       ` Matthew Heaney
2004-10-20  5:39         ` Simon Wright
2004-10-20  7:24           ` Matthew Heaney
2004-10-20  8:39             ` Dmitry A. Kazakov
2004-10-21  1:36             ` Jeffrey Carter
2004-10-21  1:46               ` Matthew Heaney
2004-10-21  7:51                 ` Dmitry A. Kazakov
2004-10-21 12:45                   ` Matthew Heaney
2004-10-21 14:11                     ` Dmitry A. Kazakov
2004-10-22  1:04                 ` Jeffrey Carter
2004-10-22  1:36                   ` Matthew Heaney
2004-10-21 19:31               ` Kevin Cline
2004-10-21 22:02                 ` Matthew Heaney [this message]
2004-10-22  0:10                   ` Matthew Heaney
2004-10-21  8:25             ` Martin Dowie
2004-10-20 17:04           ` Matthew Heaney
2004-10-20 19:37             ` Simon Wright
2004-10-20 20:04               ` Matthew Heaney
2004-10-22  5:37                 ` Simon Wright
2004-10-20  1:10       ` Jeffrey Carter
2004-10-20  7:04         ` Matthew Heaney
2004-10-20 12:42           ` Marin David Condic
2004-10-20 12:55             ` Matthew Heaney
2004-10-20 15:27             ` Matthew Heaney
2004-10-21  1:36               ` Matthew Heaney
2004-10-19 12:38   ` Marin David Condic
2004-10-18 16:59 ` Matthew Heaney
2004-10-18 18:02 ` Martin Dowie
2004-10-19 13:06   ` Marin David Condic
2004-10-19 14:51     ` Martin Dowie
2004-10-20 16:20 ` Michael Paus
2004-10-20 17:15   ` Matthew Heaney
2004-10-20 17:55     ` Michael Paus
2004-10-21 12:33   ` Marin David Condic
  -- strict thread matches above, loose matches on Subject: below --
2004-10-21 13:59 Stephen Leake
replies disabled

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