OSDN Git Service

feat(warder): add warder backbone (#181)
[bytom/vapor.git] / vendor / github.com / ugorji / go / codec / helper_not_unsafe.go
1 // +build !go1.7 safe appengine
2
3 // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
4 // Use of this source code is governed by a MIT license found in the LICENSE file.
5
6 package codec
7
8 import (
9         "reflect"
10         "sync/atomic"
11         "time"
12 )
13
14 const safeMode = true
15
16 // stringView returns a view of the []byte as a string.
17 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
18 // In regular safe mode, it is an allocation and copy.
19 //
20 // Usage: Always maintain a reference to v while result of this call is in use,
21 //        and call keepAlive4BytesView(v) at point where done with view.
22 func stringView(v []byte) string {
23         return string(v)
24 }
25
26 // bytesView returns a view of the string as a []byte.
27 // In unsafe mode, it doesn't incur allocation and copying caused by conversion.
28 // In regular safe mode, it is an allocation and copy.
29 //
30 // Usage: Always maintain a reference to v while result of this call is in use,
31 //        and call keepAlive4BytesView(v) at point where done with view.
32 func bytesView(v string) []byte {
33         return []byte(v)
34 }
35
36 func definitelyNil(v interface{}) bool {
37         // this is a best-effort option.
38         // We just return false, so we don't unnecessarily incur the cost of reflection this early.
39         return false
40 }
41
42 func rv2i(rv reflect.Value) interface{} {
43         return rv.Interface()
44 }
45
46 func rt2id(rt reflect.Type) uintptr {
47         return reflect.ValueOf(rt).Pointer()
48 }
49
50 func rv2rtid(rv reflect.Value) uintptr {
51         return reflect.ValueOf(rv.Type()).Pointer()
52 }
53
54 func i2rtid(i interface{}) uintptr {
55         return reflect.ValueOf(reflect.TypeOf(i)).Pointer()
56 }
57
58 // --------------------------
59
60 func isEmptyValue(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool {
61         switch v.Kind() {
62         case reflect.Invalid:
63                 return true
64         case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
65                 return v.Len() == 0
66         case reflect.Bool:
67                 return !v.Bool()
68         case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
69                 return v.Int() == 0
70         case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
71                 return v.Uint() == 0
72         case reflect.Float32, reflect.Float64:
73                 return v.Float() == 0
74         case reflect.Interface, reflect.Ptr:
75                 if deref {
76                         if v.IsNil() {
77                                 return true
78                         }
79                         return isEmptyValue(v.Elem(), tinfos, deref, checkStruct)
80                 }
81                 return v.IsNil()
82         case reflect.Struct:
83                 return isEmptyStruct(v, tinfos, deref, checkStruct)
84         }
85         return false
86 }
87
88 // --------------------------
89 // type ptrToRvMap struct{}
90
91 // func (*ptrToRvMap) init() {}
92 // func (*ptrToRvMap) get(i interface{}) reflect.Value {
93 //      return reflect.ValueOf(i).Elem()
94 // }
95
96 // --------------------------
97 type atomicClsErr struct {
98         v atomic.Value
99 }
100
101 func (x *atomicClsErr) load() clsErr {
102         i := x.v.Load()
103         if i == nil {
104                 return clsErr{}
105         }
106         return i.(clsErr)
107 }
108
109 func (x *atomicClsErr) store(p clsErr) {
110         x.v.Store(p)
111 }
112
113 // --------------------------
114 type atomicTypeInfoSlice struct { // expected to be 2 words
115         v atomic.Value
116 }
117
118 func (x *atomicTypeInfoSlice) load() []rtid2ti {
119         i := x.v.Load()
120         if i == nil {
121                 return nil
122         }
123         return i.([]rtid2ti)
124 }
125
126 func (x *atomicTypeInfoSlice) store(p []rtid2ti) {
127         x.v.Store(p)
128 }
129
130 // --------------------------
131 func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
132         rv.SetBytes(d.rawBytes())
133 }
134
135 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
136         rv.SetString(d.d.DecodeString())
137 }
138
139 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
140         rv.SetBool(d.d.DecodeBool())
141 }
142
143 func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
144         rv.Set(reflect.ValueOf(d.d.DecodeTime()))
145 }
146
147 func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
148         fv := d.d.DecodeFloat64()
149         if chkOvf.Float32(fv) {
150                 d.errorf("float32 overflow: %v", fv)
151         }
152         rv.SetFloat(fv)
153 }
154
155 func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
156         rv.SetFloat(d.d.DecodeFloat64())
157 }
158
159 func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
160         rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
161 }
162
163 func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
164         rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 8))
165 }
166
167 func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
168         rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 16))
169 }
170
171 func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
172         rv.SetInt(chkOvf.IntV(d.d.DecodeInt64(), 32))
173 }
174
175 func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
176         rv.SetInt(d.d.DecodeInt64())
177 }
178
179 func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
180         rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
181 }
182
183 func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
184         rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
185 }
186
187 func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
188         rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 8))
189 }
190
191 func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
192         rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 16))
193 }
194
195 func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
196         rv.SetUint(chkOvf.UintV(d.d.DecodeUint64(), 32))
197 }
198
199 func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
200         rv.SetUint(d.d.DecodeUint64())
201 }
202
203 // ----------------
204
205 func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
206         e.e.EncodeBool(rv.Bool())
207 }
208
209 func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
210         e.e.EncodeTime(rv2i(rv).(time.Time))
211 }
212
213 func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
214         e.e.EncodeString(cUTF8, rv.String())
215 }
216
217 func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
218         e.e.EncodeFloat64(rv.Float())
219 }
220
221 func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
222         e.e.EncodeFloat32(float32(rv.Float()))
223 }
224
225 func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
226         e.e.EncodeInt(rv.Int())
227 }
228
229 func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
230         e.e.EncodeInt(rv.Int())
231 }
232
233 func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
234         e.e.EncodeInt(rv.Int())
235 }
236
237 func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
238         e.e.EncodeInt(rv.Int())
239 }
240
241 func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
242         e.e.EncodeInt(rv.Int())
243 }
244
245 func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
246         e.e.EncodeUint(rv.Uint())
247 }
248
249 func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
250         e.e.EncodeUint(rv.Uint())
251 }
252
253 func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
254         e.e.EncodeUint(rv.Uint())
255 }
256
257 func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
258         e.e.EncodeUint(rv.Uint())
259 }
260
261 func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
262         e.e.EncodeUint(rv.Uint())
263 }
264
265 func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
266         e.e.EncodeUint(rv.Uint())
267 }
268
269 // // keepAlive4BytesView maintains a reference to the input parameter for bytesView.
270 // //
271 // // Usage: call this at point where done with the bytes view.
272 // func keepAlive4BytesView(v string) {}
273
274 // // keepAlive4BytesView maintains a reference to the input parameter for stringView.
275 // //
276 // // Usage: call this at point where done with the string view.
277 // func keepAlive4StringView(v []byte) {}
278
279 // func definitelyNil(v interface{}) bool {
280 //      rv := reflect.ValueOf(v)
281 //      switch rv.Kind() {
282 //      case reflect.Invalid:
283 //              return true
284 //      case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Slice, reflect.Map, reflect.Func:
285 //              return rv.IsNil()
286 //      default:
287 //              return false
288 //      }
289 // }