component: Automatically determine names

This commit is contained in:
Tom Wiesing 2022-10-17 20:39:53 +02:00
parent 10df1c3243
commit e320bb37bb
No known key found for this signature in database
31 changed files with 84 additions and 113 deletions

View file

@ -7,14 +7,16 @@ import (
)
type ComponentPool struct {
pool lazy.Pool[Component, Core]
pool lazy.Pool[Component, Still]
poolInit sync.Once
}
func (pool *ComponentPool) init() {
pool.poolInit.Do(func() {
pool.pool.Init = func(component Component, core Core) Component {
component.getBase().Core = core
pool.pool.Init = func(component Component, core Still) Component {
base := component.getComponentBase()
base.Still = core
base.name = nameOf(component)
return component
}
})
@ -23,7 +25,7 @@ func (pool *ComponentPool) init() {
type ComponentPoolContext = *lazy.PoolContext[Component]
type ComponentAllFunc = func(context ComponentPoolContext) []Component
func (pool *ComponentPool) All(core Core, init func(context ComponentPoolContext) []Component) []Component {
func (pool *ComponentPool) All(core Still, init func(context ComponentPoolContext) []Component) []Component {
pool.init()
return pool.pool.All(core, init)
}
@ -43,7 +45,7 @@ func (pool *ComponentPool) All(core Core, init func(context ComponentPoolContext
// Furthermore, the init function may not cause other components to be initialized.
//
// The init function may be nil, indicating that no additional initialization is required.
func MakeComponent[C Component](context ComponentPoolContext, core Core, init func(component C)) C {
func MakeComponent[C Component](context ComponentPoolContext, core Still, init func(component C)) C {
return lazy.Make(context, init)
}
@ -51,16 +53,16 @@ func MakeComponent[C Component](context ComponentPoolContext, core Core, init fu
//
// All should be the function of the core that initializes all components.
// All should only make calls to [InitComponent].
func ExportComponents[C Component](pool *ComponentPool, core Core, All ComponentAllFunc) []C {
func ExportComponents[C Component](pool *ComponentPool, core Still, All ComponentAllFunc) []C {
pool.init()
return lazy.ExportComponents[Component, Core, C](&pool.pool, core, All)
return lazy.ExportComponents[Component, Still, C](&pool.pool, core, All)
}
// ExportComponent exports the first component that is a C from the pool.
//
// All should be the function of the core that initializes all components.
// All should only make calls to [InitComponent].
func ExportComponent[C Component](pool *ComponentPool, core Core, All ComponentAllFunc) C {
func ExportComponent[C Component](pool *ComponentPool, core Still, All ComponentAllFunc) C {
pool.init()
return lazy.ExportComponent[Component, Core, C](&pool.pool, core, All)
return lazy.ExportComponent[Component, Still, C](&pool.pool, core, All)
}