comp.lang.ada
 help / color / mirror / Atom feed
From: "Chad R. Meiners" <crmeiners@hotmail.com>
Subject: Perhaps we keep lists elements private and adjust the elements to fit?
Date: Tue, 23 Jul 2002 14:34:01 -0400
Date: 2002-07-23T14:34:01-04:00	[thread overview]
Message-ID: <ahk7lu$1mbd$1@msunews.cl.msu.edu> (raw)
In-Reply-To: uheirlk4z.fsf@gsfc.nasa.gov

"Stephen Leake" <stephen.a.leake.1@gsfc.nasa.gov> wrote in message
news:uheirlk4z.fsf@gsfc.nasa.gov...
> "chris.danx" <spamoff.danx@ntlworld.com> writes:
>
> > <snip discussion of lists supporting limited types>
>
> > Is this a worth while tradeoff?  I guess the question "how often are
limited
> > types used?" would serve better, as it would give a measure on which to
base
> > a decision.
>
> This was discussed extensively back in December 2001 during the Grace
> Lists requirements development. You could search on Google for that
> discussion.
>
> My own opinion is that supporting limited types is a requirement for
> any general-purpose container.

Perhaps what we need to do is write the container libraries to accept
private types and provide a generic package that converts limited privates
to private types.  Let's assume that we have a limited type as follows

package Mylim is

   type Mine is limited private;

   procedure Copy(Item : in Mine; Object : out Mine);

private

   type Mine is new Integer;

end Mylim;

package body Mylim is

   procedure Copy(Item : in Mine; Object : out Mine) is
   begin
      Object := Item;
   end Copy;

end Mylim;

We want to use this type in a container package(that only accepts private
types), but we don't want to convert the package to use a private type.
Instead ,we can use a generic wrapper to convert the type for use with our
favorite container package.  I very simple wrapper (in other words untested
but it does compile) would look like

with Ada.Finalization;

generic
   type Limited_Type is limited private;
   with procedure Copy(Item : in Limited_Type; Object : out Limited_Type);
package Promote is

  type Storage is private;

  function  To    (Item : in Limited_Type) return Storage;
  procedure From  (Item : in Storage; Becomes : out Limited_Type);

private

   Default_Limited_Type : Limited_Type;

   type Limited_Access is access Limited_Type;

   type Storage is new Ada.Finalization.Controlled with
      record
         Item : Limited_Access;
      end record;

   procedure Initialize(Object : in out Storage);
   procedure Adjust    (Object : in out Storage);
   procedure Finalize  (Object : in out Storage);


end Promote;

with Ada.Unchecked_Deallocation;
package body Promote is

   ------------
   -- Adjust --
   ------------

   procedure Adjust (Object : in out Storage) is

   begin
      if Object.Item /= null then
         declare
            Temp : Limited_Access := new Limited_Type;
         begin
            Copy (Object.Item.all,Temp.all);
            Object.Item := Temp;
         end;
      end if;
   end Adjust;

   --------------
   -- Finalize --
   --------------

   procedure Finalize (Object : in out Storage) is

      procedure Free is new Ada.Unchecked_Deallocation(Limited_Type,
                                                       Limited_Access
                                                      );

   begin

      Free (Object.Item);

   end Finalize;

   ----------
   -- From --
   ----------

   procedure From  (Item : in Storage; Becomes : out Limited_Type) is
   begin
      if Item.Item = null then
         Copy (Default_Limited_Type, Becomes);
      else
         Copy (Item.Item.all, Becomes);
      end if;
   end From;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Object : in out Storage) is
   begin
      null;
   end Initialize;

   --------
   -- To --
   --------

   function To (Item : in Limited_Type) return Storage is
      Temp : Storage;
   begin
      Temp.Item := new Limited_Type;
      Copy (Item, Temp.Item.all);

      return Temp;
   end To;

end Promote;

so we can then promote Mylim.Mine to the status of private type with the
following package.

with Promote;
package Mylim.Storage is new Promote(Mylim.Mine,MyLim.Copy);

This schema has the advantage that container libraries can be written to
take private types which simplifies their use to the newbie since they can
easily instantiate linked lists of integers without having to write a copy
procedure for them.  Once someone has become conformable with using limited
types they should also be comfortable with performing a type promotion.
Comments, anyone?

-Chad R. Meiners





  parent reply	other threads:[~2002-07-23 18:34 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-22 16:56 worth the hassle? chris.danx
2002-07-22 17:17 ` Wes Groleau
2002-07-22 17:45   ` chris.danx
2002-07-22 19:05     ` Wes Groleau
2002-07-22 17:56 ` chris.danx
2002-07-22 18:06   ` Stephen Leake
2002-07-22 21:06     ` chris.danx
2002-07-23 12:16       ` chris.danx
2002-07-23 12:36       ` chris.danx
2002-07-23 15:00       ` Stephen Leake
2002-07-23 15:15         ` chris.danx
2002-07-23 15:36           ` chris.danx
2002-07-23 16:19             ` Peter Hermann
2002-07-23 16:41               ` chris.danx
2002-07-23 17:35               ` Stephen Leake
2002-07-22 17:58 ` Stephen Leake
2002-07-22 18:15   ` Frank J. Lhota
2002-07-22 19:56     ` Stephen Leake
2002-07-23  5:56     ` Kevin Cline
2002-07-22 18:44   ` chris.danx
2002-07-23  5:39   ` Kevin Cline
2002-07-23 15:05     ` Stephen Leake
2002-07-23 18:34   ` Chad R. Meiners [this message]
2002-07-23 19:23     ` Perhaps we keep lists elements private and adjust the elements to fit? 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