1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-12-21 00:21:15 +00:00
sii/registry.go

34 lines
598 B
Go
Raw Permalink Normal View History

package sii
import (
"reflect"
"sync"
)
var (
blockClass = map[string]reflect.Type{}
blockClassLock = new(sync.RWMutex)
defaultBlockType = reflect.TypeOf(RawBlock{})
)
func RegisterBlock(b Block) {
blockClassLock.Lock()
defer blockClassLock.Unlock()
blockClass[b.Class()] = reflect.TypeOf(b).Elem()
}
func getBlockInstance(t string) Block {
blockClassLock.RLock()
defer blockClassLock.RUnlock()
if rt, ok := blockClass[t]; ok {
v := reflect.New(rt).Interface()
if b, ok := v.(Block); ok {
return b
}
}
return reflect.New(defaultBlockType).Interface().(Block)
}