From: tmoran@acm.org
Subject: Re: Subprogram Pointer in a Generic
Date: Thu, 17 Apr 2003 05:27:38 GMT
Date: 2003-04-17T05:27:38+00:00 [thread overview]
Message-ID: <eXqna.513677$S_4.562153@rwcrnsc53> (raw)
In-Reply-To: slrnb9s72m.hvi.sam@willow.dyn.rfc1149.net
> It is the other way around: he wants to call the procedure taking
> the access-to-subprogram formal parameter *from* within the body of
> the generic package.
> (2) Is there a more robust way of working around it?
You want to be able to save away a pointer to a procedure? A tagged
type carries a vector of procedure pointers, so an "access T'class"
is effectively a simple pointer to a list of (one or more) pointers.
The following code registers two instantiations of a generic, one
with a "type T is Positive" and the other with "type T is Boolean".
It then calls the saved registerees, printing for T'image(T'first)
1
FALSE
package Testr is
type Registerable_Type is abstract tagged null record;
procedure Proc(Dummy : Registerable_Type) is abstract;
type Ptr_To_Registerable_Type is access all Registerable_Type'Class;
procedure Register(Registeree : Ptr_To_Registerable_Type);
procedure Call(Index : in Integer);
end Testr;
package body Testr is
P : array (1 .. 3) of Ptr_To_Registerable_Type;
N : Natural := 0;
procedure Register(Registeree : Ptr_To_Registerable_Type) is
begin
N := N + 1;
P(N) := Registeree;
end Register;
procedure Call(Index : in Integer) is
begin
Proc(P(Index).all);
end Call;
end Testr;
with Testr;
generic
type T is (<>);
package Testrg is
procedure Register_It;
private
type My_Registerable_Type is new Testr.Registerable_Type with null record;
procedure Proc(Dummy : My_Registerable_Type);
end Testrg;
with Ada.Text_Io;
package body Testrg is
Jump_Vector: aliased My_Registerable_Type;
procedure Proc(Dummy : My_Registerable_Type) is
begin
Ada.Text_Io.Put_Line(T'Image(T'First));
end Proc;
procedure Register_It is
begin
Testr.Register(Jump_Vector'unchecked_access);
end Register_It;
end Testrg;
with Testrg;
package Testr1 is
package A is new Testrg(Positive);
package B is new Testrg(Boolean);
end Testr1;
with Testr1, Testr;
with Ada.Text_Io;
procedure Testr2 is
begin
Testr1.A.Register_It;
Testr1.B.Register_It;
Testr.Call(1); -- first registeree was A, type T was Positive
Testr.Call(2); -- second registeree was B, type T was Boolean
end Testr2;
next prev parent reply other threads:[~2003-04-17 5:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-04-17 1:53 Subprogram Pointer in a Generic Charles H. Sampson
2003-04-17 2:54 ` James S. Rogers
2003-04-17 3:15 ` Samuel Tardieu
2003-04-17 5:27 ` tmoran [this message]
2003-04-17 3:12 ` Samuel Tardieu
2003-04-17 4:17 ` tmoran
2003-04-17 20:24 ` Robert A Duff
2003-04-18 2:59 ` Charles H. Sampson
2003-04-22 16:34 ` Warren W. Gay VE3WWG
2003-04-22 21:17 ` Robert A Duff
2003-04-23 20:46 ` Warren W. Gay VE3WWG
2003-04-17 19:41 ` Matthew Heaney
2003-04-17 20:39 ` Robert A Duff
2003-04-17 23:14 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox