From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.0 required=5.0 tests=BAYES_00,INVALID_MSGID, PP_MIME_FAKE_ASCII_TEXT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,fa7bb11bafaa51ed,start X-Google-Attributes: gid103376,public From: "Brian G. Holmes" Subject: General Access-To-Constant Parameters Date: 1997/09/23 Message-ID: <34282934.3249@gsc.gte.com>#1/1 X-Deja-AN: 274957702 X-Nntp-Posting-Host: fp03-206.rtp.gtegsc.com Sender: news@wlbr.iipo.gtegsc.com (Steven M. Schultz) X-Complaints-To: Email news@wlbr.iipo.gtegsc.com if this posting is inappropriate X-Trace: wlbr.iipo.gtegsc.com 875043303 23041 (None) [192.133.124.206] Organization: GTE Government Systems Reply-To: brian.holmes@gsc.gte.com Newsgroups: comp.lang.ada Date: 1997-09-23T00:00:00+00:00 List-Id: 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; -----------------------------------------------------------------------------------------------------------------------