pointer_reflect.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  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. // BoolSlice returns the address of a []bool field in the struct.
  99. func structPointer_BoolSlice(p structPointer, f field) *[]bool {
  100. return structPointer_ifield(p, f).(*[]bool)
  101. }
  102. // String returns the address of a *string field in the struct.
  103. func structPointer_String(p structPointer, f field) **string {
  104. return structPointer_ifield(p, f).(**string)
  105. }
  106. // StringSlice returns the address of a []string field in the struct.
  107. func structPointer_StringSlice(p structPointer, f field) *[]string {
  108. return structPointer_ifield(p, f).(*[]string)
  109. }
  110. // ExtMap returns the address of an extension map field in the struct.
  111. func structPointer_ExtMap(p structPointer, f field) *map[int32]Extension {
  112. return structPointer_ifield(p, f).(*map[int32]Extension)
  113. }
  114. // SetStructPointer writes a *struct field in the struct.
  115. func structPointer_SetStructPointer(p structPointer, f field, q structPointer) {
  116. structPointer_field(p, f).Set(q.v)
  117. }
  118. // GetStructPointer reads a *struct field in the struct.
  119. func structPointer_GetStructPointer(p structPointer, f field) structPointer {
  120. return structPointer{structPointer_field(p, f)}
  121. }
  122. // StructPointerSlice the address of a []*struct field in the struct.
  123. func structPointer_StructPointerSlice(p structPointer, f field) structPointerSlice {
  124. return structPointerSlice{structPointer_field(p, f)}
  125. }
  126. // A structPointerSlice represents the address of a slice of pointers to structs
  127. // (themselves messages or groups). That is, v.Type() is *[]*struct{...}.
  128. type structPointerSlice struct {
  129. v reflect.Value
  130. }
  131. func (p structPointerSlice) Len() int { return p.v.Len() }
  132. func (p structPointerSlice) Index(i int) structPointer { return structPointer{p.v.Index(i)} }
  133. func (p structPointerSlice) Append(q structPointer) {
  134. p.v.Set(reflect.Append(p.v, q.v))
  135. }
  136. var (
  137. int32Type = reflect.TypeOf(int32(0))
  138. uint32Type = reflect.TypeOf(uint32(0))
  139. float32Type = reflect.TypeOf(float32(0))
  140. int64Type = reflect.TypeOf(int64(0))
  141. uint64Type = reflect.TypeOf(uint64(0))
  142. float64Type = reflect.TypeOf(float64(0))
  143. )
  144. // A word32 represents a field of type *int32, *uint32, *float32, or *enum.
  145. // That is, v.Type() is *int32, *uint32, *float32, or *enum and v is assignable.
  146. type word32 struct {
  147. v reflect.Value
  148. }
  149. // IsNil reports whether p is nil.
  150. func word32_IsNil(p word32) bool {
  151. return p.v.IsNil()
  152. }
  153. // Set sets p to point at a newly allocated word with bits set to x.
  154. func word32_Set(p word32, o *Buffer, x uint32) {
  155. t := p.v.Type().Elem()
  156. switch t {
  157. case int32Type:
  158. if len(o.int32s) == 0 {
  159. o.int32s = make([]int32, uint32PoolSize)
  160. }
  161. o.int32s[0] = int32(x)
  162. p.v.Set(reflect.ValueOf(&o.int32s[0]))
  163. o.int32s = o.int32s[1:]
  164. return
  165. case uint32Type:
  166. if len(o.uint32s) == 0 {
  167. o.uint32s = make([]uint32, uint32PoolSize)
  168. }
  169. o.uint32s[0] = x
  170. p.v.Set(reflect.ValueOf(&o.uint32s[0]))
  171. o.uint32s = o.uint32s[1:]
  172. return
  173. case float32Type:
  174. if len(o.float32s) == 0 {
  175. o.float32s = make([]float32, uint32PoolSize)
  176. }
  177. o.float32s[0] = math.Float32frombits(x)
  178. p.v.Set(reflect.ValueOf(&o.float32s[0]))
  179. o.float32s = o.float32s[1:]
  180. return
  181. }
  182. // must be enum
  183. p.v.Set(reflect.New(t))
  184. p.v.Elem().SetInt(int64(int32(x)))
  185. }
  186. // Get gets the bits pointed at by p, as a uint32.
  187. func word32_Get(p word32) uint32 {
  188. elem := p.v.Elem()
  189. switch elem.Kind() {
  190. case reflect.Int32:
  191. return uint32(elem.Int())
  192. case reflect.Uint32:
  193. return uint32(elem.Uint())
  194. case reflect.Float32:
  195. return math.Float32bits(float32(elem.Float()))
  196. }
  197. panic("unreachable")
  198. }
  199. // Word32 returns a reference to a *int32, *uint32, *float32, or *enum field in the struct.
  200. func structPointer_Word32(p structPointer, f field) word32 {
  201. return word32{structPointer_field(p, f)}
  202. }
  203. // A word32Slice is a slice of 32-bit values.
  204. // That is, v.Type() is []int32, []uint32, []float32, or []enum.
  205. type word32Slice struct {
  206. v reflect.Value
  207. }
  208. func (p word32Slice) Append(x uint32) {
  209. n, m := p.v.Len(), p.v.Cap()
  210. if n < m {
  211. p.v.SetLen(n + 1)
  212. } else {
  213. t := p.v.Type().Elem()
  214. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  215. }
  216. elem := p.v.Index(n)
  217. switch elem.Kind() {
  218. case reflect.Int32:
  219. elem.SetInt(int64(int32(x)))
  220. case reflect.Uint32:
  221. elem.SetUint(uint64(x))
  222. case reflect.Float32:
  223. elem.SetFloat(float64(math.Float32frombits(x)))
  224. }
  225. }
  226. func (p word32Slice) Len() int {
  227. return p.v.Len()
  228. }
  229. func (p word32Slice) Index(i int) uint32 {
  230. elem := p.v.Index(i)
  231. switch elem.Kind() {
  232. case reflect.Int32:
  233. return uint32(elem.Int())
  234. case reflect.Uint32:
  235. return uint32(elem.Uint())
  236. case reflect.Float32:
  237. return math.Float32bits(float32(elem.Float()))
  238. }
  239. panic("unreachable")
  240. }
  241. // Word32Slice returns a reference to a []int32, []uint32, []float32, or []enum field in the struct.
  242. func structPointer_Word32Slice(p structPointer, f field) word32Slice {
  243. return word32Slice{structPointer_field(p, f)}
  244. }
  245. // word64 is like word32 but for 64-bit values.
  246. type word64 struct {
  247. v reflect.Value
  248. }
  249. func word64_Set(p word64, o *Buffer, x uint64) {
  250. t := p.v.Type().Elem()
  251. switch t {
  252. case int64Type:
  253. if len(o.int64s) == 0 {
  254. o.int64s = make([]int64, uint64PoolSize)
  255. }
  256. o.int64s[0] = int64(x)
  257. p.v.Set(reflect.ValueOf(&o.int64s[0]))
  258. o.int64s = o.int64s[1:]
  259. return
  260. case uint64Type:
  261. if len(o.uint64s) == 0 {
  262. o.uint64s = make([]uint64, uint64PoolSize)
  263. }
  264. o.uint64s[0] = x
  265. p.v.Set(reflect.ValueOf(&o.uint64s[0]))
  266. o.uint64s = o.uint64s[1:]
  267. return
  268. case float64Type:
  269. if len(o.float64s) == 0 {
  270. o.float64s = make([]float64, uint64PoolSize)
  271. }
  272. o.float64s[0] = math.Float64frombits(x)
  273. p.v.Set(reflect.ValueOf(&o.float64s[0]))
  274. o.float64s = o.float64s[1:]
  275. return
  276. }
  277. panic("unreachable")
  278. }
  279. func word64_IsNil(p word64) bool {
  280. return p.v.IsNil()
  281. }
  282. func word64_Get(p word64) uint64 {
  283. elem := p.v.Elem()
  284. switch elem.Kind() {
  285. case reflect.Int64:
  286. return uint64(elem.Int())
  287. case reflect.Uint64:
  288. return elem.Uint()
  289. case reflect.Float64:
  290. return math.Float64bits(elem.Float())
  291. }
  292. panic("unreachable")
  293. }
  294. func structPointer_Word64(p structPointer, f field) word64 {
  295. return word64{structPointer_field(p, f)}
  296. }
  297. type word64Slice struct {
  298. v reflect.Value
  299. }
  300. func (p word64Slice) Append(x uint64) {
  301. n, m := p.v.Len(), p.v.Cap()
  302. if n < m {
  303. p.v.SetLen(n + 1)
  304. } else {
  305. t := p.v.Type().Elem()
  306. p.v.Set(reflect.Append(p.v, reflect.Zero(t)))
  307. }
  308. elem := p.v.Index(n)
  309. switch elem.Kind() {
  310. case reflect.Int64:
  311. elem.SetInt(int64(int64(x)))
  312. case reflect.Uint64:
  313. elem.SetUint(uint64(x))
  314. case reflect.Float64:
  315. elem.SetFloat(float64(math.Float64frombits(x)))
  316. }
  317. }
  318. func (p word64Slice) Len() int {
  319. return p.v.Len()
  320. }
  321. func (p word64Slice) Index(i int) uint64 {
  322. elem := p.v.Index(i)
  323. switch elem.Kind() {
  324. case reflect.Int64:
  325. return uint64(elem.Int())
  326. case reflect.Uint64:
  327. return uint64(elem.Uint())
  328. case reflect.Float64:
  329. return math.Float64bits(float64(elem.Float()))
  330. }
  331. panic("unreachable")
  332. }
  333. func structPointer_Word64Slice(p structPointer, f field) word64Slice {
  334. return word64Slice{structPointer_field(p, f)}
  335. }