From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Marek Newsgroups: comp.lang.ada Subject: Re: Messing with access types... Date: Mon, 28 Dec 2020 12:43:15 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 28 Dec 2020 11:43:16 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="90e115cbd48b0617eb8c616852774cd4"; logging-data="25383"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NUMGuy0HRAuav0vUizwIg" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 Cancel-Lock: sha1:wQfrb0UnEQxSTYR5deH5SVSyauU= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:60963 List-Id: On 28.12.2020 11:14, Dmitry A. Kazakov wrote: > > What are you trying to achieve? Because the code does not make much > sense to me. > > P.S. If you are trying to do some C bindings flat arrays are easier > choice than Interfaces.C.Pointers. C has no arrays, but pointers. Ada's > arrays are never compatible with any pointers, except when flat. > Therefore using access to String would be wrong in most, if not all, cases. > Code is a fragment some plugin framework for host written in C. When plugin is initialized it receives pointer to host features. Then I can scan them to find concrete feature (Id) and retrieve data. Problem is with instantiation generic function Get_Data, more precisely in line: return T_Access (ATA.To_Pointer (F.Data)); OK, some changes to code to clear view: with System; package Util is type Feature is record Id : Integer; Data : System.Address; end record; type Feature_Access is access all Feature; Null_Feature : constant Feature := (0, System.Null_Address); type Feature_Array is array (Natural range <>) of aliased Feature; generic type T is private; type T_Access is access all T; function Get_Data (H : access Feature; Id : Integer) return T_Access; generic type T is private; type T_Access is access all T; function Get_Query (H : access Feature; Id : Integer; Data : in out T_Access; Required : Boolean) return Integer; end Util; ------------------------ pragma Ada_2012; with Interfaces.C.Pointers; with System.Address_To_Access_Conversions; package body Util is package Ptr is new Interfaces.C.Pointers (Index => Natural, Element => Feature, Element_Array => Feature_Array, Default_Terminator => Null_Feature); use Ptr; -------------- -- Get_Data -- -------------- function Get_Data (H : access Feature; Id : Integer) return T_Access is Pointer : Ptr.Pointer := Ptr.Pointer (H); package ATA is new System.Address_To_Access_Conversions (T); begin if H /= null then loop declare F : access Feature := Pointer; begin if Id = F.Id then return T_Access (ATA.To_Pointer (F.Data)); end if; end; Ptr.Increment (Pointer); exit when Pointer = null; end loop; end if; return null; end Get_Data; --------------- -- Get_Query -- --------------- function Get_Query (H : access Feature; Id : Integer; Data : in out T_Access; Required : Boolean) return Integer is function Get is new Get_Data (T, T_Access); begin Data := Get (H, Id); if Required and (Data /= null) then return Id; end if; return 0; end Get_Query; end Util;