initial commit
[govpp.git] / vendor / github.com / lunixbochs / struc / types.go
1 package struc
2
3 import (
4         "fmt"
5         "reflect"
6 )
7
8 type Type int
9
10 const (
11         Invalid Type = iota
12         Pad
13         Bool
14         Int
15         Int8
16         Uint8
17         Int16
18         Uint16
19         Int32
20         Uint32
21         Int64
22         Uint64
23         Float32
24         Float64
25         String
26         Struct
27         Ptr
28
29         SizeType
30         OffType
31         CustomType
32 )
33
34 func (t Type) Resolve(options *Options) Type {
35         switch t {
36         case OffType:
37                 switch options.PtrSize {
38                 case 8:
39                         return Int8
40                 case 16:
41                         return Int16
42                 case 32:
43                         return Int32
44                 case 64:
45                         return Int64
46                 default:
47                         panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize))
48                 }
49         case SizeType:
50                 switch options.PtrSize {
51                 case 8:
52                         return Uint8
53                 case 16:
54                         return Uint16
55                 case 32:
56                         return Uint32
57                 case 64:
58                         return Uint64
59                 default:
60                         panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize))
61                 }
62         }
63         return t
64 }
65
66 func (t Type) String() string {
67         return typeNames[t]
68 }
69
70 func (t Type) Size() int {
71         switch t {
72         case SizeType, OffType:
73                 panic("Size_t/Off_t types must be converted to another type using options.PtrSize")
74         case Pad, String, Int8, Uint8, Bool:
75                 return 1
76         case Int16, Uint16:
77                 return 2
78         case Int32, Uint32, Float32:
79                 return 4
80         case Int64, Uint64, Float64:
81                 return 8
82         default:
83                 panic("Cannot resolve size of type:" + t.String())
84         }
85 }
86
87 var typeLookup = map[string]Type{
88         "pad":     Pad,
89         "bool":    Bool,
90         "byte":    Uint8,
91         "int8":    Int8,
92         "uint8":   Uint8,
93         "int16":   Int16,
94         "uint16":  Uint16,
95         "int32":   Int32,
96         "uint32":  Uint32,
97         "int64":   Int64,
98         "uint64":  Uint64,
99         "float32": Float32,
100         "float64": Float64,
101
102         "size_t": SizeType,
103         "off_t":  OffType,
104 }
105
106 var typeNames = map[Type]string{
107         CustomType: "Custom",
108 }
109
110 func init() {
111         for name, enum := range typeLookup {
112                 typeNames[enum] = name
113         }
114 }
115
116 type Size_t uint64
117 type Off_t int64
118
119 var reflectTypeMap = map[reflect.Kind]Type{
120         reflect.Bool:    Bool,
121         reflect.Int8:    Int8,
122         reflect.Int16:   Int16,
123         reflect.Int:     Int32,
124         reflect.Int32:   Int32,
125         reflect.Int64:   Int64,
126         reflect.Uint8:   Uint8,
127         reflect.Uint16:  Uint16,
128         reflect.Uint:    Uint32,
129         reflect.Uint32:  Uint32,
130         reflect.Uint64:  Uint64,
131         reflect.Float32: Float32,
132         reflect.Float64: Float64,
133         reflect.String:  String,
134         reflect.Struct:  Struct,
135         reflect.Ptr:     Ptr,
136 }