comp.lang.ada
 help / color / mirror / Atom feed
* Re: Array of procedures?
@ 1991-11-27 16:48 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!gvlf
  0 siblings, 0 replies; only message in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!gvlf @ 1991-11-27 16:48 UTC (permalink / raw)


jonesm@nic.cerf.net (Matthew Jones) writes:
> I know that there are arrays of task types and that is nice,
> but is there also an array of procedure coonstruct, or
> an array of package construct. Are there any good ways
> of simulating arrays of the above without using tasks.
> 
> Thanks in advance,
> 
> Matthew Jones
> jonesm@cerf.net

I don't believe there is a "legal" way to create an array of procedures.

However, in a desperate attempt to remove tasks from an Ada program I 
have in may life time created a pseudo array of "pointers" to procedures.
Telesoft Ada does return a good value from 'Address of a procedure.  You 
can save the address in a table and call the subprogram at a later time 
using its address. 

<<WARNING WARNING>> This is very dangerous, not portable, and not
really a very good thing to do. A good self-respecting Ada programmer
would NEVER do this!!

Heres how to do it.

Save the proc'address in an array. They better have all the same parameter
type or you better know the parameters expected by the procedure.

  procedure Try_It ( Param : in Integer );

  table(position) := Try_It'Address;

To call the procedure go into a new scope containing a declaration of the
subprogram. The declaration should EXACTLY match the original subprogram.
Use the editor to grab the original declaration and paste if necessary.
You will need to have visibility to all the types used by the subprogram.


  procedure Make_Call_By_Address is
    type Space_of_32_Type is range -2**31..2**31-1;
    for Space_of_32_Type'Size use 32;
 
    type Spacer_Type is
      record
        Space_of_32_Bits : Space_of_32_Type;
        Another_32_Bits  : Space_of_32_Type;
      end record;
    for Spacer_Type'Size use 64;

    Top_Stack_Guard    : Spacer_Type;
    Bottom_Stack_Guard : Spacer_Type;

    procedure Try_It ( Param : in Integer );
    for Try_It use at table(position);
    PRAGMA Interface(Assembly, Try_It);
    
  begin
     -- Reference the variables so they are not optimized away.
     if (Bottom_Stack_Guard'Address = Top_Stack_Guard'Address) then
       raise Program_Error; -- This can never happen.
     end if;

    Try_It ( Param => 666 );
  end;

The pragma Interface fools the compiler into believing that the
declaration is ok (BAD).  The compiler will put the parameters 
onto the stack correctly.  The unused local data items protect
the stack from the pop of parameters by both the called subprogram 
and the calling subprogram (Kludge code if I ever saw it). 

As I have stated this Unsafe, Unwise, Bad Coding practice. Don't
do it at home. 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1991-11-27 16:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-11-27 16:48 Array of procedures? cis.ohio-state.edu!zaphod.mps.ohio-state.edu!unix.cis.pitt.edu!dsinc!gvlf

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