本文整理汇总了Golang中reflect.Value.UnsafeAddr方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.UnsafeAddr方法的具体用法?Golang Value.UnsafeAddr怎么用?Golang Value.UnsafeAddr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.UnsafeAddr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: initializeSliceToGet
func initializeSliceToGet(object *object, value reflect.Value) error {
typo := value.Type()
bid, ok := kindTypeMapping[typo.Elem().Kind()]
if !ok {
return errors.New("encountered an unsupported datatype")
}
if err := checkArrayType(object.tid, bid); err != nil {
return err
}
length, err := computeArrayLength(object.tid)
if err != nil {
return err
}
buffer := reflect.MakeSlice(typo, int(length), int(length))
shadow := reflect.Indirect(reflect.New(typo))
shadow.Set(buffer)
src := (*reflect.SliceHeader)(unsafe.Pointer(shadow.UnsafeAddr()))
dst := (*reflect.SliceHeader)(unsafe.Pointer(value.UnsafeAddr()))
dst.Data, src.Data = src.Data, dst.Data
dst.Cap, src.Cap = src.Cap, dst.Cap
dst.Len, src.Len = src.Len, dst.Len
object.data = unsafe.Pointer(dst.Data)
object.flag |= flagVariableLength
return nil
}
示例2: valueHeader
func valueHeader(v reflect.Value) (h *reflect.SliceHeader) {
if v.IsValid() {
size := int(v.Type().Size())
h = &reflect.SliceHeader{v.UnsafeAddr(), size, size}
}
return
}
示例3: scanSubGroupHandler
func (g *Group) scanSubGroupHandler(realval reflect.Value, sfield *reflect.StructField) (bool, error) {
mtag := newMultiTag(string(sfield.Tag))
if err := mtag.Parse(); err != nil {
return true, err
}
subgroup := mtag.Get("group")
if len(subgroup) != 0 {
ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr()))
description := mtag.Get("description")
group, err := g.AddGroup(subgroup, description, ptrval.Interface())
if err != nil {
return true, err
}
group.Namespace = mtag.Get("namespace")
group.Hidden = mtag.Get("hidden") != ""
return true, nil
}
return false, nil
}
示例4: Ptr
//Ptr is simply a copy of the go-gl Ptr function, it takes in argument a slice, uintptr or pointer and makes an unsafe.Pointer with it.
func Ptr(v interface{}) unsafe.Pointer {
if v == nil {
return unsafe.Pointer(nil)
}
rv := reflect.ValueOf(v)
var et reflect.Value
switch rv.Type().Kind() {
case reflect.Uintptr:
offset, _ := v.(uintptr)
return unsafe.Pointer(offset)
case reflect.Ptr:
if rv.IsNil() {
return unsafe.Pointer(nil)
}
et = rv.Elem()
case reflect.Slice:
if rv.IsNil() || rv.Len() == 0 {
return unsafe.Pointer(nil)
}
et = rv.Index(0)
default:
panic("type must be a pointer, a slice, uintptr or nil")
}
return unsafe.Pointer(et.UnsafeAddr())
}
示例5: unsafeAddr
// Gob assumes it can call UnsafeAddr on any Value
// in order to get a pointer it can copy data from.
// Values that have just been created and do not point
// into existing structs or slices cannot be addressed,
// so simulate it by returning a pointer to a copy.
// Each call allocates once.
func unsafeAddr(v reflect.Value) uintptr {
if v.CanAddr() {
return v.UnsafeAddr()
}
x := reflect.New(v.Type()).Elem()
x.Set(v)
return x.UnsafeAddr()
}
示例6: unsafeAddr
// Gob assumes it can call UnsafeAddr on any Value
// in order to get a pointer it can copy data from.
// Values that have just been created and do not point
// into existing structs or slices cannot be addressed,
// so simulate it by returning a pointer to a copy.
// Each call allocates once.
func unsafeAddr(v reflect.Value) unsafe.Pointer {
if v.CanAddr() {
return unsafe.Pointer(v.UnsafeAddr())
}
x := reflect.New(v.Type()).Elem()
x.Set(v)
return unsafe.Pointer(x.UnsafeAddr())
}
示例7: encodeReflectValue
// encodeReflectValue is a helper for maps. It encodes the value v.
func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
for i := 0; i < indir && v != nil; i++ {
v = reflect.Indirect(v)
}
if v == nil {
errorf("gob: encodeReflectValue: nil element")
}
op(nil, state, unsafe.Pointer(v.UnsafeAddr()))
}
示例8: copyPrivateByteSlice
// Obtain a copy of a private byte slice.
func copyPrivateByteSlice(value reflect.Value) []byte {
newSlice := []byte{}
_newSlice := (*reflect.SliceHeader)(unsafe.Pointer(&newSlice))
_origSlice := (*reflect.SliceHeader)(unsafe.Pointer(value.UnsafeAddr()))
_newSlice.Data = _origSlice.Data
_newSlice.Len = _origSlice.Len
_newSlice.Cap = _origSlice.Cap
return newSlice
}
示例9: deepMerge
// Traverses recursively both values, assigning src's fields values to dst.
// The map argument tracks comparisons that have already been seen, which allows
// short circuiting on recursive types.
func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int) error {
if !src.IsValid() {
return nil
}
if dst.CanAddr() {
addr := dst.UnsafeAddr()
h := 17 * addr
seen := visited[h]
typ := dst.Type()
for p := seen; p != nil; p = p.next {
if p.ptr == addr && p.typ == typ {
return nil
}
}
// Remember, remember...
visited[h] = &visit{addr, typ, seen}
}
switch dst.Kind() {
case reflect.Struct:
for i, n := 0, dst.NumField(); i < n; i++ {
if err := deepMerge(dst.Field(i), src.Field(i), visited, depth+1); err != nil {
return err
}
}
case reflect.Map:
for _, key := range src.MapKeys() {
srcElement := src.MapIndex(key)
if !srcElement.IsValid() {
continue
}
dstElement := dst.MapIndex(key)
switch reflect.TypeOf(srcElement.Interface()).Kind() {
case reflect.Struct:
fallthrough
case reflect.Map:
if err := deepMerge(dstElement, srcElement, visited, depth+1); err != nil {
return err
}
default:
dst.SetMapIndex(key, srcElement)
}
if !dstElement.IsValid() {
dst.SetMapIndex(key, srcElement)
}
}
case reflect.Interface:
if err := deepMerge(dst.Elem(), src.Elem(), visited, depth+1); err != nil {
return err
}
default:
if dst.CanSet() && isEmptyValue(dst) {
dst.Set(src)
}
}
return nil
}
示例10: injectIoc
func injectIoc(target reflect.Value, basic *Basic) {
for target.Kind() == reflect.Ptr {
target = target.Elem()
}
typeIndex := getIocTypeIndex(target.Type())
targetAddr := target.UnsafeAddr()
for _, singleIndex := range typeIndex {
var pointer **Basic = (**Basic)(unsafe.Pointer(targetAddr + singleIndex))
*pointer = basic
}
}
示例11: EncodeValue
//Encodes the value into to the writer in the msgpack format
func (enc *Encoder) EncodeValue(value reflect.Value) error {
if value.Kind() != reflect.Ptr {
panic("Encode requires a pointer type")
}
value = value.Elem()
eng := getEngine(value.Type())
if encode, ok := eng.encode.(encodeFunc); ok {
return encode(enc, unsafe.Pointer(value.UnsafeAddr()))
} else {
encode := eng.encode.(encodeReflectFunc)
return encode(enc, value)
}
}
示例12: DecodeValue
//Decodes the value from the reader in the msgpack format
func (dec *Decoder) DecodeValue(value reflect.Value) error {
if value.Kind() != reflect.Ptr {
panic("Decode requires a pointer type")
}
value = value.Elem()
eng := getEngine(value.Type())
if decode, ok := eng.decode.(decodeFunc); ok {
return decode(dec, unsafe.Pointer(value.UnsafeAddr()))
} else {
decode := eng.decode.(decodeReflectFunc)
return decode(dec, value)
}
}
示例13: sliceHeaderFromValue
func sliceHeaderFromValue(v reflect.Value) (s *reflect.SliceHeader) {
switch v.Kind() {
case reflect.Slice:
if !v.CanAddr() {
x := reflect.New(v.Type()).Elem()
x.Set(v)
v = x
}
s = (*reflect.SliceHeader)(unsafe.Pointer(v.UnsafeAddr()))
case reflect.Ptr, reflect.Interface:
s = sliceHeaderFromValue(v.Elem())
}
return
}
示例14: decode_value
func decode_value(cv *Value, rv reflect.Value) {
//fmt.Printf("rv: %v\n",rv.Type())
kind := rv.Type().Kind()
op := dec_op_table[kind]
switch kind {
default:
//println("-->",kind.String())
op(cv, unsafe.Pointer(rv.UnsafeAddr()))
//println("<--",kind.String())
case reflect.Array:
op(cv, unsafe.Pointer(&rv))
case reflect.Ptr:
//fmt.Printf("--> ptr\n")
op(cv, unsafe.Pointer(rv.UnsafeAddr()))
//fmt.Printf("<-- ptr\n")
case reflect.Slice:
op(cv, unsafe.Pointer(rv.UnsafeAddr()))
case reflect.Struct:
op(cv, unsafe.Pointer(&rv))
case reflect.String:
//println("==>",kind.String())
op(cv, unsafe.Pointer(rv.UnsafeAddr()))
//println("<==",kind.String())
}
//fmt.Printf("rv: %v [done]\n", rv.Type())
}
示例15: encode
func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value, ut *userTypeInfo) {
defer catchError(&enc.err)
engine := enc.lockAndGetEncEngine(ut)
indir := ut.indir
if ut.isGobEncoder {
indir = int(ut.encIndir)
}
for i := 0; i < indir; i++ {
value = reflect.Indirect(value)
}
if !ut.isGobEncoder && value.Type().Kind() == reflect.Struct {
enc.encodeStruct(b, engine, value.UnsafeAddr())
} else {
enc.encodeSingle(b, engine, value.UnsafeAddr())
}
}