summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/Signallable.scala
blob: 2979e196e37fe289d8f6f77867cf40f07adc6d38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* NSC -- new Scala compiler
 * Copyright 2005-2011 LAMP/EPFL
 * @author Paul Phillips
 */

package scala.tools
package util

/** A class for things which are signallable.
 */
abstract class Signallable[T] private (val signal: String, val description: String) {
  private var last: Option[T] = None
  private def lastString = last filterNot (_ == ()) map (_.toString) getOrElse ""

  /** The most recent result from the signal handler. */
  def lastResult: Option[T] = last

  /** Method to be executed when the associated signal is received. */
  def onSignal(): T

  // todo:
  // def unregister(): Boolean

  override def toString =  "  SIG(%s) => %s%s".format(
    signal, description, if (lastString == "") "" else " (" + lastString + ")"
  )
}

object Signallable {
  /** Same as the other apply, but an open signal is found for you.
   */
  def apply[T](description: String)(body: => T): Signallable[T] =
    apply(SignalManager.findOpenSignal().name, description)(body)

  /** Given a signal name, a description, and a handler body, this
   *  registers a signal handler and returns the Signallable instance.
   *  The signal handler registry is thereafter available by calling
   *  SignalManager.info(), or sending SIGINFO to the manager will
   *  dump it to console.
   */
  def apply[T](signal: String, description: String)(body: => T): Signallable[T] = {
    val result = new Signallable[T](signal, description) {
      def onSignal(): T = {
        val result = body
        last = Some(result)
        result
      }
    }
    SignalManager.public(signal, description)(result.onSignal())
    result
  }
}