de.sciss.lucre.synth.State.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lucresynth_2.11 Show documentation
Show all versions of lucresynth_2.11 Show documentation
Transactional extension for ScalaCollider
/*
* State.scala
* (SoundProcesses)
*
* Copyright (c) 2010-2019 Hanns Holger Rutz. All rights reserved.
*
* This software is published under the GNU Lesser General Public License v2.1+
*
*
* For further information, please contact Hanns Holger Rutz at
* [email protected]
*/
package de.sciss.lucre.synth
import scala.concurrent.stm.Ref
object State {
def apply(owner: Any, name: String, init: Boolean): State = new Impl(owner, name, init)
private final class Impl(val owner: Any, val name: String, init: Boolean) extends State {
val value = Ref(init)
def swap(newValue: Boolean)(implicit tx: Txn): Boolean = value.swap(newValue)(tx.peer)
def get(implicit tx: Txn): Boolean = value.get(tx.peer)
}
}
sealed trait State {
protected def value: Ref[Boolean]
def swap(newValue: Boolean)(implicit tx: Txn): Boolean
def get(implicit tx: Txn): Boolean
final def set(newValue: Boolean)(implicit tx: Txn): Unit =
value.set(newValue)(tx.peer)
protected def owner: Any
def name: String
override def toString = s"<$owner $name>"
}