use super::{
private::PropertyId,
properties_table::{PropertyType, PROPERTY_TABLE},
PropertyValue,
};
#[derive(Debug)]
pub struct UntypedProperty {
pub(crate) id: PropertyId,
owner: std::marker::PhantomData<std::rc::Rc<()>>,
}
impl Clone for UntypedProperty {
fn clone(&self) -> Self {
PROPERTY_TABLE.with(|t| {
t.increase_ref_count(self.id);
});
UntypedProperty {
id: self.id,
owner: std::marker::PhantomData,
}
}
}
impl Drop for UntypedProperty {
fn drop(&mut self) {
let _ = PROPERTY_TABLE.try_with(|t| {
let ref_count = t.decrease_ref_count(self.id);
if ref_count == 0 {
t.remove_entry(self.id);
}
});
}
}
impl UntypedProperty {
pub(super) fn try_from_id(id: PropertyId) -> Option<Self> {
PROPERTY_TABLE.with(|table| {
if !table.has_live_entry(id) {
return None;
}
table.increase_ref_count(id);
Some(Self {
id,
owner: std::marker::PhantomData,
})
})
}
pub(crate) fn new<T: PropertyValue>(
val: T,
inbound: Vec<PropertyId>,
data: PropertyType<T>,
debug_name: Option<&str>,
) -> Self {
UntypedProperty {
id: PROPERTY_TABLE.with(|t| t.add_entry(val, inbound, data, debug_name)),
owner: std::marker::PhantomData,
}
}
pub fn get_id(&self) -> PropertyId {
self.id
}
pub(crate) fn has_value_type<T: PropertyValue>(&self) -> bool {
PROPERTY_TABLE.with(|table| table.has_value_type::<T>(self.id))
}
}