Skip to content

Commit

Permalink
Use named wireformats, instead of anonymous objects
Browse files Browse the repository at this point in the history
  • Loading branch information
espringe committed Sep 18, 2012
1 parent 03e77ff commit 35d39af
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
44 changes: 29 additions & 15 deletions project/GenWireFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait GeneratedWireFormats {
}

def gen_section_1(maxargs: Int): String =
((2 to maxargs).map(gen_mkCaseWireFormat _) mkString "\n") +
((2 to maxargs).map(gen_mkCaseWireFormat _) mkString "\n") + "\n" +
((2 to maxargs).map(gen_mkAbstractWireFormat _) mkString "\n")

def gen_section_2(maxargs: Int): String =
Expand Down Expand Up @@ -60,11 +60,14 @@ trait GeneratedWireFormats {
}
""" format (gen_get_args, gen_raw_args_lc)
}
""" def mkCaseWireFormat[T, %s](apply: (%s) => T, unapply: T => Option[(%s)]): WireFormat[T] = new WireFormat[T] {
""" class Case%dWireFormat[T, %s](val apply: (%s) => T, val unapply: T => Option[(%s)]) extends WireFormat[T] {
%s
%s
%s }
""" format (gen_typed_args, gen_raw_args, gen_raw_args,
gen_toWire, gen_fromWire)
}
def mkCaseWireFormat[T, %s](apply: (%s) => T, unapply: T => Option[(%s)]): WireFormat[T] = new Case%dWireFormat(apply, unapply)
""" format (numargs, gen_typed_args, gen_raw_args, gen_raw_args,
gen_toWire, gen_fromWire,
gen_typed_args, gen_raw_args, gen_raw_args, numargs)
}

def gen_mkAbstractWireFormat(numargs: Int) = {
Expand All @@ -80,6 +83,7 @@ trait GeneratedWireFormats {
(1 to numargs).map(
x => fmt format (chr(x), chr(x), chr(x), chr(x))) mkString join
def gen_typed_args = gen("%C <: TT : Manifest : WireFormat", ", ")
def gen_call_types = gen("%C", ", ")
def gen_toWire = {
def gen_if_else = gen4(
"""if (clazz == implicitly[Manifest[%c]].erasure) {
Expand All @@ -104,11 +108,13 @@ trait GeneratedWireFormats {
}
""" format gen_cases
}
""" def mkAbstractWireFormat[TT, %s]() = new WireFormat[TT] {
""" class Abstract%dWireFormat[TT, %s]() extends WireFormat[TT] {
%s
%s }
""" format (gen_typed_args, gen_toWire, gen_fromWire)
def mkAbstractWireFormat[TT, %s]() = new Abstract%dWireFormat[TT, %s]()
""" format (numargs, gen_typed_args, gen_toWire, gen_fromWire,
gen_typed_args, numargs, gen_call_types)
}

def gen_mkTupleFmt(numargs: Int) = {
Expand All @@ -124,24 +130,32 @@ trait GeneratedWireFormats {
(1 to numargs).map("%c" format lc_chr(_)) mkString ", "
def gen_TupleFmt_args = gen2(
"wt%d: WireFormat[T%d]", ", ")
def gen_ClassFmt_args = gen2(
"val wt%d: WireFormat[T%d]", ", ")
def gen_calling_args = gen2(
"wt%d", ", ")
def gen_toWire_guts = gen2(
" wt%d.toWire(x._%d, out)", "\n")
def gen_fromWire_guts =
(1 to numargs).map(x =>
" val %c = wt%d.fromWire(in)" format (lc_chr(x), x)) mkString "\n"
""" implicit def Tuple%dFmt[%s]
(implicit %s) =
new WireFormat[(%s)] {
""" class Tuple%dWireFormat[%s](%s) extends WireFormat[(%s)]
{
def toWire(x: (%s), out: DataOutput) {
%s
}
def fromWire(in: DataInput): (%s) = {
%s
(%s)
}
}
""" format (numargs, gen_raw_args, gen_TupleFmt_args, gen_raw_args,
gen_raw_args, gen_toWire_guts,
gen_raw_args, gen_fromWire_guts, gen_raw_args_lc_chr)
}
implicit def Tuple%dFmt[%s](implicit %s): WireFormat[(%s)] = new Tuple%dWireFormat(%s)
""" format (numargs, gen_raw_args, gen_ClassFmt_args, gen_raw_args,
gen_raw_args,
gen_toWire_guts,
gen_raw_args,
gen_fromWire_guts,
gen_raw_args_lc_chr,
numargs, gen_raw_args, gen_TupleFmt_args, gen_raw_args, numargs, gen_calling_args)
}
}
11 changes: 7 additions & 4 deletions src/main/scala/com/nicta/scoobi/core/WireFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,32 @@ object WireFormat extends WireFormatImplicits {
def fromWire(in: DataInput) = g(wf.fromWire(in))
def toWire(x: B, out: DataOutput) { wf.toWire(f(x), out) }
}

}
}

/** Implicit definitions of WireFormat instances for common types. */
trait WireFormatImplicits extends codegen.GeneratedWireFormats {

def mkObjectWireFormat[T](x: T) = new WireFormat[T] {
class ObjectWireFormat[T](val x: T) extends WireFormat[T] {
override def toWire(obj: T, out: DataOutput) {}
override def fromWire(in: DataInput): T = x
}
def mkObjectWireFormat[T](x: T): WireFormat[T] = new ObjectWireFormat(x)

def mkCaseWireFormat[T](apply: () => T, unapply: T => Boolean): WireFormat[T] = new WireFormat[T] {
class Case0WireFormat[T](val apply: () => T, val unapply: T => Boolean) extends WireFormat[T] {
override def toWire(obj: T, out: DataOutput) {}
override def fromWire(in: DataInput): T = apply()
}

def mkCaseWireFormat[T, A1: WireFormat](apply: (A1) => T, unapply: T => Option[(A1)]): WireFormat[T] = new WireFormat[T] {
def mkCaseWireFormat[T](apply: () => T, unapply: T => Boolean): WireFormat[T] = new Case0WireFormat(apply, unapply)

class Case1WireFormat[T, A1: WireFormat](apply: (A1) => T, unapply: T => Option[(A1)]) extends WireFormat[T] {
override def toWire(obj: T, out: DataOutput) {
implicitly[WireFormat[A1]].toWire(unapply(obj).get, out)
}
override def fromWire(in: DataInput): T = apply(implicitly[WireFormat[A1]].fromWire(in))
}
def mkCaseWireFormat[T, A1: WireFormat](apply: (A1) => T, unapply: T => Option[(A1)]): WireFormat[T] = new Case1WireFormat(apply, unapply)

/**
* Catch-all implementation of a WireFormat for a type T, using Java serialization.
Expand Down

0 comments on commit 35d39af

Please sign in to comment.