register logic

This commit is contained in:
2026-08-29 23:59:56 +02:00
parent ddea3b0893
commit 94649a38fe
83 changed files with 830 additions and 699 deletions
+43
View File
@@ -0,0 +1,43 @@
package inc
type Manager[T any] struct {
nameOf func(T) string
entities []T
index map[string]int
}
func NewManager[T any](nameOf func(T) string) *Manager[T] {
return &Manager[T]{
nameOf: nameOf,
index: map[string]int{},
}
}
func (m *Manager[T]) Register(entity T) {
name := m.nameOf(entity)
if i, ok := m.index[name]; ok {
m.entities[i] = entity
return
}
m.index[name] = len(m.entities)
m.entities = append(m.entities, entity)
}
func (m *Manager[T]) GetByName(name string) (T, bool) {
i, ok := m.index[name]
if !ok {
var missing T
return missing, false
}
return m.entities[i], true
}
func (m *Manager[T]) GetAll() []T {
return m.entities
}
func registerAll[T any](m *Manager[T], register func(T)) {
for _, entity := range m.GetAll() {
register(entity)
}
}