comp.lang.ada
 help / color / mirror / Atom feed
From: "Grein, Christoph (Fa. ESG)" <Christoph.Grein@eurocopter.com>
To: comp.lang.ada@ada-france.org
Subject: AW: Why does this work? (overloads)
Date: Thu, 08 Feb 2007 13:05:22 +0100
Date: 2007-02-08T13:10:02+01:00	[thread overview]
Message-ID: <mailman.84.1170936354.18371.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <1170931228.165961.8170@m58g2000cwm.googlegroups.com>

Try this. Not very elegant, but works.

with Ada.Exceptions;

with Ada.Text_IO, Ada.Float_Text_IO;
use  Ada.Text_IO, Ada.Float_Text_IO;

with Ada.Unchecked_Conversion;

procedure Test_it is

  N: constant := 10;

  type Vector is array (Integer range <>) of Float;
  -- Fat pointers don't work (include bounds).
  -- type Vector_Ptr is access all Vector;
  -- subtype Vector_Ptr_1   is Vector_Ptr (  1 .. N      );
  -- subtype Vector_Ptr_101 is Vector_Ptr (101 .. N + 100);

  -- Thin pointers (don't include bounds):
  type Vector_Ptr_1   is access all Vector (  1 .. N      );
  type Vector_Ptr_101 is access all Vector (101 .. N + 100);

  function Convert is new
    Ada.Unchecked_Conversion (Vector_Ptr_1, Vector_Ptr_101);

  X: aliased Vector (1 .. N);  -- constrained nominal subtype
  P: Vector_Ptr_1   := X'Access;
  Q: Vector_Ptr_101 := Convert (P);

begin

  for i in X'Range loop
    X (i) := Float (i);
  end loop;

  for i in P'Range loop
    Put (Integer'Image (i));  Put (P (i));  New_Line;
  end loop;

  for i in Q'Range loop
    Put (Integer'Image (i));  Put (Q (i));  New_Line;
  end loop;

exception
  when Exc: others =>
    Put_Line ("Unexpected exception " &
              Ada.Exceptions.Exception_Name (Exc));

end Test_it;

-----Ursprüngliche Nachricht-----
Jerry wrote:

Good point. However, it uses twice the memory, and worse, changes made
in A are not reflected in B without repeating the assignment A := B
(correct?) and vice versa. My Pascal trick had neither of these
problems yet carried boundary checking using either name.

Maybe there's a way to do this using unrestricted_access variables of
GNAT.

Jerry




  parent reply	other threads:[~2007-02-08 12:05 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-07  4:39 Why does this work? (overloads) Jerry
2007-02-07  6:45 ` AW: " Grein, Christoph (Fa. ESG)
2007-02-07  7:07   ` Jerry
2007-02-07  7:26     ` AW: " Grein, Christoph (Fa. ESG)
2007-02-07 20:43       ` Jerry
2007-02-07 19:09   ` AW: " Jeffrey R. Carter
2007-02-07 21:13     ` Jerry
2007-02-07 21:33       ` Ludovic Brenta
2007-02-07 21:36       ` Randy Brukardt
2007-02-07 19:02 ` Jeffrey R. Carter
2007-02-07 19:21   ` Adam Beneschan
2007-02-07 19:25     ` Adam Beneschan
2007-02-08  0:51       ` Jeffrey R. Carter
2007-02-07 23:12     ` Robert A Duff
2007-02-08  0:49     ` Jeffrey R. Carter
2007-02-07 20:53   ` Jerry
2007-02-08  0:53     ` Jeffrey R. Carter
2007-02-08 10:40       ` Jerry
2007-02-08 11:34         ` Georg Bauhaus
2007-02-08 12:05         ` Grein, Christoph (Fa. ESG) [this message]
2007-02-08 18:52         ` Jeffrey R. Carter
2007-02-09  0:56         ` Jerry
2007-02-09  1:27         ` tmoran
replies disabled

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