2012-04-30 22:18:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Union types
|
|
|
|
func (u *Union) Define(c *Context) {
|
|
|
|
c.Putln("// Union definition %s", u.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("// Note that to *create* a Union, you should *never* create")
|
|
|
|
c.Putln("// this struct directly (unless you know what you're doing).")
|
|
|
|
c.Putln("// Instead use one of the following constructors for '%s':",
|
|
|
|
u.SrcName())
|
|
|
|
for _, field := range u.Fields {
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("// %s%sNew(%s %s) %s", u.SrcName(), field.SrcName(),
|
2012-05-01 07:08:03 +02:00
|
|
|
field.SrcName(), field.SrcType(), u.SrcName())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Putln("type %s struct {", u.SrcName())
|
|
|
|
for _, field := range u.Fields {
|
|
|
|
field.Define(c)
|
|
|
|
}
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
|
|
|
|
// Write functions for each field that create instances of this
|
|
|
|
// union using the corresponding field.
|
|
|
|
u.New(c)
|
|
|
|
|
|
|
|
// Write function that reads bytes and produces this union.
|
|
|
|
u.Read(c)
|
|
|
|
|
|
|
|
// Write function that reads bytes and produces a list of this union.
|
|
|
|
u.ReadList(c)
|
|
|
|
|
|
|
|
// Write function that writes bytes given this union.
|
|
|
|
u.Write(c)
|
|
|
|
|
|
|
|
// Write function that writes a list of this union.
|
|
|
|
u.WriteList(c)
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-01 07:08:03 +02:00
|
|
|
func (u *Union) New(c *Context) {
|
|
|
|
for _, field := range u.Fields {
|
|
|
|
c.Putln("// Union constructor for %s for field %s.",
|
|
|
|
u.SrcName(), field.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("func %s%sNew(%s %s) %s {",
|
2012-05-01 07:08:03 +02:00
|
|
|
u.SrcName(), field.SrcName(), field.SrcName(),
|
|
|
|
field.SrcType(), u.SrcName())
|
|
|
|
c.Putln("var b int")
|
|
|
|
c.Putln("buf := make([]byte, %s)", u.Size())
|
|
|
|
c.Putln("")
|
2012-05-03 07:00:01 +02:00
|
|
|
field.Write(c, "")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("")
|
|
|
|
c.Putln("// Create the Union type")
|
|
|
|
c.Putln("v := %s{}", u.SrcName())
|
|
|
|
c.Putln("")
|
|
|
|
c.Putln("// Now copy buf into all fields")
|
|
|
|
c.Putln("")
|
|
|
|
for _, field2 := range u.Fields {
|
|
|
|
c.Putln("b = 0 // always read the same bytes")
|
2012-05-03 07:00:01 +02:00
|
|
|
field2.Read(c, "v.")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
c.Putln("return v")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Union) Read(c *Context) {
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("// Union read %s", u.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("func %sRead(buf []byte, v *%s) int {", u.SrcName(), u.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("var b int")
|
|
|
|
c.Putln("")
|
|
|
|
for _, field := range u.Fields {
|
|
|
|
c.Putln("b = 0 // re-read the same bytes")
|
2012-05-03 07:00:01 +02:00
|
|
|
field.Read(c, "v.")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
c.Putln("return %s", u.Size())
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Union) ReadList(c *Context) {
|
|
|
|
c.Putln("// Union list read %s", u.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("func %sReadList(buf []byte, dest []%s) int {",
|
2012-05-01 07:08:03 +02:00
|
|
|
u.SrcName(), u.SrcName())
|
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("for i := 0; i < len(dest); i++ {")
|
|
|
|
c.Putln("dest[i] = %s{}", u.SrcName())
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("b += %sRead(buf[b:], &dest[i])", u.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("return xgb.Pad(b)")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-01 07:08:03 +02:00
|
|
|
// This is a bit tricky since writing from a Union implies that only
|
|
|
|
// the data inside ONE of the elements is actually written.
|
|
|
|
// However, we only currently support unions where every field has the
|
|
|
|
// *same* *fixed* size. Thus, we make sure to always read bytes into
|
|
|
|
// every field which allows us to simply pick the first field and write it.
|
|
|
|
func (u *Union) Write(c *Context) {
|
2012-04-30 22:18:17 +02:00
|
|
|
c.Putln("// Union write %s", u.SrcName())
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("// Each field in a union must contain the same data.")
|
|
|
|
c.Putln("// So simply pick the first field and write that to the wire.")
|
|
|
|
c.Putln("func (v %s) Bytes() []byte {", u.SrcName())
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("buf := make([]byte, %s)", u.Size().Reduce("v."))
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("")
|
2012-05-03 07:00:01 +02:00
|
|
|
u.Fields[0].Write(c, "v.")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("return buf")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Union) WriteList(c *Context) {
|
|
|
|
c.Putln("// Union list write %s", u.SrcName())
|
|
|
|
c.Putln("func %sListBytes(buf []byte, list []%s) int {",
|
|
|
|
u.SrcName(), u.SrcName())
|
|
|
|
c.Putln("b := 0")
|
|
|
|
c.Putln("var unionBytes []byte")
|
|
|
|
c.Putln("for _, item := range list {")
|
|
|
|
c.Putln("unionBytes = item.Bytes()")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("copy(buf[b:], unionBytes)")
|
2012-05-10 23:01:42 +02:00
|
|
|
c.Putln("b += xgb.Pad(len(unionBytes))")
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("return b")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Union) WriteListSize(c *Context) {
|
|
|
|
c.Putln("// Union list size %s", u.SrcName())
|
|
|
|
c.Putln("func %sListSize(list []%s) int {", u.SrcName(), u.SrcName())
|
|
|
|
c.Putln("size := 0")
|
|
|
|
c.Putln("for _, item := range list {")
|
2012-05-03 07:00:01 +02:00
|
|
|
c.Putln("size += %s", u.Size().Reduce("item."))
|
2012-05-01 07:08:03 +02:00
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("return size")
|
|
|
|
c.Putln("}")
|
|
|
|
c.Putln("")
|
2012-04-30 22:18:17 +02:00
|
|
|
}
|