TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Wed, 21 Feb 2001 09:54:50 +0000
TEXT/PLAIN (89 lines)
>> Does anyone know how to call an Ada 95 function by address
>> (if possible)? I
>> know you can
>> in C/C++.
>    type Trig_Spec is access function(Theta : Radians) return Float;
>    pragma Convention(StdCall, Trig_Spec); -- StdCall or whatever
>    A_Function : Trig_Spec;
>    ...
>    A_Function := -- get a pointer from C program, "access xyz",
>                  -- System.Address_To_Access_Conversions, or whatever
>    Y := A_Function(X);

You appear to have assumed that the original question related to calling
imported non-Ada functions by address.

If what you are trying to do is within Ada, you can simplify the code above by
missing out the pragma Convention, and using a different method to assign a
value to A_Function. A simple, but slightly more complete example would be:

package Callback_List is

   type Callback is access procedure; -- Access to procedure with no paramters

   type Callback_Index is range 1 .. 10;
   type Callback_List_Type is array (Callback_Index) of Callback;

   procedure Add_Callback (Index        : in Callback_Index,
                           The_Callback : in Callback);

   procedure Execute_Callbacks;

end Callback_List;

package body Callback_List is

   The_Callback_List : Callback_List_Type := (others => null);

   procedure Add_Callback (Index        : in Callback_Index,
                           The_Callback : in Callback) is
      The_callback_List (Index) := The_Callback;
   end Add_Callback;

   procedure Execute_Callbacks is
      for Index in Callback_Index loop
         if The_Callback_List (Index) /= null then
            The_Callback_List (Index).all; -- Calls the operations listed
         end if;
      end loop;
   end Execute_Callbacks;
end Callback_List;

package Use_Callback_List is

   procedure Update; -- This will be used as a callback

end Use_Callback_List;

with Callback_List;
with Ada.Text_IO;
package body Use_Callback_List is

   procedure Update is
      Ada.Text_IO.Put_Line ("Update Called");
   end Update;

   Callback_List.Add_Callback (Index        => 1,
                               The_Callback => Update'Access);

end Use_Callback_List;

Hope this helps.

This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender
immediately by telephoning +44(1252) 373232. You should not copy it
or use it for any purpose nor disclose or distribute its contents to
any other person.