src/Pure/System/event_bus.scala
author wenzelm
Wed, 08 Aug 2012 12:33:40 +0200
changeset 48731 a45ba78abcc1
parent 45673 cd41e3903fbf
child 55618 995162143ef4
permissions -rw-r--r--
more casual exit back to ML toplevel, to accomodate commit in SML/NJ which continues at the saved point;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38428
c13c95c97e89 event_bus.scala rather belongs to system plumbing;
wenzelm
parents: 36676
diff changeset
     1
/*  Title:      Pure/System/event_bus.scala
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     4
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
     5
Generic event bus with multiple receiving actors.
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     6
*/
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     7
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     8
package isabelle
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
     9
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    10
import scala.actors.Actor, Actor._
29191
de56edf88514 added methods "+" and "-";
wenzelm
parents: 29190
diff changeset
    11
import scala.collection.mutable.ListBuffer
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
    12
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
    13
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    14
class Event_Bus[Event]
29200
787ba47201c7 optional exception logging;
wenzelm
parents: 29191
diff changeset
    15
{
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    16
  /* receivers */
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    17
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    18
  private val receivers = new ListBuffer[Actor]
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    19
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    20
  def += (r: Actor) { synchronized { receivers += r } }
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    21
  def + (r: Actor): Event_Bus[Event] = { this += r; this }
29200
787ba47201c7 optional exception logging;
wenzelm
parents: 29191
diff changeset
    22
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    23
  def += (f: Event => Unit) {
43406
40c67d894be4 avoid compiler warning -- this is unchecked anyway;
wenzelm
parents: 38849
diff changeset
    24
    this += actor { loop { react { case x => f(x.asInstanceOf[Event]) } } }
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    25
  }
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
    26
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    27
  def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
29191
de56edf88514 added methods "+" and "-";
wenzelm
parents: 29190
diff changeset
    28
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    29
  def -= (r: Actor) { synchronized { receivers -= r } }
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    30
  def - (r: Actor) = { this -= r; this }
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
    31
29200
787ba47201c7 optional exception logging;
wenzelm
parents: 29191
diff changeset
    32
787ba47201c7 optional exception logging;
wenzelm
parents: 29191
diff changeset
    33
  /* event invocation */
787ba47201c7 optional exception logging;
wenzelm
parents: 29191
diff changeset
    34
32539
668052c4220e modernized Event_Bus -- based on actors;
wenzelm
parents: 29200
diff changeset
    35
  def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
29190
89217ccfd130 Generic event bus.
wenzelm
parents:
diff changeset
    36
}