forked from Arachnid/evmdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typemap.go
37 lines (31 loc) · 817 Bytes
/
typemap.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
package evmdis
import (
"reflect"
)
type TypeMap struct {
data map[reflect.Type]interface{}
}
func (self *TypeMap) Get(obj interface{}) {
element := reflect.ValueOf(obj).Elem()
if value, ok := self.data[element.Type()]; ok {
element.Set(reflect.ValueOf(value))
} else {
element.Set(reflect.Zero(element.Type()))
}
}
func (self *TypeMap) Pop(obj interface{}) {
element := reflect.ValueOf(obj).Elem()
if value, ok := self.data[element.Type()]; ok {
element.Set(reflect.ValueOf(value))
delete(self.data, element.Type())
} else {
element.Set(reflect.Zero(element.Type()))
}
}
func (self *TypeMap) Set(obj interface{}) {
element := reflect.ValueOf(obj).Elem()
self.data[element.Type()] = element.Interface()
}
func NewTypeMap() *TypeMap {
return &TypeMap{make(map[reflect.Type]interface{})}
}