comp.lang.ada
 help / color / mirror / Atom feed
* associate functions to objects
@ 1992-04-14  5:21 elroy.jpl.nasa.gov!usc!rpi!sarah!newserve!bingsuns.cc.binghamton.edu!cons
  0 siblings, 0 replies; 3+ messages in thread
From: elroy.jpl.nasa.gov!usc!rpi!sarah!newserve!bingsuns.cc.binghamton.edu!cons @ 1992-04-14  5:21 UTC (permalink / raw)


I'm working on a project for a Software Engineering class, and we're using Ada 
to
develop a simple database management application. My part is on building the us
er
interface for the app. One of the data type that we're using is called an
EntryBoard which is supposed to manage a list of other objects like Fields,
Buttons, Titles, etc. Each of those objects should be attached to a function th
at
will be called whenever that object is selected (sort of like callback
functions). Now, how do you attach functions to objects? I consulted my Ada
references and it seems that Ada doesn't allow passing functions as a parameter
to a procedure (the way I did it in C++ to implement the same behavior). I need
hints and advice and examples on how to do this kind of things. Will anybody he
lp
me with some light? 

-Julian Insan Kamil
Computer Consultant at SUNY-Binghamton
email: consp10@bingsunb.cc.binghamton.edu
email: consp10@bingvaxu.cc.binghamton.edu

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: associate functions to objects
@ 1992-04-14 22:53 Robert I. Eachus
  0 siblings, 0 replies; 3+ messages in thread
From: Robert I. Eachus @ 1992-04-14 22:53 UTC (permalink / raw)


   Julian Insan Kamil (consp10@bingsunb.cc.binghamton.edu)says:

 > I'm working on a project for a Software Engineering class, and
 > we're using Ada to develop a simple database management
 > application. My part is on building the user interface for the app.
 > One of the data type that we're using is called an EntryBoard which
 > is supposed to manage a list of other objects like Fields, Buttons,
 > Titles, etc. Each of those objects should be attached to a function
 > that will be called whenever that object is selected (sort of like
 > callback functions). Now, how do you attach functions to objects? I
 > consulted my Ada references and it seems that Ada doesn't allow
 > passing functions as a parameter to a procedure (the way I did it
 > in C++ to implement the same behavior). I need hints and advice and
 > examples on how to do this kind of things. Will anybody help me
 > with some light?

   Three answers, one simple, one more complex, and one which is just
hard work... 

   The "canonical" way to do this in Ada is to make the objects
instances of a task type.  Task objects and/or objects containing
tasks can be passed as parameters, and calls made to entries of these
tasks.  Problem is that with most current Ada implementations, this is
SLOW.

   The "clever" way to do this, which works on all Ada compilers which
I've tried it on is to pass addresses as parameters and use them in
address clauses for procedures:

    procedure App(X: Something; Callback: System.Address) is

      function Local_Callback return Status;
      pragma INTERFACE(Ada, Callback);
      for Local_Callback'Address use Callback;

    begin
     
      if Local_Callback = Busy then....


    The language to use in the pragma INTERFACE varies from
implementation to implementation, and if the parameter and result
profile of the function whose address is passed doesn't match the
local declaration, expect fireworks at run-time.  But otherwise this
is an implementation dependant but fairly standard way of doing
callbacks.  (The pragma INTERFACE has to be there for legality
reasons, since no body is provided for Local_Callback.  The address in
the address clause need not be static, and in this case it probably
will change from call to call.)

    The third approach is to use generics.  Define your object types
as generic packages.  Each object is then an instance one of these
packages.  (However, the interaction of abstract types can lead to a
LOT of generic formal parameters.  See my previous message.)  The
callbacks can then be passed as generic actual subprogram parameters.
It is hard to learn this style of programming but it is very powerful.
It can be mixed with abstract types as tasks when some objects need a
separate thread of control, and so forth.

    So choose whichever approach is right for this application.  Good
luck.

--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: associate functions to objects
@ 1992-04-16  7:57 eru.mt.luth.se!lunic!sunic!sunic2!seunet!lin.foa.se!eriks
  0 siblings, 0 replies; 3+ messages in thread
From: eru.mt.luth.se!lunic!sunic!sunic2!seunet!lin.foa.se!eriks @ 1992-04-16  7:57 UTC (permalink / raw)


consp10@bingsunb.cc.binghamton.edu (Julian Insan Kamil) writes:

>I'm working on a project for a Software Engineering class, and we're using Ada
 to
>develop a simple database management application. My part is on building the u
ser
>interface for the app. One of the data type that we're using is called an
>EntryBoard which is supposed to manage a list of other objects like Fields,
>Buttons, Titles, etc. Each of those objects should be attached to a function t
hat
>will be called whenever that object is selected (sort of like callback
>functions). Now, how do you attach functions to objects? I consulted my Ada
>references and it seems that Ada doesn't allow passing functions as a paramete
r
>to a procedure (the way I did it in C++ to implement the same behavior). I nee
d
>hints and advice and examples on how to do this kind of things. Will anybody h
elp
>me with some light? 

If you're going to use generics you can send a procedure or a function as an
argument when you instantiate (sp?) the package. 

(this is from the top of my head, so there might be some errors)
generic
	type Type_1 is (<>);
	type Type_2 is private;
	with procedure Foo(a : in out Type_1); -- Your supplied procedure
package Bar is
	procedure Foo_Bar(Some_Parameter : in Type_2);
end Bar;

then in your package body:

package body Bar is
	
	procedure Foo_Bar(Some_Parameter : in Type_2) is
	begin
		.
		.
		Foo(some_other_parameter);
		.
		.
	end Foo_Bar;
end Bar;

You instantiate this with
	procedure a_procedure(a : Type_2); -- Declare your procedure
	procedure a_procedure(a : Type_2) is -- the body
	begin
		(some code)
	end a_procedure;

	package My_Bar is new Bar(A_Type,another_type,a_procedure);

Simple as that. 
It's in LRM 12.1.3, 12.3.6.

>-Julian Insan Kamil
>Computer Consultant at SUNY-Binghamton
>email: consp10@bingsunb.cc.binghamton.edu
>email: consp10@bingvaxu.cc.binghamton.edu
--
Erik Svensson			Research Officer
Guided Weapons Division		National Defense Research Establishment (FOA)
Stockholm			Sweden

net.address: 	eriks@fenix.lin.foa.se

&& This .sig is protected by SIGinfectant(tm). Look for the &&, a sure sign of
quality .sig protection. &&

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1992-04-16  7:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-04-16  7:57 associate functions to objects eru.mt.luth.se!lunic!sunic!sunic2!seunet!lin.foa.se!eriks
  -- strict thread matches above, loose matches on Subject: below --
1992-04-14 22:53 Robert I. Eachus
1992-04-14  5:21 elroy.jpl.nasa.gov!usc!rpi!sarah!newserve!bingsuns.cc.binghamton.edu!cons

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