comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Need a way to convert a constant to a variable
Date: Sat, 5 Aug 2017 16:48:10 +0200
Date: 2017-08-05T16:48:10+02:00	[thread overview]
Message-ID: <om4lra$1jku$1@gioia.aioe.org> (raw)
In-Reply-To: om4hu2$1eb2$1@gioia.aioe.org

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.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2017-08-05 14:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-05 13:41 Need a way to convert a constant to a variable Victor Porton
2017-08-05 14:48 ` Dmitry A. Kazakov [this message]
2017-08-05 15:11   ` Victor Porton
2017-08-05 16:44     ` Dmitry A. Kazakov
2017-08-05 17:45       ` Victor Porton
2017-08-05 19:37         ` Dmitry A. Kazakov
2017-08-05 15:41 ` Jeffrey R. Carter
2017-08-05 16:25   ` Victor Porton
2017-08-05 20:12     ` Jeffrey R. Carter
2017-08-05 17:59 ` Per Sandberg
2017-08-15 20:05 ` Eryndlia Mavourneen
2017-08-15 20:07   ` Eryndlia Mavourneen
replies disabled

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