-
Notifications
You must be signed in to change notification settings - Fork 0
/
SliceRef.go
69 lines (59 loc) · 1.6 KB
/
SliceRef.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package zeta
import (
"reflect"
)
type SliceRef struct {
// A slice pointer.
Ref interface{}
refValue reflect.Value
sliceValue reflect.Value
sliceType reflect.Type
eleType reflect.Type
}
func NewSliceRef(ref interface{}) *SliceRef {
r := &SliceRef{Ref: ref, refValue: reflect.ValueOf(ref)}
r.sliceValue = reflect.Indirect(r.refValue)
r.sliceType = r.sliceValue.Type()
r.eleType = r.sliceType.Elem()
return r
}
func (s *SliceRef) New(len, cap int) *SliceRef {
entity := reflect.MakeSlice(s.sliceType, len, cap)
s.sliceValue.Set(entity)
return s
}
func (s *SliceRef) CreateInstance(len, cap int) interface{} {
refValue := reflect.MakeSlice(s.sliceType, len, cap)
return refValue.Interface()
// r := &SliceRef{
// Ref: ref,
// refValue: refValue,
// sliceValue: reflect.Indirect(refValue),
// }
// r.sliceType = r.sliceValue.Type()
// r.eleType = r.sliceType.Elem()
// return r
}
func (s *SliceRef) GetRef(i int) interface{} {
return s.sliceValue.Index(i).Addr().Interface()
}
func (s *SliceRef) Set(i int, value interface{}) {
s.sliceValue.Index(i).Set(reflect.ValueOf(value))
}
func (s *SliceRef) Get(i int) interface{} {
return s.sliceValue.Index(i).Interface()
}
func (s *SliceRef) GetInto(i int, valueRef interface{}) {
reflect.Indirect(reflect.ValueOf(valueRef)).Set(s.sliceValue.Index(i))
}
func (s *SliceRef) Len() int {
if s.Ref == nil {
return 0
}
return s.sliceValue.Len()
}
func (s *SliceRef) Append(value interface{}) *SliceRef {
s.sliceValue = reflect.Append(s.sliceValue, reflect.ValueOf(value))
reflect.Indirect(s.refValue).Set(s.sliceValue)
return s
}