github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/reflectlite/value.go (about) 1 // Copyright 2009 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 5 package reflectlite 6 7 import ( 8 "github.com/shogo82148/std/internal/abi" 9 "github.com/shogo82148/std/unsafe" 10 ) 11 12 // Value is the reflection interface to a Go value. 13 // 14 // Not all methods apply to all kinds of values. Restrictions, 15 // if any, are noted in the documentation for each method. 16 // Use the Kind method to find out the kind of value before 17 // calling kind-specific methods. Calling a method 18 // inappropriate to the kind of type causes a run time panic. 19 // 20 // The zero Value represents no value. 21 // Its IsValid method returns false, its Kind method returns Invalid, 22 // its String method returns "<invalid Value>", and all other methods panic. 23 // Most functions and methods never return an invalid value. 24 // If one does, its documentation states the conditions explicitly. 25 // 26 // A Value can be used concurrently by multiple goroutines provided that 27 // the underlying Go value can be used concurrently for the equivalent 28 // direct operations. 29 // 30 // To compare two Values, compare the results of the Interface method. 31 // Using == on two Values does not compare the underlying values 32 // they represent. 33 type Value struct { 34 // typ_ holds the type of the value represented by a Value. 35 // Access using the typ method to avoid escape of v. 36 typ_ *abi.Type 37 38 // Pointer-valued data or, if flagIndir is set, pointer to data. 39 // Valid when either flagIndir is set or typ.pointers() is true. 40 ptr unsafe.Pointer 41 42 // flag holds metadata about the value. 43 // The lowest bits are flag bits: 44 // - flagStickyRO: obtained via unexported not embedded field, so read-only 45 // - flagEmbedRO: obtained via unexported embedded field, so read-only 46 // - flagIndir: val holds a pointer to the data 47 // - flagAddr: v.CanAddr is true (implies flagIndir) 48 // Value cannot represent method values. 49 // The next five bits give the Kind of the value. 50 // This repeats typ.Kind() except for method values. 51 // The remaining 23+ bits give a method number for method values. 52 // If flag.kind() != Func, code can assume that flagMethod is unset. 53 // If ifaceIndir(typ), code can assume that flagIndir is set. 54 flag 55 } 56 57 // A ValueError occurs when a Value method is invoked on 58 // a Value that does not support it. Such cases are documented 59 // in the description of each method. 60 type ValueError struct { 61 Method string 62 Kind Kind 63 } 64 65 func (e *ValueError) Error() string 66 67 // CanSet reports whether the value of v can be changed. 68 // A Value can be changed only if it is addressable and was not 69 // obtained by the use of unexported struct fields. 70 // If CanSet returns false, calling Set or any type-specific 71 // setter (e.g., SetBool, SetInt) will panic. 72 func (v Value) CanSet() bool 73 74 // Elem returns the value that the interface v contains 75 // or that the pointer v points to. 76 // It panics if v's Kind is not Interface or Pointer. 77 // It returns the zero Value if v is nil. 78 func (v Value) Elem() Value 79 80 // IsNil reports whether its argument v is nil. The argument must be 81 // a chan, func, interface, map, pointer, or slice value; if it is 82 // not, IsNil panics. Note that IsNil is not always equivalent to a 83 // regular comparison with nil in Go. For example, if v was created 84 // by calling ValueOf with an uninitialized interface variable i, 85 // i==nil will be true but v.IsNil will panic as v will be the zero 86 // Value. 87 func (v Value) IsNil() bool 88 89 // IsValid reports whether v represents a value. 90 // It returns false if v is the zero Value. 91 // If IsValid returns false, all other methods except String panic. 92 // Most functions and methods never return an invalid Value. 93 // If one does, its documentation states the conditions explicitly. 94 func (v Value) IsValid() bool 95 96 // Kind returns v's Kind. 97 // If v is the zero Value (IsValid returns false), Kind returns Invalid. 98 func (v Value) Kind() Kind 99 100 // Len returns v's length. 101 // It panics if v's Kind is not Array, Chan, Map, Slice, or String. 102 func (v Value) Len() int 103 104 // Set assigns x to the value v. 105 // It panics if CanSet returns false. 106 // As in Go, x's value must be assignable to v's type. 107 func (v Value) Set(x Value) 108 109 // Type returns v's type. 110 func (v Value) Type() Type 111 112 // ValueOf returns a new Value initialized to the concrete value 113 // stored in the interface i. ValueOf(nil) returns the zero Value. 114 func ValueOf(i any) Value