comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Wrap-Up (was Re: Unchecked_Conversion on different sized types -- problem?)
Date: 2000/01/15
Date: 2000-01-15T00:00:00+00:00	[thread overview]
Message-ID: <ETSf4.273$541.8047@newsread1.prod.itd.earthlink.net> (raw)
In-Reply-To: qaIf4.469$dw3.16808@news.wenet.net

In article <qaIf4.469$dw3.16808@news.wenet.net> , "Mike Silva" 
<mjsilva@jps.net> wrote:

> 4) Enumerations with representation clauses seem to be a little like piggy
> banks -- easy to fill, but harder to extract from.

Don't bother using rep clauses for enum types.  Just use an integer
type.


> So, my solution is to map an enumeration to an array of strings.  Thanks to
> all for your comments.


Someone showed how to use Unbounded_String for this.  There are a couple
of other ways:

1) Use static strings:

  A_String : aliased constant String := "this is a test";
  B_String : aliased constant String := "of the emergency";
  C_String : aliased constant String := "broadcast systems";

  type Index_Subtype is (A, B, C);

  type String_Access is access constant String;

  type String_Table is array (Index_Subtype) of String_Access;

  Table : constant String_Table :=
    (A => A_String'Access,
     B => B_String'Access,
     C => C_String'Access);
begin
    ... Table (B).all ...


This avoids heap use.

2) You can use "dynamic" strings, which get allocated statically:

  type Index_Subtype is (A, B, C);

  type String_Access is access constant String;

  type String_Table is array (Index_Subtype) of String_Access;

  Table : constant String_Table :=
    (A => new String'("this is a test"),
     B => new String'("of the emergency"),
     C => new String'("broadcast system"));


The strings are all static, and because they're designated by a
pointer-to-constant string access object, the compilar is smart enough
to do the allocation statically.  This avoids heap use.


Compilar writers: if we use a little function to call the allocator, ie

  function "+" (S : String) return String_Access is
  begin
    return new String'(S);
  end;

  Table : constant String_Table :=
    (A => +"this is a test",
     B => +"of the emergency",
     C => +"broadcast sytsem");
begin

then is the allocation still done statically?




  reply	other threads:[~2000-01-15  0:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-13  0:00 Unchecked_Conversion on different sized types -- problem? Mike Silva
2000-01-13  0:00 ` Bryce Bardin
2000-01-13  0:00   ` Mike Silva
2000-01-13  0:00     ` Mike Silva
2000-01-14  0:00       ` Bryce Bardin
2000-01-14  0:00         ` Mike Silva
2000-01-14  0:00   ` Matthew Heaney
2000-01-13  0:00 ` Jim Rogers
2000-01-13  0:00   ` Mike Silva
2000-01-13  0:00     ` James S. Rogers
2000-01-13  0:00 ` reason67
2000-01-14  0:00 ` Jeff Carter
2000-01-14  0:00 ` Keith Thompson
2000-01-16  0:00   ` David A. Cobb
2000-01-14  0:00 ` Vladimir Olensky
2000-01-14  0:00 ` Wrap-Up (was Re: Unchecked_Conversion on different sized types -- problem?) Mike Silva
2000-01-15  0:00   ` Matthew Heaney [this message]
2000-01-15  0:00     ` Robert A Duff
2000-01-14  0:00 ` Unchecked_Conversion on different sized types -- problem? Werner Pachler
2000-01-14  0:00   ` Bryce Bardin
2000-01-14  0:00   ` reason67
replies disabled

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