safe_map.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package reflect2
  2. import (
  3. "reflect"
  4. "unsafe"
  5. )
  6. type safeMapType struct {
  7. safeType
  8. }
  9. func (type2 *safeMapType) Key() Type {
  10. return type2.safeType.cfg.Type2(type2.Type.Key())
  11. }
  12. func (type2 *safeMapType) MakeMap(cap int) interface{} {
  13. ptr := reflect.New(type2.Type)
  14. ptr.Elem().Set(reflect.MakeMap(type2.Type))
  15. return ptr.Interface()
  16. }
  17. func (type2 *safeMapType) UnsafeMakeMap(cap int) unsafe.Pointer {
  18. panic("does not support unsafe operation")
  19. }
  20. func (type2 *safeMapType) SetIndex(obj interface{}, key interface{}, elem interface{}) {
  21. keyVal := reflect.ValueOf(key)
  22. elemVal := reflect.ValueOf(elem)
  23. val := reflect.ValueOf(obj)
  24. val.Elem().SetMapIndex(keyVal.Elem(), elemVal.Elem())
  25. }
  26. func (type2 *safeMapType) UnsafeSetIndex(obj unsafe.Pointer, key unsafe.Pointer, elem unsafe.Pointer) {
  27. panic("does not support unsafe operation")
  28. }
  29. func (type2 *safeMapType) TryGetIndex(obj interface{}, key interface{}) (interface{}, bool) {
  30. keyVal := reflect.ValueOf(key)
  31. if key == nil {
  32. keyVal = reflect.New(type2.Type.Key()).Elem()
  33. }
  34. val := reflect.ValueOf(obj).MapIndex(keyVal)
  35. if !val.IsValid() {
  36. return nil, false
  37. }
  38. return val.Interface(), true
  39. }
  40. func (type2 *safeMapType) GetIndex(obj interface{}, key interface{}) interface{} {
  41. val := reflect.ValueOf(obj).Elem()
  42. keyVal := reflect.ValueOf(key).Elem()
  43. elemVal := val.MapIndex(keyVal)
  44. if !elemVal.IsValid() {
  45. ptr := reflect.New(reflect.PtrTo(val.Type().Elem()))
  46. return ptr.Elem().Interface()
  47. }
  48. ptr := reflect.New(elemVal.Type())
  49. ptr.Elem().Set(elemVal)
  50. return ptr.Interface()
  51. }
  52. func (type2 *safeMapType) UnsafeGetIndex(obj unsafe.Pointer, key unsafe.Pointer) unsafe.Pointer {
  53. panic("does not support unsafe operation")
  54. }
  55. func (type2 *safeMapType) Iterate(obj interface{}) MapIterator {
  56. m := reflect.ValueOf(obj).Elem()
  57. return &safeMapIterator{
  58. m: m,
  59. keys: m.MapKeys(),
  60. }
  61. }
  62. func (type2 *safeMapType) UnsafeIterate(obj unsafe.Pointer) MapIterator {
  63. panic("does not support unsafe operation")
  64. }
  65. type safeMapIterator struct {
  66. i int
  67. m reflect.Value
  68. keys []reflect.Value
  69. }
  70. func (iter *safeMapIterator) HasNext() bool {
  71. return iter.i != len(iter.keys)
  72. }
  73. func (iter *safeMapIterator) Next() (interface{}, interface{}) {
  74. key := iter.keys[iter.i]
  75. elem := iter.m.MapIndex(key)
  76. iter.i += 1
  77. keyPtr := reflect.New(key.Type())
  78. keyPtr.Elem().Set(key)
  79. elemPtr := reflect.New(elem.Type())
  80. elemPtr.Elem().Set(elem)
  81. return keyPtr.Interface(), elemPtr.Interface()
  82. }
  83. func (iter *safeMapIterator) UnsafeNext() (unsafe.Pointer, unsafe.Pointer) {
  84. panic("does not support unsafe operation")
  85. }