comp.lang.ada
 help / color / mirror / Atom feed
From: Martin <martin@thedowies.com>
Subject: Re: Passing a String to a C/C++ Subprogram (Special Case)
Date: Wed, 16 Oct 2013 10:54:45 -0700 (PDT)
Date: 2013-10-16T10:54:45-07:00	[thread overview]
Message-ID: <50cd020a-480f-4569-990b-b2861f628bc1@googlegroups.com> (raw)
In-Reply-To: <54aa8ad2-cd98-4f3e-bef9-494ddaefe4e9@googlegroups.com>

On Wednesday, October 16, 2013 6:42:29 PM UTC+1, Eryndlia Mavourneen wrote:
> On Wednesday, October 16, 2013 12:32:36 PM UTC-5, Martin wrote:
> 
> > On Tuesday, October 15, 2013 10:02:17 PM UTC+1, Eryndlia Mavourneen wrote:
> 
> > 
> 
> > > On Tuesday, October 15, 2013 3:11:30 PM UTC-5, Adam Beneschan wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > On Tuesday, October 15, 2013 12:54:18 PM UTC-7, Eryndlia Mavourneen wrote:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > I have a couple of strings that are bounded; that is, they are essentially fixed-length strings, and I need to pass them as "in" parameters to a subprogram that may or may not be written in C/C++.  I don't really want to hassle with the Interfaces packages for this, since the subprogram language is indefinite, and the strings are part of a package that many others likely will want to use, and the strings most likely will be used as standard Ada fixed-length strings.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > Q: Is there any reason that I cannot just declare each of them in this way, assuming the subprogram is written in C/C++:
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >      . . .
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         A_String : String (1 .. 30);
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         pragma Convention (C, A_String);
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         . . .
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         procedure C_Prog (C_String : in String);
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         pragma Import (Entity => C_Prog, ...);
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >      begin
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >         C_Prog (C_String => A_String);
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > >      . . .
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > > and have it work properly?
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > B.3(70) says, "An Ada parameter of an array type with component type T, of any mode, is passed as a t* argument to a C function, where t is the C type corresponding to the Ada type T."  Since a String is defined as an array of Character in Ada, this should cause a "char*" argument to be passed.  However, this is in a section marked "Implementation Advice", so there's a possibility that a particular Ada compiler may not conform.  You'll want to check the Ada compiler manual and/or try it yourself and see what code it generates.  Note that this means the 'First and 'Last of the string parameter won't be passed to the C routine.  But it looks like you know that already. 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > >                                -- Adam
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > Yes, Adam.  Thank you.  Of course, Annex B is optional anyway.  lol
> 
> > 
> 
> > > 
> 
> > 
> 
> > > I guess you choose your poison.
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > 
> 
> > 
> 
> > > -- Eryndlia Mavourneen (KK1T)
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > Annex B is part of the 'core language' - it isn't optional. Annex A and J are also 'core'. The 'Special Needs Annex' sections are the 'optional' parts of the language. See 1.1.2 and 1.1.3.
> 
> > 
> 
> > 
> 
> > 
> 
> > -- Martin
> 
> 
> 
> In the Ada95 LRM (I have to use Ada95.), in the introduction to Annex B (B 2/3) it states:  "Support for interfacing to any foreign language is optional."
> 
> 
> 
> -- Eryndlia Mavourneen (KK1T)

That looks like Ada2012 RM rather than Ada95 (or Ada2005) [http://www.ada-auth.org/standards/12rm/html/RM-B.html].

But I do like the Ada2012 wording...it seems clearer - either the compiler has to indicate the lack of support; or exception has to be raised. If neither of those occur then your code should work as expected.

-- Martin


  reply	other threads:[~2013-10-16 17:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-15 19:54 Passing a String to a C/C++ Subprogram (Special Case) Eryndlia Mavourneen
2013-10-15 20:11 ` Adam Beneschan
2013-10-15 21:02   ` Eryndlia Mavourneen
2013-10-16  7:17     ` Dmitry A. Kazakov
2013-10-16 15:14       ` Jeffrey Carter
2013-10-16 17:32     ` Martin
2013-10-16 17:42       ` Eryndlia Mavourneen
2013-10-16 17:54         ` Martin [this message]
2013-10-16 18:13           ` Eryndlia Mavourneen
2013-10-16 19:40             ` Simon Wright
2013-10-16 20:02               ` Eryndlia Mavourneen
2013-10-19  2:11           ` Randy Brukardt
2013-10-16 19:11       ` Adam Beneschan
2013-10-16 21:31         ` Martin
2013-10-16 21:41           ` Adam Beneschan
2013-10-16 23:15             ` Martin
2013-10-15 20:59 ` Jeffrey Carter
2013-10-15 21:13   ` Eryndlia Mavourneen
2013-10-15 22:19     ` Shark8
2013-10-16  0:00     ` Jeffrey Carter
2013-10-17  9:55     ` Georg Bauhaus
2013-10-16 20:17 ` sbelmont700
2013-10-16 20:48   ` Adam Beneschan
2013-10-17 13:05     ` Eryndlia Mavourneen
2013-10-17 13:58       ` sbelmont700
2013-10-17 16:28         ` 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