forked from hprose/hprose-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_manager.go
75 lines (66 loc) · 2.34 KB
/
class_manager.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
70
71
72
73
74
75
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/class_manager.go *
* *
* hprose ClassManager for Go. *
* *
* LastModified: Mar 27, 2014 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package hprose
import (
"reflect"
"sync"
)
type classManager struct {
classCache map[string]reflect.Type
aliasCache map[reflect.Type]string
tagCache map[reflect.Type]string
mutex *sync.Mutex
}
// Register class with alias.
func (cm *classManager) Register(class reflect.Type, alias string, tag ...string) {
cm.mutex.Lock()
cm.classCache[alias] = class
cm.aliasCache[class] = alias
if len(tag) == 1 {
cm.tagCache[class] = tag[0]
}
cm.mutex.Unlock()
}
// GetClassAlias by class.
func (cm *classManager) GetClassAlias(class reflect.Type) (alias string) {
cm.mutex.Lock()
alias = cm.aliasCache[class]
cm.mutex.Unlock()
return alias
}
// GetClass by alias.
func (cm *classManager) GetClass(alias string) (class reflect.Type) {
cm.mutex.Lock()
class = cm.classCache[alias]
cm.mutex.Unlock()
return class
}
// GetTag by class.
func (cm *classManager) GetTag(class reflect.Type) (tag string) {
cm.mutex.Lock()
tag = cm.tagCache[class]
cm.mutex.Unlock()
return tag
}
func initClassManager() *classManager {
cm := classManager{make(map[string]reflect.Type), make(map[reflect.Type]string), make(map[reflect.Type]string), new(sync.Mutex)}
return &cm
}
// ClassManager used to be register class with alias for hprose serialize/unserialize.
var ClassManager = initClassManager()