de.sciss.lucre.impl.ObjImpl.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lucre-core_3 Show documentation
Show all versions of lucre-core_3 Show documentation
Extension of Scala-STM, adding optional durability layer, and providing API for confluent and reactive event layers
The newest version!
/*
* ObjImpl.scala
* (Lucre 4)
*
* Copyright (c) 2009-2024 Hanns Holger Rutz. All rights reserved.
*
* This software is published under the GNU Affero General Public License v3+
*
*
* For further information, please contact Hanns Holger Rutz at
* [email protected]
*/
package de.sciss.lucre
package impl
import de.sciss.serial.{DataInput, TFormat}
import scala.annotation.meta.field
object ObjImpl {
def read[T <: Txn[T]](in: DataInput)(implicit tx: T): Obj[T] = {
val typeId = in.readInt()
val tpe = getType(typeId)
tpe.readIdentifiedObj(in)
}
implicit def format[T <: Txn[T]]: TFormat[T, Obj[T]] = anyFmt.cast
@field private[this] final val sync = new AnyRef
@field private[this] final val anyFmt = new Fmt[AnyTxn]
@volatile private var map = Map[Int, Obj.Type](MapObj.typeId -> MapObj)
def addType(tpe: Obj.Type): Unit = sync.synchronized {
val typeId = tpe.typeId
if (map.contains(typeId))
throw new IllegalArgumentException(
s"Object type $typeId (0x${typeId.toHexString}) was already registered ($tpe overrides ${map(typeId)})")
map += typeId -> tpe
}
@inline
def getType(id: Int): Obj.Type = map.getOrElse(id, sys.error(s"Unknown object type $id (0x${id.toHexString})"))
private final class Fmt[T <: Txn[T]] extends CastTxnFormat[T, Obj] {
override def readT(in: DataInput)(implicit tx: T): Obj[T] = ObjImpl.read(in)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy