unions, events and errors... oh my

This commit is contained in:
Andrew Gallant (Ocelot)
2012-05-01 01:08:03 -04:00
parent 73154769b3
commit 83a71d4648
8 changed files with 317 additions and 38 deletions

View File

@@ -22,18 +22,36 @@ func (e *ErrorCopy) Define(c *Context) {
c.Putln("")
c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType())
c.Putln("")
c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType())
c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName())
c.Putln("}")
c.Putln("")
// Read defines a function that transforms a byte slice into this
// error struct.
e.Read(c)
// Write defines a function that transoforms this error struct into
// a byte slice.
e.Write(c)
// Makes sure that this error type is an Error interface.
c.Putln("func (err %s) ImplementsError() { }", e.ErrType())
c.Putln("")
c.Putln("func (err %s) Bytes() []byte {", e.ErrType())
c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType())
c.Putln("}")
c.Putln("")
// Let's the XGB know how to read this error.
c.Putln("func init() {")
c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName())
c.Putln("}")
c.Putln("")
}
func (e *ErrorCopy) Read(c *Context) {
c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType())
c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName())
c.Putln("}")
c.Putln("")
}
func (e *ErrorCopy) Write(c *Context) {
c.Putln("func (err %s) Bytes() []byte {", e.ErrType())
c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType())
c.Putln("}")
c.Putln("")
}