use once_cell::sync::OnceCell;
use std::{
any::{Any, TypeId},
cell::RefCell,
collections::BTreeMap,
rc::Rc,
};
use crate::prelude::{Model, Proxy, Singleton};
#[derive(Default)]
pub struct BaseModel {
storages: RefCell<BTreeMap<TypeId, Rc<dyn Any>>>,
}
unsafe impl std::marker::Send for BaseModel {}
unsafe impl std::marker::Sync for BaseModel {}
impl BaseModel {
pub fn new() -> Self {
Self {
storages: RefCell::new(BTreeMap::new()),
}
}
}
impl Singleton for BaseModel {
fn global() -> &'static Self {
static BASE_MODEL_INSTANCE: OnceCell<BaseModel> = OnceCell::new();
BASE_MODEL_INSTANCE.get_or_init(Self::new)
}
}
impl Model for BaseModel {
fn has_proxy<P: Proxy>(&self) -> bool {
let type_id = TypeId::of::<P>();
self.storages.borrow().contains_key(&type_id)
}
fn register_proxy<P: Proxy>(&self, proxy: Rc<P>) {
let type_id = TypeId::of::<P>();
log::info!("Register Proxy [BaseModel] {:?}", proxy);
self.storages.borrow_mut().insert(type_id, proxy.clone());
proxy.on_register();
}
fn remove_proxy<P: Proxy>(&self) -> Option<Rc<P>> {
let type_id = TypeId::of::<P>();
self.storages
.borrow_mut()
.remove(&type_id)
.map(|proxy| match proxy.downcast::<P>() {
Ok(proxy) => {
proxy.on_remove();
proxy
}
Err(_) => {
panic!("Something wrong with proxy storage");
}
})
}
fn retrieve_proxy<P: Proxy>(&self) -> Option<Rc<P>> {
let type_id = TypeId::of::<P>();
match self.storages.borrow().get(&type_id) {
Some(item) => match item.clone().downcast::<P>() {
Ok(proxy) => Some(proxy),
Err(_) => {
log::error!("Something wrong with proxy storage");
None
}
},
None => None,
}
}
}