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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use primitives::{Component, Id, Valid};

use downcast::Downcast;

use std::any::Any;

// ++++++++++++++++++++ StoreBase ++++++++++++++++++++

pub trait StoreBase: Any + Send + Sync {
    type Id: Id;

    fn len(&self) -> usize;

    fn is_assigned<'id>(&self, id: Valid<'id, Self::Id>) -> bool;

    /*fn assign_cloned<'id>(
        &mut self, 
        dest: Valid<'id, Self::Id>, 
        src: Valid<'id, Self::Id>,
    ) -> bool;*/

    /// A variant of `.release(id)` which drops the returned component. 
    /// Returns `true` if sucessful.
    fn release_drop<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool;

    fn clear(&mut self);
}

impl<ID: Id, T: StoreBase<Id = ID>> Downcast<T> for StoreBase<Id = ID> {
    impl_downcast_items!(T);
}

impl<ID: Id> StoreBase<Id = ID> {
    downcast_method_items!();
}

impl<S: StoreBase + ?Sized> StoreBase for Box<S> {
    type Id = S::Id;

    fn len(&self) -> usize { (**self).len() }

    fn is_assigned<'id>(&self, id: Valid<'id, Self::Id>) -> bool { (**self).is_assigned(id) }

    /*fn assign_cloned<'id>(
        &mut self, 
        dest: Valid<'id, Self::Id>, 
        src: Valid<'id, Self::Id>,
    ) -> bool {
        (**self).assign_cloned(dest, src)
    }*/

    fn release_drop<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool { (**self).release_drop(id) }

    fn clear(&mut self){ (**self).clear() }
}

// ++++++++++++++++++++ ComponentStore ++++++++++++++++++++

/// Workaround for lack of HK-lifetimes.
pub trait _ComponentStore<'a>: StoreBase {
    type _Component: Component;

    type Components: Iterator<Item = &'a Self::_Component> + 'a;
    
    type ComponentsMut: Iterator<Item = &'a mut Self::_Component> + 'a;
}

pub trait ComponentStore: StoreBase
    where Self: for<'a> _ComponentStore<'a, _Component = <Self as ComponentStore>::Component>
{
    type Component: Component; 

    fn assign<'id, T>(&mut self, id: Valid<'id, Self::Id>, com: T) -> Option<Self::Component>
        where T: Into<Self::Component>;

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<Self::Component>;

    /// Returns `None` if `id` is not assigned.
    fn get<'id>(&self, id: Valid<'id, Self::Id>) -> Option<&Self::Component>;

    /// Returns `None` if `id` is not assigned.
    fn get_mut<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<&mut Self::Component>;

    fn components<'a>(&'a self) -> <Self as _ComponentStore<'a>>::Components;

    fn components_mut<'a>(&'a mut self) -> <Self as _ComponentStore<'a>>::ComponentsMut;
}

// ++++++++++++++++++++ ComponentStoreIter ++++++++++++++++++++

/// Workaround for lack of HK-lifetimes.
pub trait _ComponentStoreAssignments<'a>: _ComponentStore<'a> {
    type Assignments: Iterator<Item = (Valid<'a, Self::Id>, &'a Self::_Component)> + 'a;

    type AssignmentsMut: Iterator<Item = (Valid<'a, Self::Id>, &'a mut Self::_Component)> + 'a;
}

pub trait ComponentStoreAssignments: ComponentStore
    where Self: for<'a> _ComponentStoreAssignments<'a>
{
    fn assignments<'a>(&'a self) -> <Self as _ComponentStoreAssignments<'a>>::Assignments;

    fn assignments_mut<'a>(&'a mut self) -> <Self as _ComponentStoreAssignments<'a>>::AssignmentsMut;
}

// ++++++++++++++++++++ EntityStore ++++++++++++++++++++

pub trait EntityStore: StoreBase {
    fn assign<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool;

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool {
        self.release_drop(id)
    }
}

// ++++++++++++++++++++ EntityStoreIter ++++++++++++++++++++

pub trait _EntityStoreEntities<'a>: EntityStore {
    type Entities: Iterator<Item = Valid<'a, Self::Id>>;
}

pub trait EntityStoreEntities: EntityStore
    where Self: for<'a> _EntityStoreEntities<'a>
{
    fn entities<'a>(&'a self) -> <Self as _EntityStoreEntities<'a>>::Entities;
}

// ++++++++++++++++++++ StoreWithDirtyState ++++++++++++++++++++

/// Workaround for lack of HK-lifetimes.
pub trait _StoreWithDirtyState<'a>: StoreBase {
    type DirtyEntities: Iterator<Item = Valid<'a, Self::Id>>;
}

/// TODO naming. `ChangeList`, `DirtyState`?
pub trait StoreWithDirtyState: StoreBase 
    where Self: for<'a> _StoreWithDirtyState<'a>
{
    fn is_dirty<'a>(&self, id: Valid<'a, Self::Id>) -> bool;

    fn dirty_entities<'a>(&'a self) -> <Self as _StoreWithDirtyState<'a>>::DirtyEntities;
}


/* NOTE: old code
pub trait SecondaryIdStoreTypes<'a, C: Id>: StoreBase {
    /// Some store-types don't store the primary-id themselves; for these the first
    /// tuple element will always yield `None`.
    type Iter: Iterator<Item = (Option<Valid<'a, Self::Id>>, Self::Output)>;

    /// Some store-types don't store the primary-id themselves; for these the first
    /// tuple element will always yield `None`.
    type IterMut: Iterator<Item = (Option<Valid<'a, Self::Id>>, &'a mut C)> = IterMutDummy<(Option<Valid<'a, Self::Id>>, &'a mut C)>;
}

pub trait SecondaryIdStore<C>: ComponentStore<C>
    where C: Id, Self: for<'a> ComponentStoreComponents<'a, C, Output = C>
{
    fn validate(&self, m_id: C) -> Option<Valid<Self::Id>>;

    fn invalidate(&mut self, id: C) -> Option<Valid<Self::Id>>;

    /// Generates a new id and activates it for `p_id`.
    fn generate_for<'id>(
        &mut self, 
        p_id: Valid<'id, Self::Id>
    ) -> IdGenerationResult<C>;

    fn activate_for<'id>(
        &mut self, 
        p_id: Valid<'id, Self::Id>,
        m_id: C,
    ) -> IdActivationResult<(), (C, Self::Id)>;

    /// TODO should this return `(C, Valid<Self::Id>)`?
    ///
    /// TODO should `id` be `usize`?
    fn slot_of(&mut self, m_id: C) -> Option<(C, Self::Id)>;
}
*/