use std::rc::Rc;
use std::cell::RefCell;
use std::fmt;
use super::control::SwarmControl;
pub type ObjectPosition = usize;
pub type SpawnId = usize;
pub type ForEachHandler<ItemType> = fn(&mut ItemType);
pub type EnumerateHandler<ItemType> = fn(&usize, &mut ItemType);
pub type ForAllHandler<ItemType, Properties> = fn(&ObjectPosition, &mut [ItemType], &mut Properties);
pub type UpdateHandler<ItemType, Properties> = fn(&mut SwarmControl<ItemType, Properties>);
pub type FactoryHandler<ItemType, Properties> = fn(&mut ItemType, &mut Properties);
pub struct Factory<ItemType, Properties> {
pub type_def: usize,
pub methode: FactoryHandler<ItemType, Properties>,
}
pub struct Spawn(pub(crate) Rc<RefCell<Tag>>);
impl Spawn {
pub(crate) fn new(index: usize) -> Self {
Spawn( Rc::new( RefCell::new( Tag{ id:index, pos:index, active:false })))
}
pub fn id(&self) -> SpawnId {
self.0.borrow().id
}
pub fn pos(&self) -> ObjectPosition {
self.0.borrow().pos
}
pub fn active(&self) -> bool {
self.0.borrow().active
}
pub fn mirror(&self) -> Self {
Spawn (Rc::clone(&self.0))
}
}
impl fmt::Debug for Spawn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl PartialEq for Spawn {
fn eq(&self, other: &Spawn) -> bool {
self.id() == other.id()
}
}
impl Default for Spawn {
fn default() -> Self {
Spawn( Rc::new( RefCell::new( Tag::default() )))
}
}
impl Clone for Spawn {
fn clone(&self) -> Self {
self.mirror()
}
}
#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub struct Tag {
pub(crate) id: SpawnId,
pub(crate) pos: ObjectPosition,
pub(crate) active: bool,
}
#[allow(dead_code)]
impl Tag {
fn id(&self) -> &SpawnId { &self.id }
fn pos(&self) -> &ObjectPosition { &self.pos }
fn active(&self) -> bool { self.active }
}