diff options
author | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-02 01:46:30 -0400 |
---|---|---|
committer | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-02 01:46:30 -0400 |
commit | 39507f86ab0468292c24081a80f41c1799d6b477 (patch) | |
tree | 002668a66ec90c71b3bbd05141b378d0b2218d58 /nexgb/xgbgen/go_error.go | |
parent | f48b6fafc6d96527a554876e13fc5a0b548005af (diff) | |
download | haven-39507f86ab0468292c24081a80f41c1799d6b477.tar.gz haven-39507f86ab0468292c24081a80f41c1799d6b477.tar.xz haven-39507f86ab0468292c24081a80f41c1799d6b477.zip |
finally starting on the crescendo: requests and replies.
Diffstat (limited to 'nexgb/xgbgen/go_error.go')
-rw-r--r-- | nexgb/xgbgen/go_error.go | 65 |
1 files changed, 46 insertions, 19 deletions
diff --git a/nexgb/xgbgen/go_error.go b/nexgb/xgbgen/go_error.go index 493a8b9..17c0db5 100644 --- a/nexgb/xgbgen/go_error.go +++ b/nexgb/xgbgen/go_error.go @@ -3,15 +3,53 @@ package main // Error types func (e *Error) Define(c *Context) { c.Putln("// Error definition %s (%d)", e.SrcName(), e.Number) + c.Putln("// Size: %s", e.Size()) + c.Putln("") + c.Putln("const %s = %d", e.ErrConst(), e.Number) + c.Putln("") + c.Putln("type %s struct {", e.ErrType()) + c.Putln("Sequence uint16") + c.Putln("NiceName string") + for _, field := range e.Fields { + field.Define(c) + } + c.Putln("}") c.Putln("") -} -func (e *Error) Read(c *Context, prefix string) { - c.Putln("// Error read %s", e.SrcName()) + // Read defines a function that transforms a byte slice into this + // error struct. + e.Read(c) + + // Makes sure that this error type is an Error interface. + c.Putln("func (v %s) ImplementsError() { }", e.ErrType()) + c.Putln("") + + // Let's the XGB event loop read this error. + c.Putln("func init() {") + c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.ErrType()) + c.Putln("}") + c.Putln("") } -func (e *Error) Write(c *Context, prefix string) { - c.Putln("// Error write %s", e.SrcName()) +func (e *Error) Read(c *Context) { + c.Putln("// Error read %s", e.SrcName()) + c.Putln("func New%s(buf []byte) %s {", e.ErrType(), e.ErrType()) + c.Putln("v := %s{}", e.ErrType()) + c.Putln("v.NiceName = \"%s\"", e.SrcName()) + c.Putln("") + c.Putln("b := 1 // skip error determinant") + c.Putln("b += 1 // don't read error number") + c.Putln("") + c.Putln("v.Sequence = get16(buf[b:])") + c.Putln("b += 2") + c.Putln("") + for _, field := range e.Fields { + field.Read(c) + c.Putln("") + } + c.Putln("return v") + c.Putln("}") + c.Putln("") } // ErrorCopy types @@ -27,31 +65,20 @@ func (e *ErrorCopy) Define(c *Context) { // 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("") // 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("newErrorFuncs[%d] = New%s", e.Number, e.ErrType()) 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("func New%s(buf []byte) %s {", e.ErrType(), e.ErrType()) + c.Putln("return %s(New%s(buf))", e.ErrType(), e.Old.(*Error).ErrType()) c.Putln("}") c.Putln("") } |