當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Value.UnsafeAddr方法代碼示例

本文整理匯總了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
}
開發者ID:postfix,項目名稱:hdf5,代碼行數:33,代碼來源:get.go

示例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
}
開發者ID:pseudomind,項目名稱:go-opencl,代碼行數:7,代碼來源:byte_slice.go

示例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
}
開發者ID:constabulary,項目名稱:docker-depfile-example,代碼行數:26,代碼來源:group.go

示例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())
}
開發者ID:peterudkmaya11,項目名稱:lux,代碼行數:29,代碼來源:freaky.go

示例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()
}
開發者ID:anuvazhayil,項目名稱:HelloWorld_32bitOS,代碼行數:14,代碼來源:decode.go

示例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())
}
開發者ID:jtratner,項目名稱:unescaped-go-json,代碼行數:14,代碼來源:decode.go

示例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()))
}
開發者ID:richlowe,項目名稱:gcc,代碼行數:10,代碼來源:encode.go

示例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
}
開發者ID:zhoudianyou,項目名稱:zerocopy,代碼行數:10,代碼來源:zerocopy.go

示例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
}
開發者ID:rusnak,項目名稱:mergo,代碼行數:59,代碼來源:mergo.go

示例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
	}
}
開發者ID:fishedee,項目名稱:fishgo,代碼行數:11,代碼來源:ioc.go

示例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)
	}
}
開發者ID:experimental-games,項目名稱:netherrack,代碼行數:15,代碼來源:encoder.go

示例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)
	}
}
開發者ID:experimental-games,項目名稱:netherrack,代碼行數:15,代碼來源:decoder.go

示例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
}
開發者ID:pseudomind,項目名稱:go-opencl,代碼行數:14,代碼來源:reslice.go

示例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())
}
開發者ID:sbinet,項目名稱:go-ctypes,代碼行數:26,代碼來源:ctypes.go

示例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())
	}
}
開發者ID:richlowe,項目名稱:gcc,代碼行數:16,代碼來源:encode.go


注:本文中的reflect.Value.UnsafeAddr方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。