pointer_reflect.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. // +build appengine
  32. // This file contains an implementation of proto field accesses using package reflect.
  33. // It is slower than the code in pointer_unsafe.go but it avoids package unsafe and can
  34. // be used on App Engine.
  35. package proto
  36. import (
  37. "math"
  38. "reflect"
  39. )
  40. // A structPointer is a pointer to a struct.
  41. type structPointer struct {
  42. v reflect.Value
  43. }
  44. // toStructPointer returns a structPointer equivalent to the given reflect value.
  45. // The reflect value must itself be a pointer to a struct.
  46. func toStructPointer(v reflect.Value) structPointer {
  47. return structPointer{v}
  48. }
  49. // IsNil reports whether p is nil.
  50. func structPointer_IsNil(p structPointer) bool {
  51. return p.v.IsNil()
  52. }
  53. // Interface returns the struct pointer as an interface value.
  54. func structPointer_Interface(p structPointer, _ reflect.Type) interface{} {
  55. return p.v.Interface()
  56. }
  57. // A field identifies a field in a struct, accessible from a structPointer.
  58. // In this implementation, a field is identified by the sequence of field indices
  59. // passed to reflect's FieldByIndex.
  60. type field []int
  61. // toField returns a field equivalent to the given reflect field.
  62. func toField(f *reflect.StructField) field {
  63. return f.Index
  64. }
  65. // invalidField is an invalid field identifier.
  66. var invalidField = field(nil)
  67. // IsValid reports whether the field identifier is valid.
  68. func (f field) IsValid() bool { return f != nil }
  69. // field returns the given field in the struct as a reflect value.
  70. func structPointer_field(p structPointer, f field) reflect.Value {
  71. // Special case: an extension map entry with a value of type T
  72. // passes a *T to the struct-handling code with a zero field,
  73. // expecting that it will be treated as equivalent to *struct{ X T },
  74. // which has the same memory layout. We have to handle that case
  75. // specially, because reflect will panic if we call FieldByIndex on a
  76. // non-struct.
  77. if f == nil {
  78. return p.v.Elem()
  79. }
  80. return p.v.Elem().FieldByIndex(f)
  81. }
  82. // ifield returns the given field in the struct as an interface value.
  83. func structPointer_ifield(p structPointer, f field) interface{} {
  84. return structPointer_field(p, f).Addr().Interface()
  85. }
  86. // Bytes returns the address of a []byte field in the struct.
  87. func structPointer_Bytes(p structPointer, f field) *[]byte {
  88. return structPointer_ifield(p, f).(*[]byte)
  89. }
  90. // BytesSlice returns the address of a [][]byte field in the struct.
  91. func structPointer_BytesSlice(p structPointer, f field) *[][]byte {
  92. return structPointer_ifield(p, f).(*[][]byte)
  93. }
  94. // Bool returns the address of a *bool field in the struct.
  95. func structPointer_Bool(p structPointer, f field) **bool {
  96. return structPointer_ifield(p, f).(**bool)
  97. }
  98. // BoolVal returns the address of a bool field in the struct.
  99. func structPointer_BoolVal(p structPointer, f field) *bool {
  100. return structPointer_ifield(p, f).(*bool)
  101. }
  102. // BoolSlice returns the address of a []bool field in the struct.
  103. func structPointer_BoolSlice(p structPointer, f field) *[]bool {
  104. return structPointer_ifield(p, f).(*[]bool)
  105. }
  106. // String returns the address of a *string field in the struct.
  107. func structPointer_String(p structPointer, f field) **string {
  108. return structPointer_ifield(p, f).(**string)
  109. }
  110. // StringVal returns the address of a string field in the struct.
  111. func structPointer_StringVal(p structPointer, f field) *string {
  112. return structPointer_ifield(p, f).(*string)
  113. }
  114. // StringSlice returns the address of a []string field in the struct.
  115. func structPointer_StringSlice(p structPointer, f field) *[]string {
  116. return structPointer_ifield(p, f).(*[]string)
  117. }
  118. // ExtMap returns the address of an extension map field in the struct.
  119. func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
  120. return structPointer_ifield(p, f).(*map[int32]Extension)
  121. }
  122. // NewAt returns the reflect.Value for a pointer to a field in the struct.
  123. func structPointer_NewAt(p structPointer, f field, typ reflect.Type) reflect.Value {
  124. return structPointer_field(p, f).Addr()
  125. }
  126. // SetStructPointer writes a *struct field in the struct.
  127. func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
  128. structPointer_field(p, f).Set(q.v)
  129. }
  130. // GetStructPointer reads a *struct field in the struct.
  131. func structPointer_GetStructPointer(p structPointer, f field) structPointer {
  132. return structPointer{structPointer_field(p, f)}
  133. }
  134. // StructPointerSlice the address of a []*struct field in the struct.
  135. func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
  136. return structPointerSlice{structPointer_field(p, f)}
  137. }
  138. // A structPointerSlice represents the address of a slice of pointers to structs
  139. // (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
  140. type structPointerSlice struct {
  141. v reflect.Value
  142. }
  143. func (p structPointerSlice) Len() int { return p.v.Len() }
  144. func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
  145. func (p structPointerSlice) Append(q structPointer) {
  146. p.v.Set(reflect.Append(p.v, q.v))
  147. }
  148. var (
  149. int32Type = reflect.TypeOf(int32(0))
  150. uint32Type = reflect.TypeOf(uint32(0))
  151. float32Type = reflect.TypeOf(float32(0))
  152. int64Type = reflect.TypeOf(int64(0))
  153. uint64Type = reflect.TypeOf(uint64(0))
  154. float64Type = reflect.TypeOf(float64(0))
  155. )
  156. // A word32 represents a field of type *int32, *uint32, *float32, or *enum.
  157. // That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
  158. type word32 struct {
  159. v reflect.Value
  160. }
  161. // IsNil reports whether p is nil.
  162. func word32_IsNil(p word32) bool {
  163. return p.v.IsNil()
  164. }
  165. // Set sets p to point at a newly allocated word with bits set to x.
  166. func word32_Set(p word32, o *Buffer, x uint32) {
  167. t := p.v.Type().Elem()
  168. switch t {
  169. case int32Type:
  170. if len(o.int32s) == 0 {
  171. o.int32s = make([]int32, uint32PoolSize)
  172. }
  173. o.int32s[0] = int32(x)
  174. p.v.Set(reflect.ValueOf(&o.int32s[0]))
  175. o.int32s = o.int32s[1:]
  176. return
  177. case uint32Type:
  178. if len(o.uint32s) == 0 {
  179. o.uint32s = make([]uint32, uint32PoolSize)
  180. }
  181. o.uint32s[0] = x
  182. p.v.Set(reflect.ValueOf(&o.uint32s[0]))
  183. o.uint32s = o.uint32s[1:]
  184. return
  185. case float32Type:
  186. if len(o.float32s) == 0 {
  187. o.float32s = make([]float32, uint32PoolSize)
  188. }
  189. o.float32s[0] = math.Float32frombits(x)
  190. p.v.Set(reflect.ValueOf(&o.float32s[0]))
  191. o.float32s = o.float32s[1:]
  192. return
  193. }
  194. // must be enum
  195. p.v.Set(reflect.New(t))
  196. p.v.Elem().SetInt(int64(int32(x)))
  197. }
  198. // Get gets the bits pointed at by p, as a uint32.
  199. func word32_Get(p word32) uint32 {
  200. elem := p.v.Elem()
  201. switch elem.Kind() {
  202. case reflect.Int32:
  203. return uint32(elem.Int())
  204. case reflect.Uint32:
  205. return uint32(elem.Uint())
  206. case reflect.Float32:
  207. return math.Float32bits(float32(elem.Float()))
  208. }
  209. panic("unreachable")
  210. }
  211. // Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
  212. func structPointer_Word32(p structPointer, f field) word32 {
  213. return word32{structPointer_field(p, f)}
  214. }
  215. // A word32Val represents a field of type int32, uint32, float32, or enum.
  216. // That is, v.Type() is int32, uint32, float32, or enum and v is assignable.
  217. type word32Val struct {
  218. v reflect.Value
  219. }
  220. // Set sets *p to x.
  221. func word32Val_Set(p word32Val, x uint32) {
  222. switch p.v.Type() {
  223. case int32Type:
  224. p.v.SetInt(int64(x))
  225. return
  226. case uint32Type:
  227. p.v.SetUint(uint64(x))
  228. return
  229. case float32Type:
  230. p.v.SetFloat(float64(math.Float32frombits(x)))
  231. return
  232. }
  233. // must be enum
  234. p.v.SetInt(int64(int32(x)))
  235. }
  236. // Get gets the bits pointed at by p, as a uint32.
  237. func word32Val_Get(p word32Val) uint32 {
  238. elem := p.v
  239. switch elem.Kind() {
  240. case reflect.Int32:
  241. return uint32(elem.Int())
  242. case reflect.Uint32:
  243. return uint32(elem.Uint())
  244. case reflect.Float32:
  245. return math.Float32bits(float32(elem.Float()))
  246. }
  247. panic("unreachable")
  248. }
  249. // Word32Val returns a reference to a int32, uint32, float32, or enum field in the struct.
  250. func structPointer_Word32Val(p structPointer, f field) word32Val {
  251. return word32Val{structPointer_field(p, f)}
  252. }
  253. // A word32Slice is a slice of 32-bit values.
  254. // That is, v.Type() is []int32, []uint32, []float32, or []enum.
  255. type word32Slice struct {
  256. v reflect.Value
  257. }
  258. func (p word32Slice) Append(x uint32) {
  259. n, m := p.v.Len(), p.v.Cap()
  260. if n < m {
  261. p.v.SetLen(n + 1)
  262. } else {
  263. t := p.v.Type().Elem()
  264. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  265. }
  266. elem := p.v.Index(n)
  267. switch elem.Kind() {
  268. case reflect.Int32:
  269. elem.SetInt(int64(int32(x)))
  270. case reflect.Uint32:
  271. elem.SetUint(uint64(x))
  272. case reflect.Float32:
  273. elem.SetFloat(float64(math.Float32frombits(x)))
  274. }
  275. }
  276. func (p word32Slice) Len() int {
  277. return p.v.Len()
  278. }
  279. func (p word32Slice) Index(i int) uint32 {
  280. elem := p.v.Index(i)
  281. switch elem.Kind() {
  282. case reflect.Int32:
  283. return uint32(elem.Int())
  284. case reflect.Uint32:
  285. return uint32(elem.Uint())
  286. case reflect.Float32:
  287. return math.Float32bits(float32(elem.Float()))
  288. }
  289. panic("unreachable")
  290. }
  291. // Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
  292. func structPointer_Word32Slice(p structPointer, f field) word32Slice {
  293. return word32Slice{structPointer_field(p, f)}
  294. }
  295. // word64 is like word32 but for 64-bit values.
  296. type word64 struct {
  297. v reflect.Value
  298. }
  299. func word64_Set(p word64, o *Buffer, x uint64) {
  300. t := p.v.Type().Elem()
  301. switch t {
  302. case int64Type:
  303. if len(o.int64s) == 0 {
  304. o.int64s = make([]int64, uint64PoolSize)
  305. }
  306. o.int64s[0] = int64(x)
  307. p.v.Set(reflect.ValueOf(&o.int64s[0]))
  308. o.int64s = o.int64s[1:]
  309. return
  310. case uint64Type:
  311. if len(o.uint64s) == 0 {
  312. o.uint64s = make([]uint64, uint64PoolSize)
  313. }
  314. o.uint64s[0] = x
  315. p.v.Set(reflect.ValueOf(&o.uint64s[0]))
  316. o.uint64s = o.uint64s[1:]
  317. return
  318. case float64Type:
  319. if len(o.float64s) == 0 {
  320. o.float64s = make([]float64, uint64PoolSize)
  321. }
  322. o.float64s[0] = math.Float64frombits(x)
  323. p.v.Set(reflect.ValueOf(&o.float64s[0]))
  324. o.float64s = o.float64s[1:]
  325. return
  326. }
  327. panic("unreachable")
  328. }
  329. func word64_IsNil(p word64) bool {
  330. return p.v.IsNil()
  331. }
  332. func word64_Get(p word64) uint64 {
  333. elem := p.v.Elem()
  334. switch elem.Kind() {
  335. case reflect.Int64:
  336. return uint64(elem.Int())
  337. case reflect.Uint64:
  338. return elem.Uint()
  339. case reflect.Float64:
  340. return math.Float64bits(elem.Float())
  341. }
  342. panic("unreachable")
  343. }
  344. func structPointer_Word64(p structPointer, f field) word64 {
  345. return word64{structPointer_field(p, f)}
  346. }
  347. // word64Val is like word32Val but for 64-bit values.
  348. type word64Val struct {
  349. v reflect.Value
  350. }
  351. func word64Val_Set(p word64Val, o *Buffer, x uint64) {
  352. switch p.v.Type() {
  353. case int64Type:
  354. p.v.SetInt(int64(x))
  355. return
  356. case uint64Type:
  357. p.v.SetUint(x)
  358. return
  359. case float64Type:
  360. p.v.SetFloat(math.Float64frombits(x))
  361. return
  362. }
  363. panic("unreachable")
  364. }
  365. func word64Val_Get(p word64Val) uint64 {
  366. elem := p.v
  367. switch elem.Kind() {
  368. case reflect.Int64:
  369. return uint64(elem.Int())
  370. case reflect.Uint64:
  371. return elem.Uint()
  372. case reflect.Float64:
  373. return math.Float64bits(elem.Float())
  374. }
  375. panic("unreachable")
  376. }
  377. func structPointer_Word64Val(p structPointer, f field) word64Val {
  378. return word64Val{structPointer_field(p, f)}
  379. }
  380. type word64Slice struct {
  381. v reflect.Value
  382. }
  383. func (p word64Slice) Append(x uint64) {
  384. n, m := p.v.Len(), p.v.Cap()
  385. if n < m {
  386. p.v.SetLen(n + 1)
  387. } else {
  388. t := p.v.Type().Elem()
  389. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  390. }
  391. elem := p.v.Index(n)
  392. switch elem.Kind() {
  393. case reflect.Int64:
  394. elem.SetInt(int64(int64(x)))
  395. case reflect.Uint64:
  396. elem.SetUint(uint64(x))
  397. case reflect.Float64:
  398. elem.SetFloat(float64(math.Float64frombits(x)))
  399. }
  400. }
  401. func (p word64Slice) Len() int {
  402. return p.v.Len()
  403. }
  404. func (p word64Slice) Index(i int) uint64 {
  405. elem := p.v.Index(i)
  406. switch elem.Kind() {
  407. case reflect.Int64:
  408. return uint64(elem.Int())
  409. case reflect.Uint64:
  410. return uint64(elem.Uint())
  411. case reflect.Float64:
  412. return math.Float64bits(float64(elem.Float()))
  413. }
  414. panic("unreachable")
  415. }
  416. func structPointer_Word64Slice(p structPointer, f field) word64Slice {
  417. return word64Slice{structPointer_field(p, f)}
  418. }