1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::{IntoSendStorage, entity::EntitiesStorage, storage::{FastIndexExt, ReadOnlyStorage}, sync::{ReadGuardRef, OrderedIndexGuard}};
use std::{marker, ops::Deref};
use crate::Component;
use crate::ComponentSend;
use crate::Bitmask;
use super::{OrderedData, SafeIter};
use crate::storage::{HierarchicalStorage, StorageRef, IntoHierarchicalIter};
use crate::utils::delete_lifetime;
use crate::storage::forest::ForestHierarchicalIter;
use crate::idtree;
use std::fmt::Debug;

/// Operator over hierarchical components that will select an entity's
/// component and it's parent for read only
///
/// ```
/// # #[macro_use] extern crate rinecs_derive;
/// # use rinecs::*;
/// #[derive(HierarchicalComponent, Debug)]
/// struct Position{x: f32, y: f32}
/// # let mut world = World::new();
/// # let entities = world.entities();
/// for (pos, parent_pos) in entities.ordered_iter_for::<ReadAndParent<Position>>(){
///
/// }
/// ```
///
pub struct ReadAndParent<'a, T>{
    _marker: marker::PhantomData<&'a T>,
}

pub struct ParentStorageRead<'a, S, BS, T>{
    storage: BS,
    marker_t: marker::PhantomData<&'a T>,
    marker_s: marker::PhantomData<S>
}

impl<'a, 'r, S, BS, T> FastIndexExt for ParentStorageRead<'a, S, BS, T>
where
    S: HierarchicalStorage<'a,T> + 'a,
    BS: Deref<Target = S>,
    T: Component
{
    type FastIndex = idtree::NodeId;
    type StaticTypeId = T;

    fn fast_index(&self, guid: usize) -> idtree::NodeId {
        unsafe{ self.storage.ordered_fast_index(guid) }
    }
}

impl<'a, 'r, S, BS, T> StorageRef<'r> for ParentStorageRead<'a, S, BS, T>
where
    S: HierarchicalStorage<'a,T> + 'a,
    BS: Deref<Target = S>,
    T: Component
{
    type Data = ReadAndParent<'a, T>;
    type Component = (&'r T, Option<&'r T>);

    unsafe fn get_fast_unchecked(&'r mut self, guid: idtree::NodeId) -> Self::Component {
        let node = self.storage.fast_get_node(guid);
        let parent = node.parent().map(|p| delete_lifetime(&p as &T) );
        let node = delete_lifetime(&node as &T);
        (&node, parent)
    }

    unsafe fn get_unchecked(&'r mut self, guid: usize) -> Self::Component {
        let node = self.storage.get_node(guid);
        let parent = node.parent().map(|p| delete_lifetime(&p as &T) );
        let node = delete_lifetime(&node as &T);
        (&node, parent)
    }

    fn contains(&self, guid: usize) -> bool {
        self.storage.contains(guid)
    }
}

impl<'a, 'r, S,T> IntoSendStorage<'r> for ParentStorageRead<'a, S, ReadGuardRef<'a, S>, T>
where
    S: HierarchicalStorage<'a,T> + 'r,
    T: Component
{
    type SendStorage = ParentStorageRead<'a, S, &'r S, T>;

    fn into_send_storage(&'r mut self) -> Self::SendStorage {
        ParentStorageRead {
            storage: &self.storage,
            marker_s: marker::PhantomData,
            marker_t: marker::PhantomData,
        }
    }
}

unsafe impl<'a, S, BS, T> ReadOnlyStorage for ParentStorageRead<'a, S, BS, T>{}

pub struct ReadAndParentIter<'a, T: Component>
    where for<'b> <T as Component>::Storage: HierarchicalStorage<'b,T>,
        //   for<'b> RwLockReadGuard<'b, <T as Component>::Storage>: IntoHierarchicalIter<'b,T>
{
    it: Option<ForestHierarchicalIter<'a, T>> //<RwLockWriteGuard<'a, <T as Component>::Storage> as IntoOrderedIterMut>::OrderedIterMut
}

impl<'a,T> Iterator for ReadAndParentIter<'a, T>
    where T: Component + Debug,
          for<'b> <T as Component>::Storage: HierarchicalStorage<'b,T>,
        //   for<'b> RwLockReadGuard<'b, <T as Component>::Storage>: IntoHierarchicalIter<'b,T>,
{
    type Item = (&'a T, Option<&'a T>);
    fn next(&mut self) -> Option<(&'a T, Option<&'a T>)>{
        self.it.as_mut().and_then(|it| it.next().map(|n| {
            let n_ref = unsafe{ delete_lifetime(&n as &T) };
            let p = n.parent().map(|p|  unsafe{ delete_lifetime(&p as &T) });
            (n_ref, p)
        }))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.it.as_ref().map(|it| it.size_hint()).unwrap_or((0, None))
    }
}

unsafe impl<T> SafeIter<'_> for ReadAndParent<'_, T>{}

impl<'a, T: ComponentSend> OrderedData<'a> for ReadAndParent<'a,T>
    where for<'b> <T as Component>::Storage: HierarchicalStorage<'b,T>,
          for<'b> ReadGuardRef<'b, <T as Component>::Storage>: IntoHierarchicalIter<'b,T>
{
    type Iter = ReadAndParentIter<'a,T>;
    type Components = T;
    type ComponentsRef = (&'a T, Option<&'a T>);
    type Storage = ParentStorageRead<'a, <T as Component>::Storage, ReadGuardRef<'a, <T as Component>::Storage>, Self::Components>;
    fn query_mask<E: EntitiesStorage>(entities: &E) -> Bitmask{
        Bitmask::has(entities.component_mask::<T>())
    }

    fn into_iter<E: EntitiesStorage>(entities: &'a E) -> Self::Iter{
        ReadAndParentIter{
            it: entities.storage::<T>()
                .map(|s| s.into_hierarchical_iter())
        }
    }

    fn storage<E: EntitiesStorage>(entities: &'a E) -> Option<Self::Storage>{
        Some(ParentStorageRead{
            storage: entities.storage::<T>()?,
            marker_s: marker::PhantomData,
            marker_t: marker::PhantomData,
        })
    }

    fn ordered_ids<S: FastIndexExt, E: EntitiesStorage>(entities: &'a E, query_mask: Bitmask, unordered_storage: &S) -> OrderedIndexGuard<'a>{
        entities.ordered_entities_for::<T,_>(query_mask, unordered_storage)
    }
}