comp.lang.ada
 help / color / mirror / Atom feed
* General Access-To-Constant Parameters
@ 1997-09-23  0:00 Brian G. Holmes
  1997-09-24  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 2+ messages in thread
From: Brian G. Holmes @ 1997-09-23  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 4910 bytes --]


Why doesn�t the Ada95 language support general access-to-constant
parameters?

General access parameters are a very useful construct of the Ada95
language.  Their use and advantages are discussed in several sections of
the Rationale.  I have also seen the construct abused in several
bindings to C software; a general access parameter - an access-to-object
parameter - is used where an access-to-constant parameter should have
been used.  An example of what I consider general access parameter abuse
follows.

Brian G. Holmes
GTE Government Systems Corporation
brian.holmes@gsc.gte.com


-----------------------------------------------------------------------------------------------------------------------
--
--  General_Access_Parameter_Abuse_Example...
--
--  Illustrates the abuse of general access parameters when creating
bindings.
--
--  The example is meant to show how parameters that might better accept
access-to-constant values are coded to accept
--  access-to-variable values.  The result is that the user must convert
constant objects into variable objects before
--  using the interface.  The example is somewhat contrived, but it is
easy to see how it might be extended to a more
--  complex and less familiar function.
--
--  The memory.h and string.h files both declare the memcpy function as
"void *memcpy(void *, const void *, size_t);".
--
--  19970923 BGH; created.
--
-----------------------------------------------------------------------------------------------------------------------

with Ada.Text_IO;
with Interfaces.C;
procedure General_Access_Parameter_Abuse_Example is


        type    Identification_Information is tagged record
                Number : Interfaces.C.int;
                Text   : Interfaces.C.char_array(0..80);
                end record;

        Bits_Per_Byte                        : constant := 8;

        Identification_Information_Byte_Size : constant
Interfaces.C.size_t :=
                                              
Interfaces.C.size_t(Identification_Information'size/Bits_Per_Byte);

        --

        procedure Bad_Interface( Name:  in out
Identification_Information;
                                 Given: in    
Identification_Information ) is

                procedure Memcpy( Target: access
Identification_Information;
                                  Source: access
Identification_Information;
                                  Length: in     Interfaces.C.size_t );

                pragma Import( C,Memcpy,"memcpy" );

                Local_Copy_Of_Given: aliased Identification_Information
:= Given;

        begin

                Memcpy(
Name'access,Local_Copy_Of_Given'access,Identification_Information_Byte_Size
);

        end Bad_Interface;

        --

        procedure Good_Interface( Name:  in out
Identification_Information;
                                  Given: in    
Identification_Information ) is

                type        Constant_Char_Array_Access is access
constant Identification_Information;

                procedure Memcpy( Target: access
Identification_Information;
                                  Source: in    
Constant_Char_Array_Access;
                                  Length: in     Interfaces.C.size_t );

                pragma Import( C,Memcpy,"memcpy" );

        begin

                Memcpy(
Name'access,Given'access,Identification_Information_Byte_Size );

        end Good_Interface;

        --

        Constant_Identifier : constant Identification_Information :=
(332211,(0..1=>'0',others=>Interfaces.C.nul));

        Identifier          : Identification_Information;


begin


        Ada.Text_IO.Put_Line( "*" );
        Ada.Text_IO.Put_Line( "* 
General_Access_Parameter_Abuse_Example..." );
        Ada.Text_IO.Put_Line( "* 
=========================================" );

        Ada.Text_IO.Put_Line( "*  Constant_Identifier is (" &
                             
Interfaces.C.int'image(Constant_Identifier.Number) & ",""" &
                             
Interfaces.C.To_Ada(Constant_Identifier.Text) & """ )." );

        Bad_Interface( Identifier,Constant_Identifier );

        Ada.Text_IO.Put_Line( "*  Identifier          is (" &
                              Interfaces.C.int'image(Identifier.Number)
& ",""" &
                              Interfaces.C.To_Ada(Identifier.Text) & """
)." );

        Good_Interface( Identifier,Constant_Identifier );

        Ada.Text_IO.Put_Line( "*  Identifier          is (" &
                              Interfaces.C.int'image(Identifier.Number)
& ",""" &
                              Interfaces.C.To_Ada(Identifier.Text) & """
)." );

        Ada.Text_IO.Put_Line( "* 
=========================================" );
        Ada.Text_IO.Put_Line( "*" );


end General_Access_Parameter_Abuse_Example;

-----------------------------------------------------------------------------------------------------------------------




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

* Re: General Access-To-Constant Parameters
  1997-09-23  0:00 General Access-To-Constant Parameters Brian G. Holmes
@ 1997-09-24  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 2+ messages in thread
From: Matthew Heaney @ 1997-09-24  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2015 bytes --]


In article <34282934.3249@gsc.gte.com>, brian.holmes@gsc.gte.com wrote:

>Why doesn�t the Ada95 language support general access-to-constant
>parameters?

This question was answered by Tucker Taft; I got the quote below from DejaNews.


>>Subject:      Re: Two ideas for the next Ada Standard
>>From:         stt@henning.camb.inmet.com (Tucker Taft)
>>Date:         1996/09/06
>>Message-Id:   <DxBMIy.EG.0.-s@inmet.camb.inmet.com>
>>Newsgroups:   comp.lang.ada
>>[More Headers]
>>
>>Jonas Nygren (ehsjony@ehs.ericsson.se) wrote:
>>: ...
>>: If we could have written:
>>
>>: procedure P1 (Obj : access constant T);
>>
>>: We could then call P1(Obj). Everything would then have been
>>: symmetrical and beautiful (again in my eyes).
>>
>>: And I can't see that it would have been too difficult to implement
>>: in a compiler.
>>
>>: ... I believe for e.g. that Ada would benefit by the 'access const'
>>: construct. ...
>>
>>So do I.  This was just a boo-boo.  There were some subtle reasons
>>why we left this out, but in retrospect, they seem far too subtle
>>to have won the day.  There was enormous pressure to leave out
>>"marginal" changes, and sometimes, this pressure created some
>>strange incentives.  I think many people today would agree that
>>allowing "access constant" in a parameter would be a simplification
>>rather than an added complication in the language.
>>
>>Luckily (and in my biased opinion), this is one of the few cases
>>where we really goofed.  I would agree that "access constant" parameters
>>should be one of the first minor updates to be considered.  

[snip]

>>-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
>>Intermetrics, Inc.  Cambridge, MA  USA

This 

is 

some 

dummy

text

necessary

because

my

newsreader

won't 

allow

me

to

post

more

quote

lines

than

new

text.

Please

ignore.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-23  0:00 General Access-To-Constant Parameters Brian G. Holmes
1997-09-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