-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregistry.go
More file actions
160 lines (136 loc) · 3.73 KB
/
Copy pathregistry.go
File metadata and controls
160 lines (136 loc) · 3.73 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gpucontext
import "sync"
// Registry provides thread-safe registration and lookup of named factories.
// It supports priority-based selection when multiple implementations exist.
//
// Type parameter T is the type returned by factories.
//
// Example:
//
// var backends = gpucontext.NewRegistry[Backend](
// gpucontext.WithPriority("vulkan", "dx12", "metal", "gles", "software"),
// )
//
// backends.Register("vulkan", func() Backend { return NewVulkanBackend() })
// backends.Register("software", func() Backend { return NewSoftwareBackend() })
//
// best := backends.Best() // Returns vulkan if available, otherwise software
type Registry[T any] struct {
mu sync.RWMutex
factories map[string]func() T
priority []string
}
// RegistryOption configures a Registry.
type RegistryOption func(*registryConfig)
type registryConfig struct {
priority []string
}
// WithPriority sets the priority order for backend selection.
// Backends listed first are preferred over backends listed later.
// Backends not in the list have lowest priority (in registration order).
func WithPriority(names ...string) RegistryOption {
return func(c *registryConfig) {
c.priority = names
}
}
// NewRegistry creates a new Registry with optional configuration.
func NewRegistry[T any](opts ...RegistryOption) *Registry[T] {
cfg := ®istryConfig{}
for _, opt := range opts {
opt(cfg)
}
return &Registry[T]{
factories: make(map[string]func() T),
priority: cfg.priority,
}
}
// Register adds a factory for the given name.
// If a factory with the same name already exists, it is replaced.
// Thread-safe.
func (r *Registry[T]) Register(name string, factory func() T) {
r.mu.Lock()
defer r.mu.Unlock()
r.factories[name] = factory
}
// Unregister removes the factory with the given name.
// Thread-safe.
func (r *Registry[T]) Unregister(name string) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.factories, name)
}
// Get returns the factory output for the given name.
// Returns the zero value of T if not found.
// Thread-safe.
func (r *Registry[T]) Get(name string) T {
r.mu.RLock()
defer r.mu.RUnlock()
if factory, ok := r.factories[name]; ok {
return factory()
}
var zero T
return zero
}
// Has returns true if a factory with the given name is registered.
// Thread-safe.
func (r *Registry[T]) Has(name string) bool {
r.mu.RLock()
defer r.mu.RUnlock()
_, ok := r.factories[name]
return ok
}
// Best returns the highest-priority registered implementation.
// Returns the zero value of T if no implementations are registered.
// Thread-safe.
func (r *Registry[T]) Best() T {
r.mu.RLock()
defer r.mu.RUnlock()
// Try priority list first
for _, name := range r.priority {
if factory, ok := r.factories[name]; ok {
return factory()
}
}
// Fall back to any registered factory
for _, factory := range r.factories {
return factory()
}
var zero T
return zero
}
// BestName returns the name of the highest-priority registered implementation.
// Returns empty string if no implementations are registered.
// Thread-safe.
func (r *Registry[T]) BestName() string {
r.mu.RLock()
defer r.mu.RUnlock()
// Try priority list first
for _, name := range r.priority {
if _, ok := r.factories[name]; ok {
return name
}
}
// Fall back to any registered factory
for name := range r.factories {
return name
}
return ""
}
// Available returns all registered names.
// Thread-safe.
func (r *Registry[T]) Available() []string {
r.mu.RLock()
defer r.mu.RUnlock()
names := make([]string, 0, len(r.factories))
for name := range r.factories {
names = append(names, name)
}
return names
}
// Count returns the number of registered factories.
// Thread-safe.
func (r *Registry[T]) Count() int {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.factories)
}