本文整理汇总了Golang中reflect.NewAt函数的典型用法代码示例。如果您正苦于以下问题:Golang NewAt函数的具体用法?Golang NewAt怎么用?Golang NewAt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewAt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: gobEncodeOpFor
// gobEncodeOpFor returns the op for a type that is known to implement
// GobEncoder.
func (enc *Encoder) gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
rt := ut.user
if ut.encIndir == -1 {
rt = reflect.PtrTo(rt)
} else if ut.encIndir > 0 {
for i := int8(0); i < ut.encIndir; i++ {
rt = rt.Elem()
}
}
var op encOp
op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
var v reflect.Value
if ut.encIndir == -1 {
// Need to climb up one level to turn value into pointer.
v = reflect.NewAt(rt, unsafe.Pointer(&p)).Elem()
} else {
v = reflect.NewAt(rt, p).Elem()
}
if !state.sendZero && isZero(v) {
return
}
state.update(i)
state.enc.encodeGobEncoder(state.b, ut, v)
}
return &op, int(ut.encIndir) // encIndir: op will get called with p == address of receiver.
}
示例2: gobDecodeOpFor
// gobDecodeOpFor returns the op for a type that is known to implement
// GobDecoder.
func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) (*decOp, int) {
rcvrType := ut.user
if ut.decIndir == -1 {
rcvrType = reflect.PtrTo(rcvrType)
} else if ut.decIndir > 0 {
for i := int8(0); i < ut.decIndir; i++ {
rcvrType = rcvrType.Elem()
}
}
var op decOp
op = func(i *decInstr, state *decoderState, p unsafe.Pointer) {
// Caller has gotten us to within one indirection of our value.
if i.indir > 0 {
if *(*unsafe.Pointer)(p) == nil {
*(*unsafe.Pointer)(p) = unsafe.Pointer(reflect.New(ut.base).Pointer())
}
}
// Now p is a pointer to the base type. Do we need to climb out to
// get to the receiver type?
var v reflect.Value
if ut.decIndir == -1 {
v = reflect.NewAt(rcvrType, unsafe.Pointer(&p)).Elem()
} else {
v = reflect.NewAt(rcvrType, p).Elem()
}
state.dec.decodeGobDecoder(ut, state, v)
}
return &op, int(ut.indir)
}
示例3: Call
func (func_data *FuncData) Call(args unsafe.Pointer, ret unsafe.Pointer) {
numIn := func_data.fn.Type().NumIn()
var goArgs []*uint32
get_c_slice(args, numIn, unsafe.Pointer(&goArgs))
getArg := func(index int) reflect.Value {
return reflect.NewAt(func_data.fn_type.In(index), unsafe.Pointer(goArgs[index])).Elem()
}
funArgs := make([]reflect.Value, numIn)
for i := 0; i < numIn; i++ {
funArgs[i] = getArg(i)
}
rets := func_data.fn.Call(funArgs)
numOut := func_data.fn_type.NumOut()
if numOut == 1 {
reflect.NewAt(func_data.fn_type.Out(0), ret).Elem().Set(rets[0])
}
}
示例4: valRepr
func valRepr(s *gosym.Sym, typ reflect.Type, values []string, _html bool) (r string) {
val, _ := strconv.ParseUint(values[0], 0, 64)
var val2 uint64
if len(values) > 1 {
val2, _ = strconv.ParseUint(values[1], 0, 64)
}
// If there's a panic prettyfy'ing the value just
// assume it's a pointer. It's better than
// omitting the error page.
defer func() {
if recover() != nil {
r = pointerRepr(nil, val, false)
}
}()
switch types.Kind(typ.Kind()) {
case types.Bool:
if val == 0 {
return "= false"
}
return "= true"
case types.Int:
return "= " + strconv.FormatInt(int64(val), 10)
case types.Uint:
return "= " + strconv.FormatUint(val, 10)
case types.Float:
if typ.Kind() == reflect.Float32 {
return "= " + strconv.FormatFloat(float64(math.Float32frombits(uint32(val))), 'g', -1, 32)
}
return "= " + strconv.FormatFloat(math.Float64frombits(uint64(val)), 'g', -1, 64)
case types.Slice:
return sliceRepr(val, val2, s)
case types.String:
v := stringRepr(val, val2)
if _html {
v = html.Escape(v)
}
return v
case types.Interface:
if typ.NumMethod() == 0 {
return emptyInterfaceRepr(val, val2)
}
idata := [2]uintptr{uintptr(val), uintptr(val2)}
v := reflect.NewAt(typ, unsafe.Pointer(&idata[0])).Elem()
return descRepr(val, &v, _html)
case types.Func:
fn := reflect.NewAt(typ, unsafe.Pointer(&val)).Elem()
f := runtime.FuncForPC(fn.Pointer())
if f != nil {
return "= " + f.Name()
}
}
return pointerRepr(typ, val, _html)
}
示例5: deserialize
func (c *RedisCacher) deserialize(byt []byte) (ptr interface{}, err error) {
b := bytes.NewBuffer(byt)
decoder := gob.NewDecoder(b)
var p interface{}
err = decoder.Decode(&p)
if err != nil {
c.logErrf("decode failed: %v", err)
return
}
v := reflect.ValueOf(p)
c.logDebugf("deserialize type:%v", v.Type())
if v.Kind() == reflect.Struct {
var pp interface{} = &p
datas := reflect.ValueOf(pp).Elem().InterfaceData()
sp := reflect.NewAt(v.Type(),
unsafe.Pointer(datas[1])).Interface()
ptr = sp
vv := reflect.ValueOf(ptr)
c.logDebugf("deserialize convert ptr type:%v | CanAddr:%t", vv.Type(), vv.CanAddr())
} else {
ptr = p
}
return
}
示例6: goMInit
//export goMInit
func goMInit(db, pClientData unsafe.Pointer, argc int, argv **C.char, pzErr **C.char, isCreate int) unsafe.Pointer {
m := (*sqliteModule)(pClientData)
if m.c.db != (*C.sqlite3)(db) {
*pzErr = mPrintf("%s", "Inconsistent db handles")
return nil
}
args := make([]string, argc)
var A []*C.char
slice := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(argv)), Len: argc, Cap: argc}
a := reflect.NewAt(reflect.TypeOf(A), unsafe.Pointer(&slice)).Elem().Interface()
for i, s := range a.([]*C.char) {
args[i] = C.GoString(s)
}
var vTab VTab
var err error
if isCreate == 1 {
vTab, err = m.module.Create(m.c, args)
} else {
vTab, err = m.module.Connect(m.c, args)
}
if err != nil {
*pzErr = mPrintf("%s", err.Error())
return nil
}
vt := &sqliteVTab{m, vTab, nil}
// prevents 'vt' from being gced
if m.vts == nil {
m.vts = make(map[*sqliteVTab]bool)
}
m.vts[vt] = true
*pzErr = nil
return unsafe.Pointer(vt)
}
示例7: Slice_
func (m *GslMultiset) Slice_() interface{} {
baseType := gogsl.GOGSL_SIZE_T_TYPE
sliceType := reflect.SliceOf(baseType)
size := K(m)
hdr := &reflect.SliceHeader{Len: size, Cap: size, Data: uintptr(C.get_multiset_data((*C.gsl_multiset)(unsafe.Pointer(m.Ptr()))))}
return reflect.NewAt(sliceType, unsafe.Pointer(hdr)).Elem().Interface()
}
示例8: closureMarshal
//export closureMarshal
func closureMarshal(closure *C.GClosure, ret *C.GValue, nParams C.guint, params *C.GValue, hint, data C.gpointer) {
// callback value
f := *((*interface{})(unsafe.Pointer(data)))
fValue := reflect.ValueOf(f)
fType := fValue.Type()
if int(nParams) != fType.NumIn() {
log.Fatal("number of parameters and arguments mismatch")
}
// convert GValue to reflect.Value
var paramSlice []C.GValue
h := (*reflect.SliceHeader)(unsafe.Pointer(¶mSlice))
h.Len = int(nParams)
h.Cap = h.Len
h.Data = uintptr(unsafe.Pointer(params))
var arguments []reflect.Value
for i, gv := range paramSlice {
goValue := fromGValue(&gv)
var arg reflect.Value
switch fType.In(i).Kind() {
case reflect.Ptr:
p := goValue.(unsafe.Pointer)
arg = reflect.NewAt(fType.In(i), unsafe.Pointer(&p)).Elem()
default:
panic("FIXME") //TODO
}
arguments = append(arguments, arg)
}
// call
fValue.Call(arguments[:fType.NumIn()])
//TODO set return value
}
示例9: sliceEncoder
func sliceEncoder(typ reflect.Type) (typeEncoder, error) {
switch typ.Elem().Kind() {
case reflect.Int8, reflect.Uint8, reflect.Int16, reflect.Uint16, reflect.Int32, reflect.Uint32, reflect.Int64, reflect.Uint64:
// Take advantage of the fast path in Write
return func(enc *encoder, p unsafe.Pointer) error {
v := reflect.NewAt(typ, p).Elem()
return Write(enc, enc.order, v.Interface())
}, nil
}
etyp := typ.Elem()
eenc, err := makeEncoder(etyp)
if err != nil {
return nil, err
}
s := etyp.Size()
return func(enc *encoder, p unsafe.Pointer) error {
h := (*reflect.SliceHeader)(p)
ep := unsafe.Pointer(h.Data)
for ii := 0; ii < h.Len; ii++ {
if err := eenc(enc, ep); err != nil {
return err
}
}
ep = unsafe.Pointer(uintptr(ep) + s)
return nil
}, nil
}
示例10: 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
}
示例11: scanSubCommandHandler
func (c *Command) scanSubCommandHandler(parentg *Group) scanHandler {
f := func(realval reflect.Value, sfield *reflect.StructField) (bool, error) {
mtag := newMultiTag(string(sfield.Tag))
if err := mtag.Parse(); err != nil {
return true, err
}
subcommand := mtag.Get("command")
if len(subcommand) != 0 {
ptrval := reflect.NewAt(realval.Type(), unsafe.Pointer(realval.UnsafeAddr()))
shortDescription := mtag.Get("description")
longDescription := mtag.Get("long-description")
subcommandsOptional := mtag.Get("subcommands-optional")
subc, err := c.AddCommand(subcommand, shortDescription, longDescription, ptrval.Interface())
if err != nil {
return true, err
}
if len(subcommandsOptional) > 0 {
subc.SubcommandsOptional = true
}
return true, nil
}
return parentg.scanSubGroupHandler(realval, sfield)
}
return f
}
示例12: dec_struct_message
// Decode an embedded message.
func (o *Buffer) dec_struct_message(p *Properties, base uintptr) (err error) {
raw, e := o.DecodeRawBytes(false)
if e != nil {
return e
}
ptr := (**struct{})(unsafe.Pointer(base + p.offset))
typ := p.stype.Elem()
bas := reflect.New(typ).Pointer()
structp := unsafe.Pointer(bas)
*ptr = (*struct{})(structp)
// If the object can unmarshal itself, let it.
if p.isMarshaler {
iv := reflect.NewAt(p.stype.Elem(), structp).Interface()
return iv.(Unmarshaler).Unmarshal(raw)
}
obuf := o.buf
oi := o.index
o.buf = raw
o.index = 0
err = o.unmarshalType(p.stype, p.sprop, false, bas)
o.buf = obuf
o.index = oi
return err
}
示例13: gslNtupleValueFunctionCaller
//export gslNtupleValueFunctionCaller
func gslNtupleValueFunctionCaller(x uintptr, cFunParamPtr uintptr) float64 {
ntsf := (*GslNtupleValueFunction)(unsafe.Pointer(cFunParamPtr))
hdr := &reflect.SliceHeader{Data: x, Len: ntsf.ntuple.refLen, Cap: ntsf.ntuple.refLen}
sliceType := reflect.SliceOf(ntsf.ntuple.baseType)
slice := reflect.NewAt(sliceType, unsafe.Pointer(hdr)).Elem().Interface()
return ntsf.Function(slice, ntsf.Params)
}
示例14: gslSimanCopyConstructorCaller
//export gslSimanCopyConstructorCaller
func gslSimanCopyConstructorCaller(xptr uintptr) uintptr {
//fmt.Println("CC")
xarg := (*GslSimanArgument)(unsafe.Pointer(xptr))
if xarg.impl.ctorFn != nil {
nptr := xarg.impl.ctorFn(xarg.x)
rv := &GslSimanArgument{impl: xarg.impl, x: nptr}
rvptr := uintptr(unsafe.Pointer(rv))
xarg.impl.holdRefs[rvptr] = rv
//fmt.Printf("CC. RV x is %s inval=%f, outval=%f\n", reflect.TypeOf(rv.x).String(), *xarg.x.(*float64), *rv.x.(*float64))
return rvptr
} else {
var rv *GslSimanArgument
x := reflect.ValueOf(xarg.x)
xType := x.Type()
baseType := xType.Elem()
if xType.Kind() == reflect.Ptr {
nxptr := reflect.New(baseType)
//C.memcpy(unsafe.Pointer(nxptr), unsafe.Pointer(x.Pointer()), C.size_t(baseType.Size()))
nxptr.Elem().Set(x.Elem())
rv = &GslSimanArgument{impl: xarg.impl, x: reflect.NewAt(baseType, unsafe.Pointer(nxptr.Pointer())).Interface()}
} else {
panic("fail")
rv = &GslSimanArgument{impl: xarg.impl, x: xarg.x}
}
//fmt.Printf("CC RV x is %s inval=%f, outval=%f\n", reflect.TypeOf(rv.x).String(), *xarg.x.(*float64), *rv.x.(*float64))
rvptr := uintptr(unsafe.Pointer(rv))
xarg.impl.holdRefs[rvptr] = rv
return rvptr
}
}
示例15: UnsafeReflectValue
// UnsafeReflectValue converts the passed reflect.Value into a one that bypasses
// the typical safety restrictions preventing access to unaddressable and
// unexported data. It works by digging the raw pointer to the underlying
// value out of the protected value and generating a new unprotected (unsafe)
// reflect.Value to it.
//
// This allows us to check for implementations of the Stringer and error
// interfaces to be used for pretty printing ordinarily unaddressable and
// inaccessible values such as unexported struct fields.
func UnsafeReflectValue(v reflect.Value) (rv reflect.Value) {
indirects := 1
vt := v.Type()
upv := unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetPtr)
rvf := *(*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(&v)) + offsetFlag))
if rvf&flagIndir != 0 {
vt = reflect.PtrTo(v.Type())
indirects++
} else if offsetScalar != 0 {
// The value is in the scalar field when it's not one of the
// reference types.
switch vt.Kind() {
case reflect.Uintptr:
case reflect.Chan:
case reflect.Func:
case reflect.Map:
case reflect.Ptr:
case reflect.UnsafePointer:
default:
upv = unsafe.Pointer(uintptr(unsafe.Pointer(&v)) +
offsetScalar)
}
}
pv := reflect.NewAt(vt, upv)
rv = pv
for i := 0; i < indirects; i++ {
rv = rv.Elem()
}
return rv
}