All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.sciss.lucre.impl.ElemImpl.scala Maven / Gradle / Ivy

Go to download

Extension of Scala-STM, adding optional durability layer, and providing API for confluent and reactive event layers

The newest version!
/*
 *  ElemImpl.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 ElemImpl {
  def read[T <: Txn[T]](in: DataInput)(implicit tx: T): Elem[T] = {
    val typeId  = in.readInt()
    val tpe     = getType(typeId)
    tpe.readIdentifiedObj(in)
  }

  implicit def format[T <: Txn[T]]: TFormat[T, Elem[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.empty[Int, Elem.Type]
  @volatile private var map = Map[Int, Elem.Type](MapObj.typeId -> MapObj)

  def addType(tpe: Elem.Type): Unit = sync.synchronized {
    val typeId = tpe.typeId
    if (map.contains(typeId))
      throw new IllegalArgumentException(
        s"Element type $typeId (0x${typeId.toHexString}) was already registered ($tpe overrides ${map(typeId)})")

    map += typeId -> tpe
  }

  @inline
  def getType(id: Int): Elem.Type = map.getOrElse(id, sys.error(s"Unknown element type $id (0x${id.toHexString})"))

  private final class Fmt[T <: Txn[T]] extends CastTxnFormat[T, Elem] {
    override def readT(in: DataInput)(implicit tx: T): Elem[T] = ElemImpl.read(in)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy