rust/hwphysics/src/data.rs
changeset 15305 0076bf602969
child 15354 dff37ac61dcf
equal deleted inserted replaced
15304:b043b5d5219a 15305:0076bf602969
       
     1 use std::any::{Any, TypeId};
       
     2 
       
     3 const BLOCK_SIZE: usize = 8192;
       
     4 
       
     5 pub trait TypeTuple {
       
     6     type Storage: Default;
       
     7 
       
     8     fn len() -> usize;
       
     9     fn get_types(dest: &mut Vec<TypeId>);
       
    10 }
       
    11 
       
    12 //TODO macroise this template for tuples up to sufficient size
       
    13 impl<T: 'static> TypeTuple for (T,) {
       
    14     type Storage = (Vec<T>,);
       
    15 
       
    16     #[inline]
       
    17     fn len() -> usize {
       
    18         1
       
    19     }
       
    20 
       
    21     #[inline]
       
    22     fn get_types(dest: &mut Vec<TypeId>) {
       
    23         dest.push(TypeId::of::<T>());
       
    24     }
       
    25 }
       
    26 
       
    27 pub struct GearDataCollection<T: TypeTuple> {
       
    28     len: usize,
       
    29     blocks: T::Storage,
       
    30 }
       
    31 
       
    32 impl<T: TypeTuple> GearDataCollection<T> {
       
    33     fn new() -> Self {
       
    34         Self {
       
    35             len: 0,
       
    36             blocks: T::Storage::default(),
       
    37         }
       
    38     }
       
    39 
       
    40     fn iter<F: Fn(T)>(&self, f: F) {}
       
    41 }
       
    42 
       
    43 pub struct GearDataGroup {
       
    44     group_selector: u64,
       
    45     data: Box<dyn Any + 'static>,
       
    46 }
       
    47 
       
    48 impl GearDataGroup {
       
    49     fn iter() {}
       
    50 }
       
    51 
       
    52 pub struct GearDataManager {
       
    53     types: Vec<TypeId>,
       
    54     groups: Vec<GearDataGroup>,
       
    55 }
       
    56 
       
    57 impl GearDataManager {
       
    58     pub fn new() -> Self {
       
    59         Self {
       
    60             types: vec![],
       
    61             groups: vec![],
       
    62         }
       
    63     }
       
    64 
       
    65     pub fn register<T: 'static>(&mut self) {
       
    66         assert!(self.types.len() <= 64);
       
    67         let id = TypeId::of::<T>();
       
    68         if !self.types.contains(&id) {
       
    69             self.types.push(id);
       
    70         }
       
    71     }
       
    72 
       
    73     fn create_selector(&self, types: &[TypeId]) -> u64 {
       
    74         let mut selector = 0u64;
       
    75         for (i, typ) in self.types.iter().enumerate() {
       
    76             if types.contains(&typ) {
       
    77                 selector |= 1 << (i as u64)
       
    78             }
       
    79         }
       
    80         selector
       
    81     }
       
    82 
       
    83     pub fn iter<T: TypeTuple + 'static, F: Fn(T) + Copy>(&self, f: F) {
       
    84         let mut types = vec![];
       
    85         T::get_types(&mut types);
       
    86         let selector = self.create_selector(&types);
       
    87         for group in &self.groups {
       
    88             if group.group_selector & selector == selector {
       
    89                 group
       
    90                     .data
       
    91                     .downcast_ref::<GearDataCollection<T>>()
       
    92                     .unwrap()
       
    93                     .iter(f);
       
    94             }
       
    95         }
       
    96     }
       
    97 }