comp.lang.ada
 help / color / mirror / Atom feed
* generic package System.Address_To_Access_Conversion
@ 1998-11-23  0:00 R Heltzel
  1998-11-23  0:00 ` Tom Moran
  1998-11-24  0:00 ` Matthew Heaney
  0 siblings, 2 replies; 3+ messages in thread
From: R Heltzel @ 1998-11-23  0:00 UTC (permalink / raw)


Could someone please send me an example piece of code that illustrates
the method of using this generic package using the GNAT compiler?

Thanks





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: generic package System.Address_To_Access_Conversion
  1998-11-23  0:00 generic package System.Address_To_Access_Conversion R Heltzel
@ 1998-11-23  0:00 ` Tom Moran
  1998-11-24  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Moran @ 1998-11-23  0:00 UTC (permalink / raw)


>Could someone please send me an example piece of code that illustrates
>the method of using this generic package using the GNAT compiler?

with System.Address_To_Access_Conversions;
...
    package Treat_As_Header
      is new
System.Address_To_Access_Conversions(Simple_Wav_Header_Type);
...
      Header : constant Treat_As_Header.Object_Pointer
        := Treat_As_Header.To_Pointer(Buffer(Buffer'first)'address);
...
      Header.RIFF_tag := "RIFF";
...





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: generic package System.Address_To_Access_Conversion
  1998-11-23  0:00 generic package System.Address_To_Access_Conversion R Heltzel
  1998-11-23  0:00 ` Tom Moran
@ 1998-11-24  0:00 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 1998-11-24  0:00 UTC (permalink / raw)


R Heltzel <HeltzeRF@wec.com> writes:

> Could someone please send me an example piece of code that illustrates
> the method of using this generic package using the GNAT compiler?

I've been using this to turn a read-only view of an object into a
read-write view.  For example, if you have an access-to-constant type:

  type Stack_Access is access constant Stack_Type;

then modifying the stack designated by such an access object is
illegal.  But, you can get around this feature by using
Addr_To_Acc_Conv:

  procedure Op (S : in Stack_Access) is

     package Conversions is
       new System.Address_To_Access_Conversions (Stack_Type);

     use Conversions;

     Read_Only_Stack : Stack_Type renames S.all;

     Read_Write_Stack : Stack_Type renames
       To_Pointer (Read_Only_Stack'Address).all;

  begin

    Push (19, On => Read_Write_Stack);

  end Op;


Of course, if you're using gnat, then you can use the attribute
T'Unrestricted_Access, which is more type-safe but less portable:

  procedure Op (S : in Stack_Access) is

    type Read_Write_Stack_Access is access all Stack_Type;

    for Read_Write_Stack_Access'Storage_Size use 0;

    SA : constant Read_Write_Stack_Access :=
      S.all'Unrestricted_Access;

    Read_Write_Stack : Stack_Type renames SA.all;

  begin

    Push (19, On => Read_Write_Stack);

  end Op;


Perhaps more clear is to just make the parameter mode "in Stack_Type"
(this is how you can implement an operation that has side effect, even
for a function).  First, let's use our little friend
Sys.Addr_To_Acc_Conv:

  procedure Op (S : in Stack_Type) is

     package Conversions is
       new System.Address_To_Access_Conversions (Stack_Type);

     use Conversions;

     Read_Write_Stack : Stack_Type renames
       To_Pointer (S'Address).all;

  begin

    Push (19, On => Read_Write_Stack);

  end Op;


(Note the mode of param S.)  Now, using T'Unrestricted_Access:


  procedure Op (S : in Stack_Type) is

    type Read_Write_Stack_Access is access all Stack_Type;

    for Read_Write_Stack_Access'Storage_Size use 0;

    SA : constant Read_Write_Stack_Access :=
      S'Unrestricted_Access;

    Read_Write_Stack : Stack_Type renames SA.all;

  begin

    Push (19, On => Read_Write_Stack);

  end Op;


Where possible, it's nice to take T'Address of an object whose type is
pass-by-reference.  That is, a tagged type, or a limited type whose
full-view is also limited.  The implementation advice in the RM
recommends that taking T'Address of a by-reference type should return "a
useful result," which will improve your chances for portability.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-11-24  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-11-23  0:00 generic package System.Address_To_Access_Conversion R Heltzel
1998-11-23  0:00 ` Tom Moran
1998-11-24  0:00 ` Matthew Heaney

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