use std::any::{type_name, TypeId};
use std::mem::{align_of, size_of};
use crate::engine::types::ComponentID;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct ComponentDesc {
pub component_id: Option<ComponentID>,
pub name: &'static str,
pub type_id: TypeId,
pub size: usize,
pub align: usize,
pub gpu_usage: bool,
}
impl ComponentDesc {
#[inline]
pub fn new(
component_id: Option<ComponentID>,
name: &'static str,
type_id: TypeId,
size: usize,
align: usize,
gpu_usage: bool,
) -> Self {
Self {
component_id,
name,
type_id,
size,
align,
gpu_usage,
}
}
#[inline]
pub fn of<T: 'static>() -> Self {
Self {
component_id: None,
name: type_name::<T>(),
type_id: TypeId::of::<T>(),
size: size_of::<T>(),
align: align_of::<T>(),
gpu_usage: false,
}
}
#[inline]
pub fn use_gpu(mut self, gpu_usage: bool) -> Self {
self.gpu_usage = gpu_usage;
self
}
#[inline]
pub fn matches_type<T: 'static>(&self) -> bool {
self.type_id == TypeId::of::<T>()
}
#[inline]
pub fn with_id(mut self, component_id: ComponentID) -> Self {
self.component_id = Some(component_id);
self
}
}
impl std::fmt::Display for ComponentDesc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let id = match self.component_id {
Some(id) => id.to_string(),
None => "unassigned".to_string(),
};
write!(
f,
"ComponentDesc {{ id: {}, name: {}, size: {}, align: {}, uses gpu: {} }}",
id, self.name, self.size, self.align, self.gpu_usage
)
}
}