pointer_reflect.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build purego appengine
  5. package impl
  6. import (
  7. "fmt"
  8. "reflect"
  9. )
  10. // offset represents the offset to a struct field, accessible from a pointer.
  11. // The offset is the field index into a struct.
  12. type offset []int
  13. // offsetOf returns a field offset for the struct field.
  14. func offsetOf(f reflect.StructField) offset {
  15. if len(f.Index) != 1 {
  16. panic("embedded structs are not supported")
  17. }
  18. return f.Index
  19. }
  20. // IsValid reports whether the offset is valid.
  21. func (f offset) IsValid() bool { return f != nil }
  22. // invalidOffset is an invalid field offset.
  23. var invalidOffset = offset(nil)
  24. // zeroOffset is a noop when calling pointer.Apply.
  25. var zeroOffset = offset([]int{0})
  26. // pointer is an abstract representation of a pointer to a struct or field.
  27. type pointer struct{ v reflect.Value }
  28. // pointerOfValue returns v as a pointer.
  29. func pointerOfValue(v reflect.Value) pointer {
  30. return pointer{v: v}
  31. }
  32. // pointerOfIface returns the pointer portion of an interface.
  33. func pointerOfIface(v interface{}) pointer {
  34. return pointer{v: reflect.ValueOf(v)}
  35. }
  36. // IsNil reports whether the pointer is nil.
  37. func (p pointer) IsNil() bool {
  38. return p.v.IsNil()
  39. }
  40. // Apply adds an offset to the pointer to derive a new pointer
  41. // to a specified field. The current pointer must be pointing at a struct.
  42. func (p pointer) Apply(f offset) pointer {
  43. // TODO: Handle unexported fields in an API that hides XXX fields?
  44. return pointer{v: p.v.Elem().FieldByIndex(f).Addr()}
  45. }
  46. // AsValueOf treats p as a pointer to an object of type t and returns the value.
  47. // It is equivalent to reflect.ValueOf(p.AsIfaceOf(t))
  48. func (p pointer) AsValueOf(t reflect.Type) reflect.Value {
  49. if got := p.v.Type().Elem(); got != t {
  50. panic(fmt.Sprintf("invalid type: got %v, want %v", got, t))
  51. }
  52. return p.v
  53. }
  54. // AsIfaceOf treats p as a pointer to an object of type t and returns the value.
  55. // It is equivalent to p.AsValueOf(t).Interface()
  56. func (p pointer) AsIfaceOf(t reflect.Type) interface{} {
  57. return p.AsValueOf(t).Interface()
  58. }
  59. func (p pointer) Bool() *bool { return p.v.Interface().(*bool) }
  60. func (p pointer) BoolPtr() **bool { return p.v.Interface().(**bool) }
  61. func (p pointer) BoolSlice() *[]bool { return p.v.Interface().(*[]bool) }
  62. func (p pointer) Int32() *int32 { return p.v.Interface().(*int32) }
  63. func (p pointer) Int32Ptr() **int32 { return p.v.Interface().(**int32) }
  64. func (p pointer) Int32Slice() *[]int32 { return p.v.Interface().(*[]int32) }
  65. func (p pointer) Int64() *int64 { return p.v.Interface().(*int64) }
  66. func (p pointer) Int64Ptr() **int64 { return p.v.Interface().(**int64) }
  67. func (p pointer) Int64Slice() *[]int64 { return p.v.Interface().(*[]int64) }
  68. func (p pointer) Uint32() *uint32 { return p.v.Interface().(*uint32) }
  69. func (p pointer) Uint32Ptr() **uint32 { return p.v.Interface().(**uint32) }
  70. func (p pointer) Uint32Slice() *[]uint32 { return p.v.Interface().(*[]uint32) }
  71. func (p pointer) Uint64() *uint64 { return p.v.Interface().(*uint64) }
  72. func (p pointer) Uint64Ptr() **uint64 { return p.v.Interface().(**uint64) }
  73. func (p pointer) Uint64Slice() *[]uint64 { return p.v.Interface().(*[]uint64) }
  74. func (p pointer) Float32() *float32 { return p.v.Interface().(*float32) }
  75. func (p pointer) Float32Ptr() **float32 { return p.v.Interface().(**float32) }
  76. func (p pointer) Float32Slice() *[]float32 { return p.v.Interface().(*[]float32) }
  77. func (p pointer) Float64() *float64 { return p.v.Interface().(*float64) }
  78. func (p pointer) Float64Ptr() **float64 { return p.v.Interface().(**float64) }
  79. func (p pointer) Float64Slice() *[]float64 { return p.v.Interface().(*[]float64) }
  80. func (p pointer) String() *string { return p.v.Interface().(*string) }
  81. func (p pointer) StringPtr() **string { return p.v.Interface().(**string) }
  82. func (p pointer) StringSlice() *[]string { return p.v.Interface().(*[]string) }
  83. func (p pointer) Bytes() *[]byte { return p.v.Interface().(*[]byte) }
  84. func (p pointer) BytesSlice() *[][]byte { return p.v.Interface().(*[][]byte) }
  85. func (p pointer) Extensions() *map[int32]ExtensionField {
  86. return p.v.Interface().(*map[int32]ExtensionField)
  87. }
  88. func (p pointer) Elem() pointer {
  89. return pointer{v: p.v.Elem()}
  90. }
  91. // PointerSlice copies []*T from p as a new []pointer.
  92. // This behavior differs from the implementation in pointer_unsafe.go.
  93. func (p pointer) PointerSlice() []pointer {
  94. // TODO: reconsider this
  95. if p.v.IsNil() {
  96. return nil
  97. }
  98. n := p.v.Elem().Len()
  99. s := make([]pointer, n)
  100. for i := 0; i < n; i++ {
  101. s[i] = pointer{v: p.v.Elem().Index(i)}
  102. }
  103. return s
  104. }