1
0
mirror of https://github.com/golang/go synced 2024-11-14 08:20:21 -07:00

reflect.PtrValue.SetSub() to set pointers

R=rsc
OCL=19101
CL=19101
This commit is contained in:
Rob Pike 2008-11-12 14:19:39 -08:00
parent 2f4d35ffb9
commit b1d37b74d9
2 changed files with 21 additions and 0 deletions

View File

@ -91,6 +91,17 @@ func main() {
var s string; var s string;
var t reflect.Type; var t reflect.Type;
{
var ip *int32;
var i int32 = 1234;
vip := reflect.NewValue(&ip);
vi := reflect.NewValue(i);
vip.(reflect.PtrValue).Sub().(reflect.PtrValue).SetSub(vi);
if *ip != 1234 {
panicln("SetSub failure", *ip);
}
}
// Types // Types
typedump("missing", "$missing$"); typedump("missing", "$missing$");
typedump("int", "int"); typedump("int", "int");

View File

@ -39,6 +39,7 @@ export type Empty interface {} // TODO(r): Delete when no longer needed?
export type Value interface { export type Value interface {
Kind() int; Kind() int;
Type() Type; Type() Type;
Addr() Addr;
Interface() Empty; Interface() Empty;
} }
@ -58,6 +59,10 @@ func (c *Common) Type() Type {
return c.typ return c.typ
} }
func (c *Common) Addr() Addr {
return c.addr
}
func (c *Common) Interface() Empty { func (c *Common) Interface() Empty {
return sys.unreflect(*AddrToPtrAddr(c.addr), c.typ.String()); return sys.unreflect(*AddrToPtrAddr(c.addr), c.typ.String());
} }
@ -493,6 +498,7 @@ export type PtrValue interface {
Type() Type; Type() Type;
Sub() Value; Sub() Value;
Get() Addr; Get() Addr;
SetSub(Value);
} }
type PtrValueStruct struct { type PtrValueStruct struct {
@ -507,6 +513,10 @@ func (v *PtrValueStruct) Sub() Value {
return NewValueAddr(v.typ.(PtrType).Sub(), v.Get()); return NewValueAddr(v.typ.(PtrType).Sub(), v.Get());
} }
func (v *PtrValueStruct) SetSub(subv Value) {
*AddrToPtrAddr(v.addr) = subv.Addr();
}
func PtrCreator(typ Type, addr Addr) Value { func PtrCreator(typ Type, addr Addr) Value {
return &PtrValueStruct{ Common{PtrKind, typ, addr} }; return &PtrValueStruct{ Common{PtrKind, typ, addr} };
} }