comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Get_Line
Date: Fri, 1 Nov 2002 18:32:08 -0500
Date: 2002-11-01T18:32:08-05:00	[thread overview]
Message-ID: <us63nof66esp1b@corp.supernews.com> (raw)
In-Reply-To: apuqd0$103i$1@msunews.cl.msu.edu


"Chad R. Meiners" <crmeiners@hotmail.com> wrote in message
news:apuqd0$103i$1@msunews.cl.msu.edu...
>
> Would you be happy with the following instead?
>
> type Line is private;
>
> procedure Get_Line (File : File_Type; Item : out Line);
>

You already have a resizable string type, so you'd be better off using that.
The easiest solution would be to create a child of Ada.Strings.Unbounded:

with Ada.Text_IO;
package Ada.Strings.Unbounded.Text_IO is
   procedure Get_Line (File : File_Type; Item : out Unbounded_String);
end;

> function  To_String (Item : Line) return String;

Note that this isn't terribly efficient, for the same reasons
Ada.Strings.Unbounded is inefficient.  At least GNAT has
Ada.Strings.Unbounded.Aux, which allows you to get at the internal string
directly.

In C++, I can return a reference.  Something like:

class C
{
public:
   const std::string& f() const;
//...
};

The closest thing in Ada is to use an access type:

type Line_Type is private;

type String_Access is access all String;

function To_String_Access (Line : Line_Type) return String_Access;

and then you could:

declare
   S : String renames To_String_Access (Line).all;  --no copying
begin

You could tighten the safety by being able to decorate the access type, e.g.

type String_Access (<>) is limited access all String;

which would prevent users from holding on to a copy of the access value
returned by the selector function (forcing them to do a rename, as above).

All the Charles containers use this technique, to allow in-place
manipluation of container elements.  So you typically have a pair of
functions:

   function Element (Container : Container_Type)
     return Element_Type;

   generic
     type Element_Access is access all Element_Type;
   function Generic_Element (Container : Container_Type)
     return Element_Access;

This is analogous to the iterator dereference operators:

   value_type& operator*() const;

which allows you to do this:

   int& histogram = *iter;
   ++histogram;

This kind of in-place manipulation is awkward without references.  In my
container example above, you'd have:

declare
   Histogram : Integer renames To_Access (Iterator).all;
begin
   Histogram := Histogram + 1;
end;

See the Charles page for more info:

http://home.earthlink.net/~matthewjheaney/charles/charles-vectors-unbounded.
html

http://home.earthlink.net/~matthewjheaney/charles/index.html

Matt








  reply	other threads:[~2002-11-01 23:32 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-29 20:24 Get_Line Justin Birtwell
2002-10-29 20:55 ` Get_Line David C. Hoos
2002-10-30  1:30 ` Get_Line Jeffrey Carter
2002-10-30 13:33   ` Get_Line Justin Birtwell
2002-10-30 14:40     ` Get_Line Preben Randhol
2002-10-30 17:09     ` Get_Line Jean-Pierre Rosen
2002-10-30 18:08     ` Get_Line Jeffrey Carter
2002-10-30 22:42       ` Get_Line Robert A Duff
2002-10-31  0:26         ` Get_Line Chad R. Meiners
2002-10-31  0:44           ` Get_Line Robert A Duff
2002-10-31 10:32             ` Get_Line John English
2002-10-31 11:30               ` Get_Line Preben Randhol
2002-10-31 13:10                 ` Get_Line John English
2002-10-31 17:39             ` Get_Line Warren W. Gay VE3WWG
2002-10-31 21:46             ` Get_Line Chad R. Meiners
2002-11-01 16:59               ` Get_Line Robert A Duff
2002-11-01 21:04                 ` Get_Line Chad R. Meiners
2002-11-01 23:32                   ` Matthew Heaney [this message]
2002-11-02  0:28                     ` Get_Line Chad R. Meiners
2002-10-31  8:53       ` Get_Line Preben Randhol
2002-10-31 18:04         ` Get_Line Jeffrey Carter
2002-11-01 11:18           ` Get_Line Preben Randhol
2002-10-30 14:44 ` Get_Line Preben Randhol
2002-10-31 21:55 ` Get_Line Matthew Heaney
  -- strict thread matches above, loose matches on Subject: below --
1999-07-02  0:00 GET LINE babefan
1999-07-02  0:00 ` czgrr
1998-06-05  0:00 get_line Steve Dyrdahl
1998-06-05  0:00 ` get_line Samuel Mize
1998-06-17  0:00   ` get_line Hans Marqvardsen
1998-06-18  0:00     ` get_line John McCabe
1998-06-21  0:00       ` get_line Robert Dewar
1998-06-06  0:00 ` get_line Dale Stanbrough
1998-06-06  0:00   ` get_line Matthew Heaney
1998-06-07  0:00     ` get_line Dale Stanbrough
1998-06-06  0:00       ` get_line Matthew Heaney
1998-06-18  0:00     ` get_line Robert I. Eachus
1998-06-06  0:00   ` get_line Robert Dewar
replies disabled

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