comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Array Index: substitute integer with enumeration
Date: Tue, 9 Jun 2020 10:25:35 -0700 (PDT)
Date: 2020-06-09T10:25:35-07:00	[thread overview]
Message-ID: <ea604de9-8c3c-4dbd-a44a-24990e46322do@googlegroups.com> (raw)
In-Reply-To: <hjsnauF9ikeU1@mid.individual.net>

On Thursday, June 4, 2020 at 10:54:57 AM UTC-6, hreba wrote:
> Just as an example, let's assume there is a general geometry package 
> which requires a function from the client which transforms cartesian 
> coordinates into a client-specific coordinate system:
> 
> type Point is array (range 1..3) of Float;
> 
> type Transform_T is access Function (p: Point) return Point;

You don't need accesses in any of your program-facing compilation-units; you *might* for the library-facing interfaces. The type Point can also be modeled differently for the program-facing side and the library-facing side. — That is, you can do everything you need to interface the library in the private-section and/or body (body would arguably be preferable, because if you discard the library's usage, you've constrained the library only to the body and thus only have one thing to change.

Using coordinates + enumerations you could, perhaps, use:
   Type Point is record
      r, phi, theta: Interfaces.IEEE_Float_32;
   End Record
   with Size => Interfaces.IEEE_Float_32*3;

and then in your library interfacing part, have:
    Type Library_Point is Array(1..3) of Interfaces.IEEE_Float_32
       with Convention => Fortran, Size => Interfaces.IEEE_Float_32*3;
    Pragma Assert( Point'Size = Library_Point'Size,
                  "Point-type sizes must match."    );
    -- Imported functions.
    -- To_Cartesian and To_Spherical, etc.
    
    Function Convert( Input : Point ) return Library_Point is
       -- Isolating the Unchecked Conversion.
       Function Cvt is new Ada.Unchecked_Conversion(Point, Library_Point);
    Begin
       Return Result : constant Library_Point := To_Cartesian( Cvt(Input) );
    End Convert;
    
    Function Convert( Input : Library_Point ) return Point is
       -- Isolating the Unchecked Conversion.
       Function Cvt is new Ada.Unchecked_Conversion(Library_Point, Point);
    Begin
       Return Result : constant Library_Point := To_Spherical( Cvt(Input) );
    End Convert;

and take your conversion functions
    To_Cartesian and To_Spherical and passing their accesses to the imported functions that need them.


TL;DR — Make the interface you want to present to your program first then, in the body, do all your interfacing and library-importing.

> My questions are now:
> 
> 1. Does the initialization of pc involve any copying at run time?
IIRC, parameters are passed as reference for arrays and records; so if that's what you're using to model a point, it shouldn't for functions. Initialization, especially with aggregates, is a different matter and one I'm less sure on. (I seem to recall someone, Lucretia?, doing some work with GNAT and coming to the conclusion that if there was an "Others" in the aggregate there would be a loop generated, which might have copying.)
> 2. If so, how can it be avoided?
Usually by things like shown: isolate your critical functions, and then (a) present an interface for the rest of the program to use while (b) ensuring those critical functions behave as you want. (This might take a little more expertise on the subject, as it sounds like you could be messing with embedded and my experience there is extremely limited.)

      parent reply	other threads:[~2020-06-09 17:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 16:54 Array Index: substitute integer with enumeration hreba
2020-06-04 17:49 ` Jeffrey R. Carter
2020-06-05 15:16   ` hreba
2020-06-05 17:15     ` Jeffrey R. Carter
2020-06-09 17:25 ` Shark8 [this message]
replies disabled

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