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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: Victor Porton Newsgroups: comp.lang.ada Subject: Re: Need a way to convert a constant to a variable Date: Sat, 05 Aug 2017 18:11:06 +0300 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: qrWnZLr+FAc2xoElQM2hng.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.14.10 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:47608 Date: 2017-08-05T18:11:06+03:00 List-Id: Dmitry A. Kazakov wrote: > On 2017-08-05 15:41, Victor Porton wrote: >> I've sent the following email to ada-comment mailing list. I duplicate it >> here. >> >> !topic Need a way to convert a constant to a variable >> !reference Ada 2012 RM >> !from Victor Porton 17-08-05 >> !keywords constant, variable, view, conversion >> !discussion >> >> Sometimes one needs to convert a constant view into variable view (I am >> fully conscious that after this the programmer should take care not to >> change the object of the view). >> >> In the following (not compilable with GNAT 7.1.0) code I present my >> best attempt to solve the following problem: >> >> Write a function with an "in" indefinite holder with a string, return >> chars_ptr corresponding to the string. >> >> It looks like there is no solution of this in Ada 2012 :-( >> >> with Interfaces.C; use Interfaces.C; >> with Interfaces.C.Strings; use Interfaces.C.Strings; >> with Ada.Containers.Indefinite_Holders; >> >> procedure Conv is >> >> package Char_Array_Holders is >> new Ada.Containers.Indefinite_Holders(char_array); >> >> type C_String_Holder is new Char_Array_Holders.Holder >> with null record; >> >> function To_C_String (Object: C_String_Holder) return chars_ptr is >> Value: char_array renames Constant_Reference(Object).Element.all; >> Value2: aliased Char_Array(Value'Range) with Import; >> for Value2'Address use Value'Address; >> begin >> return To_Chars_Ptr(Char_Array_Access'(Value2'Access)); >> end; >> begin >> null; >> end; > > AFAIK, Ada.Containers.Indefinite_Holders was designed in a way to > prevent specifically what you want. > >> Can any problem with different representation of constant and variables >> appear? > > 1. Don't use Ada.Containers.Indefinite_Holders? > > 2. Don't use char_array. It has restricted use when interfacing C. You > probably need chars_ptr put into a controlled type if you want safe C > strings. This is basically same as Holder internally is, except that you > are in full control. I do use char_array for interfacing with C. It is a pity that I need to reimplement Indefinite_Holders for this task. -- Victor Porton - http://portonvictor.org