comp.lang.ada
 help / color / mirror / Atom feed
From: Dmitry A. Kazakov <mailbox@dmitry-kazakov.de>
Subject: Re: Problem whith objects, access and abstract tagged private types
Date: Thu, 08 Jan 2004 16:22:47 +0100
Date: 2004-01-08T16:22:47+01:00	[thread overview]
Message-ID: <l5sqvv4al75orus70gesj0u9ksog2eu4e8@4ax.com> (raw)
In-Reply-To: 3ffd619b$0$22318$626a54ce@news.free.fr

On Thu, 8 Jan 2004 14:58:44 +0100, "Bruno" <b.santo@free.fr> wrote:

>Hello to all,
>
>I'm newbie in Ada95 and I have some difficulties with the language.
>I'll give you my specifications :
>
>Specif (.ads) :
>---------------
>
>   package Objets is
>
>      type Item_Type is abstract tagged private;
>      -- Action
>      procedure Traitement (F : in Item_Type);
>      -- Allows init
>      procedure Modifier (F  : in out Item_Type'Class;
>                          Nb : in     Natural);
>
>      type Item_Elmt is private;
>      function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt;
>      function To_Item_Type (F : in Item_Elmt) return Item_Type'Class;
>
>   private
>
>      type Item_Type is abstract tagged
>         record
>            Compteur : Natural := 0;
>         end record;
>      type Item_Elmt is access all Item_Type'Class;
>
>   end Objets;
>
>I chose private types for hiding implementation of my types and access
>types.
>
>My problem is that I can't make the function "To_Item_Elmt" that create
>"Item_Elmt".
>
>I try to write this body :
>
>   function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
>      My_Tag : constant Ada.Tags.Tag := F'Tag;
>   begin
>      return new My_Tag' (F);
>   end To_Item_Elmt;
>
>The compilation indicates the following errors :
>    subtype mark required in this context
>    found "My_Tag" declared at line 30
>
>If somebody could help and explain me how to resolve my problem, thanks for
>his help.

You cannot dispatch on bare tags.

1. Assuming that To_Item_Elmt indeed has to create a *copy* of the
argument, then it could be:

function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
begin
   return new Item_Type'Class'(F);
end To_Item_Elmt;

But it is a sort of suspicious design to hide the implementation of a
pointer type, not making the pointer "smart". Observe that the
following will leak:

declare
   X : Derived_From_Item_Type;
   Y : Item_Elmt := To_Item_Elmt (X);
begin
   null;
end; -- A dangling copy of X produced by To_Item_Elmt is still there

2. If To_Item_Elmt should not copy the argument but just get an access
to it, then it is impossible to do, because the argument is "in".
Although, to complete the picture, there is so-called Rosen trick to
circumvent that. [I would not recommed it for a novice] It goes as
follows:

   type Item_Type is abstract tagged limited private;
   ...
private
   type Item_Elmt is access all Item_Type'Class;
   type Item_Type is abstract tagged limited record
      Self : Item_Elmt := Item_Type'Unchecked_Access;
      Compteur : Natural := 0;
   end record;

function To_Item_Elmt (F : in Item_Type'Class) return Item_Elmt is
begin
   return F.Self;
end To_Item_Elmt;

3. However, probably, you need no Item_Elmt type at all, as a guess.

--
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  parent reply	other threads:[~2004-01-08 15:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-08 13:58 Problem whith objects, access and abstract tagged private types Bruno
2004-01-08 14:56 ` Georg Bauhaus
2004-01-08 15:22 ` Dmitry A. Kazakov [this message]
2004-01-08 18:31 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2004-01-08 14:51 amado.alves
replies disabled

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