comp.lang.ada
 help / color / mirror / Atom feed
From: bishopm@source.asset.com (Michael M. Bishop)
Subject: Re: Addressing functions (long)
Date: 17 Dec 1994 02:23:58 -0500
Date: 1994-12-17T02:23:58-05:00	[thread overview]
Message-ID: <3cu3me$fvd@source.asset.com> (raw)
In-Reply-To: 3clisk$9th@felix.seas.gwu.edu

In article <3clisk$9th@felix.seas.gwu.edu>,
Michael Feldman <mfeldman@seas.gwu.edu> wrote:
>In article <3citrc$bb5@earth.usa.net>, Bill Buckley <wbuckley@earth> wrote:
>> 
>>High Ada lovers, I've got a question on storing subprogram address in 
>>Ada.  I know I can get the address of a subprogram via the ADDRESS 
>>attribute in Ada but can not figure a way to use it.  I don't have any 
>>immediate need for such a procedure but it has my old C-C++ courisity 
>>wondering.  For instance if I were to have an array of subprogram addresses
>>A(1..2) => (1 => Print1'ADDRESS,
>>            2 => Print2'ADDRESS);
>>how could I implement the calling these functions (note : I know the 
>>syntax above is wrong but can't get this simple editor to back up to 
>>change it).  I have heard from several other Ada programmers that this 
>>may not be possible. 
>
>You can usually get something reasonably meaningful by casting the address
>to an integer. Instantiate Unchecked_Conversion: 
>
>FUNCTION AddressToInt IS NEW Unchecked_Conversion 
>  (Source => System.Address, Target => Integer);
>
>you can then use a normal Integer_IO instance to display it. If the
>instance is called My_Int_IO, then
>
>   My_Int_IO.Put(Item => AddressToInt(Print1'Address), Base => 16);
>
>should display it nicely in hexadecimal.
>
>Mike Feldman

I think Bill is asking about a way to call the above functions whose
addresses are stored in the array. I've done this by passing the
subprogram addresses to a C function that does the actual calling. This
might be a compiler-specific solution as it uses pragmas. I'm using
SunAda version 1.1 on SunOS 4.1. The C routine looks like this (in this
example, I'm passing a parameter back to the Ada procedure):

pragma Wierd_C_Stuff (On);

void call_procedure_with_a_parameter (proc_addr, parm)
  int    proc_addr;
  double parm;  /* use whatever parameter type you need */
{
  union {
    int procedure_address;
    void (*procedure) ();
  } proc_union;

  proc_union.procedure_address = proc_addr;
  proc_union.procedure (parm);
}

pragma Wierd_C_Stuff (Off);

I may have messed up the syntax for the procedure pointer member of the
union. I apologize if I did. In order to interface the above C function
with Ada, I need a couple of pragmas.

pragma Interface (C, call_procedure_with_a_parameter);

procedure Procedure_To_Be_Called
  (Parm : in Float) is  -- in SunAda, Float is equivalent to C's double
begin
  < whatever goes here >
end Procedure_To_Be_Called;

pragma External (C, Procedure_To_Be_Called);

procedure Call_Procedure_By_Address is
begin
  Call_Procedure_With_A_Parameter (Procedure_To_Be_Called'address,
      3.1416);
end Call_Procedure_By_Address;

The pragma External informs the compiler that Procedure_To_Be_Called
will be called by a C function. I hope this helps.


-- 
| Mike Bishop              | The opinions expressed here reflect    |
| bishopm@source.asset.com | those of this station, its management, |
| Member: Team Ada         | and the entire world.                  |



  parent reply	other threads:[~1994-12-17  7:23 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-12-13  1:36 Addressing functions Bill Buckley
1994-12-13 10:14 ` Robert I. Eachus
1994-12-14 13:59   ` Theodore E. Dennison
1994-12-16 14:18   ` Arthur Evans Jr
1994-12-17 17:27     ` Robert Dewar
1994-12-17 17:32     ` Robert Dewar
1994-12-18  6:44       ` Bill Buckley
1994-12-16 16:56   ` Robert Dewar
1994-12-13 19:04 ` Bob Duff
1994-12-14  1:48 ` Michael Feldman
1994-12-16 14:53   ` Robert Dewar
1994-12-17  7:23   ` Michael M. Bishop [this message]
1994-12-18  6:53     ` Addressing functions (long) Bill Buckley
1994-12-14 12:33 ` Addressing functions Mitch Gart
1994-12-15 16:26   ` Erich A. Pfeiffer
replies disabled

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