comp.lang.ada
 help / color / mirror / Atom feed
From: eachus@spectre.mitre.org (Robert I. Eachus)
Subject: Re: "Classes" as packages in Ada
Date: 1998/11/25
Date: 1998-11-25T00:00:00+00:00	[thread overview]
Message-ID: <EACHUS.98Nov25182652@spectre.mitre.org> (raw)
In-Reply-To: 3UU62.124$8X3.638914@news.rdc1.az.home.com

In article <3UU62.124$8X3.638914@news.rdc1.az.home.com> "John Goodsen" <jgoodsen@radsoft.com> writes:

  > So my conclusion is that Ada code for an interface is not even
  > close in simplicity (from the programmer's point of view) as
  > something like...

   About all I can say, is you are right, and you are wrong.  As Ed
pointed out, most of what requires interfaces in other languages is
done with generic mix-ins in Ada 95.  I'd say you were comparing
apples and oranges, but it is more like comparing Waldorf salad and
apples.  I have a package (actually family of packages) that is simple
to use for event notification which not only allows for all the bells
and whistles Ed showed, but a few more besides.  (You can have
multiple delivery methods for the same class of messages, it is
closely integrated with Ada tasking, etc., etc.)

   But if I wanted to simply implement the Ada equivalent to John's
classes, I'd probably use an Ada protected object.  (Note to Ada 95
programmers, this is a perfect case for using requeue to insure that
each observer is notified once for each event.)

    protected type Observed_Object is
      procedure Set(Observed: in Object);
      function Value return Object;
      entry Modified(Observed: out Object);
    private
      entry Waiting(Observed: out Object);
      Current: Object;
      Reporting: Boolean := False;
    end Observed_Object;
       
    protected body Observed_Object is
      procedure Set (Observed: in Object) is
      begin
        Current := Observed;
        Reporting := True;
      end Set;

      function Value return Object is begin return Current; end Value;

      entry Modiified(Observed: out Object) when not Reporting is  
      begin requeue Waiting; end;

      entry Waiting(Observed: out Object) when Reporting is
      begin
        Observed := Current;
        if Waiting'Count = 0 then Reporting := False; end if;
      end; 

    end Observed_Object;

    Now I probably could spend pages describing why I made certain
choices in this version, and how to modify it to deal with other
cases, but I'll refrain.  (Knowing the list though, I'm sure there
will be a dozen variations posted by Monday ;-)  Some other
possibilities include a class of events, allowing callers to wait for
more than one event, using access types to allow the object value to
be limited, etc.

    I'll just say that this implementation deals naturally with
concurrency, the protected declaration is of a size with John's
interface, and the implementation is almost trivial.

    For those that don't know Ada too well, and this is a new and not
to well explored corner of Ada 95, here is how it works.  A thread
wanting to be notified of the next event calls Modified usually in a
loop:

    while Something loop
      Some_Object.Modified(Value);
      Do_Something_with(Value;
    end loop;

    If the protected object is not currently reporting to queued
callers, the call is immediately requeued on Waiting.  When someone
modifies the current value, Reporting is set to true, all the tasks
currently waiting on Waiting are given the new value to do with as
they will, then any tasks that have already called Modified get moved
to Waiting.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...




  reply	other threads:[~1998-11-25  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-24  0:00 "Classes" as packages in Ada Samuel Mize
1998-11-24  0:00 ` John Goodsen
1998-11-25  0:00   ` Ed Falis
1998-11-25  0:00     ` John Goodsen
1998-11-25  0:00       ` Robert I. Eachus [this message]
1998-11-28  0:00         ` Brian Rogoff
1998-11-30  0:00         ` Robert I. Eachus
1999-03-28  0:00         ` Matthew Heaney
1998-11-27  0:00       ` Larry Kilgallen
1999-03-28  0:00       ` Matthew Heaney
replies disabled

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