use std::default::Default;
use crate::core::Property;
use crate::util::AnyMap;
#[derive(Clone, Default)]
pub struct PropertySet {
pub(crate) map: AnyMap,
}
impl PropertySet {
pub fn new() -> Self {
Self { map: AnyMap::new() }
}
pub fn one<P: Property>(value: P) -> Self {
Self::new().with(value)
}
pub fn with<P: Property>(mut self, value: P) -> Self {
self.map.insert(value);
self
}
pub fn get<P: Property>(&self) -> Option<&P> {
self.map.get::<P>()
}
pub fn insert<P: Property>(&mut self, value: P) -> Option<P> {
self.map.insert(value)
}
pub fn remove<P: Property>(&mut self) -> Option<P> {
self.map.remove::<P>()
}
}
impl std::fmt::Debug for PropertySet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{} properties>", self.map.len())
}
}
impl<P: Property> From<P> for PropertySet {
fn from(prop: P) -> Self {
Self::one(prop)
}
}
macro_rules! impl_props_from_tuple {
(
$(
$Type: ident, $idx: tt;
)*
) => {
impl<
$($Type: Property,)*
>
From<( $($Type,)* )> for PropertySet
{
fn from(value: ( $($Type,)* )) -> Self {
PropertySet::new()
$(
.with(value.$idx)
)*
}
}
};
}
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6; P7, 7; P8, 8; P9, 9; P10, 10; P11, 11;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6; P7, 7; P8, 8; P9, 9; P10, 10;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6; P7, 7; P8, 8; P9, 9;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6; P7, 7; P8, 8;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6; P7, 7;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5; P6, 6;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4; P5, 5;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3; P4, 4;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2; P3, 3;);
impl_props_from_tuple!(P0, 0; P1, 1; P2, 2;);
impl_props_from_tuple!(P0, 0; P1, 1;);
impl_props_from_tuple!(P0, 0;);