aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/go_union.go
blob: 3b7365d0da64109dc2a22f5b6dee5cd14ef9b29f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main

// Union types
func (u *Union) Define(c *Context) {
	c.Putln("// Union definition %s", u.SrcName())
	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 {
		c.Putln("//     New%s%s(%s %s) %s", u.SrcName(), field.SrcName(),
			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)

	// Write function that computes the size of a list of these unions.
	u.WriteListSize(c)
}

func (u *Union) New(c *Context) {
	for _, field := range u.Fields {
		c.Putln("// Union constructor for %s for field %s.",
			u.SrcName(), field.SrcName())
		c.Putln("func New%s%s(%s %s) %s {",
			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("")
		field.Write(c)
		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")
			field2.Read(c)
			c.Putln("")
		}
		c.Putln("return v")
		c.Putln("}")
		c.Putln("")
	}
}

func (u *Union) Read(c *Context) {
	c.Putln("// Union read %s", u.SrcName())
	c.Putln("func Read%s(buf []byte, v *%s) int {", u.SrcName(), u.SrcName())
	c.Putln("var b int")
	c.Putln("")
	for _, field := range u.Fields {
		c.Putln("b = 0 // re-read the same bytes")
		field.Read(c)
		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())
	c.Putln("func Read%sList(buf []byte, dest []%s) int {",
		u.SrcName(), u.SrcName())
	c.Putln("b := 0")
	c.Putln("for i := 0; i < len(dest); i++ {")
	c.Putln("dest[i] = %s{}", u.SrcName())
	c.Putln("b += Read%s(buf[b:], &dest[i])", u.SrcName())
	c.Putln("}")
	c.Putln("return pad(b)")
	c.Putln("}")
	c.Putln("")
}

// 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) {
	c.Putln("// Union write %s", u.SrcName())
	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())
	c.Putln("buf := make([]byte, %s)", u.Size().Reduce("v.", ""))
	c.Putln("b := 0")
	c.Putln("")
	u.Fields[0].Write(c)
	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()")
	c.Putln("copy(buf[b:], len(unionBytes))")
	c.Putln("b += pad(len(unionBytes))")
	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 {")
	c.Putln("size += %s", u.Size().Reduce("item.", ""))
	c.Putln("}")
	c.Putln("return size")
	c.Putln("}")
	c.Putln("")
}