aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/go_single_field.go
blob: 3c354e7d3bd401e9e8435530f2100560e0ba507d (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
package main

import (
	"fmt"
	"log"
)

func (f *SingleField) Define(c *Context) {
	c.Putln("%s %s", f.SrcName(), f.Type.SrcName())
}

func ReadSimpleSingleField(c *Context, name string, typ Type) {
	switch t := typ.(type) {
	case *Resource:
		c.Putln("%s = get32(buf[b:])", name)
	case *TypeDef:
		switch t.Size().Eval() {
		case 1:
			c.Putln("%s = %s(buf[b])", name, t.SrcName())
		case 2:
			c.Putln("%s = %s(get16(buf[b:]))", name, t.SrcName())
		case 4:
			c.Putln("%s = %s(get32(buf[b:]))", name, t.SrcName())
		case 8:
			c.Putln("%s = %s(get64(buf[b:]))", name, t.SrcName())
		}
	case *Base:
		var val string
		switch t.Size().Eval() {
		case 1:
			val = fmt.Sprintf("buf[b]")
		case 2:
			val = fmt.Sprintf("get16(buf[b:])")
		case 4:
			val = fmt.Sprintf("get32(buf[b:])")
		case 8:
			val = fmt.Sprintf("get64(buf[b:])")
		}

		// We need to convert base types if they aren't uintXX or byte
		ty := t.SrcName()
		if ty != "byte" && ty != "uint16" && ty != "uint32" && ty != "uint64" {
			val = fmt.Sprintf("%s(%s)", ty, val)
		}
		c.Putln("%s = %s", name, val)
	default:
		log.Fatalf("Cannot read field '%s' as a simple field with %T type.",
			name, typ)
	}

	c.Putln("b += %s", typ.Size())
}

func (f *SingleField) Read(c *Context) {
	switch t := f.Type.(type) {
	case *Resource:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *TypeDef:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Base:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Struct:
		c.Putln("v.%s = %s{}", f.SrcName(), t.SrcName())
		c.Putln("b += Read%s(buf[b:], &v.%s)", t.SrcName(), f.SrcName())
		c.Putln("")
	default:
		log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
	}
}

func WriteSimpleSingleField(c *Context, name string, typ Type) {
	switch t := typ.(type) {
	case *Resource:
		c.Putln("put32(buf[b:], uint32(%s))", name)
	case *TypeDef:
		switch t.Size().Eval() {
		case 1:
			c.Putln("buf[b] = byte(%s)", name)
		case 2:
			c.Putln("put16(buf[b:], uint16(%s))", name)
		case 4:
			c.Putln("put32(buf[b:], uint32(%s))", name)
		case 8:
			c.Putln("put64(buf[b:], uint64(%s))", name)
		}
	case *Base:
		switch t.Size().Eval() {
		case 1:
			if t.SrcName() != "byte" {
				c.Putln("buf[b] = byte(%s)", name)
			} else {
				c.Putln("buf[b] = %s", name)
			}
		case 2:
			if t.SrcName() != "uint16" {
				c.Putln("put16(buf[b:], uint16(%s))", name)
			} else {
				c.Putln("put16(buf[b:], %s)", name)
			}
		case 4:
			if t.SrcName() != "uint32" {
				c.Putln("put32(buf[b:], uint32(%s))", name)
			} else {
				c.Putln("put32(buf[b:], %s)", name)
			}
		case 8:
			if t.SrcName() != "uint64" {
				c.Putln("put64(buf[b:], uint64(%s))", name)
			} else {
				c.Putln("put64(buf[b:], %s)", name)
			}
		}
	default:
		log.Fatalf("Cannot read field '%s' as a simple field with %T type.",
			name, typ)
	}

	c.Putln("b += %s", typ.Size())
}

func (f *SingleField) Write(c *Context) {
	switch t := f.Type.(type) {
	case *Resource:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *TypeDef:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Base:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Struct:
		c.Putln("{")
		c.Putln("structBytes := v.%s.Bytes()", f.SrcName())
		c.Putln("copy(buf[b:], structBytes)")
		c.Putln("b += len(structBytes)")
		c.Putln("}")
	default:
		log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
	}
}