use std::ops::{Index, IndexMut};
use crate::component::{Component, ComponentRef};
use crate::object::ObjectRef;
pub trait ComponentPool<T: Component>: Index<ComponentRef<T>, Output = T> + IndexMut<ComponentRef<T>>
where
Self: Sized
{
fn add(&mut self, comp: T) -> ComponentRef<T>;
fn remove(&mut self, r: ComponentRef<T>);
fn len(&self) -> usize;
fn is_empty(&self) -> bool
{
self.len() == 0
}
}
pub trait Iter<'a, T: 'a + Component>
{
type Iter: Iterator<Item = (ComponentRef<T>, &'a T)>;
type IterMut: Iterator<Item = (ComponentRef<T>, &'a mut T)>;
fn iter(&'a self) -> Self::Iter;
fn iter_mut(&'a mut self) -> Self::IterMut;
}
pub trait Attachments<T: Component>
{
fn attach(&mut self, entity: ObjectRef, r: ComponentRef<T>);
fn list(&self, entity: ObjectRef) -> Option<Vec<ComponentRef<T>>>;
fn clear(&mut self, entity: ObjectRef);
fn get_first_mut(&mut self, entity: ObjectRef) -> Option<&mut T>;
fn get_first(&self, entity: ObjectRef) -> Option<&T>;
}
pub trait ComponentManager<T: Component>
{
fn get(&self) -> &T::Pool;
fn get_mut(&mut self) -> &mut T::Pool;
fn get_component(&self, r: ComponentRef<T>) -> &T
{
&self.get()[r]
}
fn get_component_mut(&mut self, r: ComponentRef<T>) -> &mut T
{
&mut self.get_mut()[r]
}
fn add_component(&mut self, comp: T) -> ComponentRef<T>
{
self.get_mut().add(comp)
}
fn remove_component(&mut self, r: ComponentRef<T>)
{
self.get_mut().remove(r);
}
}