use crate::object::{CoreObject, Context, ObjectRef, ObjectFactory};
use std::collections::HashMap;
use crate::system::System;
use crate::reflection::interface::{ClassConnector, PropertyInitializer};
pub struct ObjectRegistry<TContext: Context>
{
classes: HashMap<String, Box<dyn Fn (Option<bpx::sd::Object>) -> ObjectFactory<TContext>>>
}
impl<TContext: Context> System<TContext::SystemContext> for ObjectRegistry<TContext>
{
fn update(&mut self, _: &TContext::SystemContext, _: &<TContext::SystemContext as crate::system::Context>::AppState)
{
}
}
impl<TContext: Context> Default for ObjectRegistry<TContext>
{
fn default() -> Self
{
return ObjectRegistry {
classes: HashMap::new()
};
}
}
impl<TContext: Context> ObjectRegistry<TContext>
{
pub fn create(&self, class: &str, props: Option<bpx::sd::Object>) -> Option<ObjectFactory<TContext>>
{
if let Some(func) = self.classes.get(class)
{
return Some(func(props));
}
return None;
}
pub fn register<TObject: 'static + CoreObject<TContext> + ClassConnector>(&mut self)
{
self.classes.insert(TObject::class_name().to_string(), Box::new(|_| {
return ObjectFactory::from(|this| TObject::new_instance(this));
}));
}
pub fn register_with_props<TObject: 'static + CoreObject<TContext> + ClassConnector + PropertyInitializer>(&mut self)
{
self.classes.insert(TObject::class_name().to_string(), Box::new(|props| {
return ObjectFactory::from(|this| {
let mut obj = TObject::new_instance(this);
if let Some(props) = props {
obj.initialize(props);
}
return obj;
});
}));
}
}