2018-02-15 17:26:30 +08:00
|
|
|
package vars
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/elves/elvish/eval/vals"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ptr struct {
|
|
|
|
ptr interface{}
|
|
|
|
}
|
|
|
|
|
2018-03-08 21:20:31 +08:00
|
|
|
// FromPtr creates a variable from a pointer. The variable is kept in sync
|
2018-02-15 17:26:30 +08:00
|
|
|
// with the value the pointer points to, using elvToGo and goToElv conversions
|
|
|
|
// when Get and Set.
|
2018-03-08 21:20:31 +08:00
|
|
|
func FromPtr(p interface{}) Var {
|
2018-02-15 17:26:30 +08:00
|
|
|
return ptr{p}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the value pointed by the pointer, after conversion using FromGo.
|
|
|
|
func (v ptr) Get() interface{} {
|
|
|
|
return vals.FromGo(reflect.Indirect(reflect.ValueOf(v.ptr)).Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get sets the value pointed by the pointer, after conversion using ScanToGo.
|
|
|
|
func (v ptr) Set(val interface{}) error {
|
|
|
|
return vals.ScanToGo(val, v.ptr)
|
|
|
|
}
|