|
| 1 | +use crate::archetype::ArchetypeComponentId; |
| 2 | +use crate::component::{ComponentId, ComponentTicks, Components}; |
| 3 | +use crate::storage::{Column, SparseSet}; |
| 4 | +use bevy_ptr::{OwningPtr, Ptr, UnsafeCellDeref}; |
| 5 | +use std::cell::UnsafeCell; |
| 6 | + |
| 7 | +/// The type-erased backing storage and metadata for a single resource within a [`World`]. |
| 8 | +/// |
| 9 | +/// [`World`]: crate::world::World |
| 10 | +pub struct ResourceData { |
| 11 | + column: Column, |
| 12 | + id: ArchetypeComponentId, |
| 13 | +} |
| 14 | + |
| 15 | +impl ResourceData { |
| 16 | + /// Returns true if the resource is populated. |
| 17 | + #[inline] |
| 18 | + pub fn is_present(&self) -> bool { |
| 19 | + !self.column.is_empty() |
| 20 | + } |
| 21 | + |
| 22 | + /// Gets the [`ArchetypeComponentId`] for the resource. |
| 23 | + #[inline] |
| 24 | + pub fn id(&self) -> ArchetypeComponentId { |
| 25 | + self.id |
| 26 | + } |
| 27 | + |
| 28 | + /// Gets a read-only pointer to the underlying resource, if available. |
| 29 | + #[inline] |
| 30 | + pub fn get_data(&self) -> Option<Ptr<'_>> { |
| 31 | + self.column.get_data(0) |
| 32 | + } |
| 33 | + |
| 34 | + /// Gets a read-only reference to the change ticks of the underlying resource, if available. |
| 35 | + #[inline] |
| 36 | + pub fn get_ticks(&self) -> Option<&ComponentTicks> { |
| 37 | + self.column |
| 38 | + .get_ticks(0) |
| 39 | + // SAFETY: |
| 40 | + // - This borrow's lifetime is bounded by the lifetime on self. |
| 41 | + // - A read-only borrow on self can only exist while a mutable borrow doesn't |
| 42 | + // exist. |
| 43 | + .map(|ticks| unsafe { ticks.deref() }) |
| 44 | + } |
| 45 | + |
| 46 | + #[inline] |
| 47 | + pub(crate) fn get_with_ticks(&self) -> Option<(Ptr<'_>, &UnsafeCell<ComponentTicks>)> { |
| 48 | + self.column.get(0) |
| 49 | + } |
| 50 | + |
| 51 | + /// Inserts a value into the resource. If a value is already present |
| 52 | + /// it will be replaced. |
| 53 | + /// |
| 54 | + /// # Safety |
| 55 | + /// `value` must be valid for the underlying type for the resource. |
| 56 | + /// |
| 57 | + /// The underlying type must be [`Send`] or be inserted from the main thread. |
| 58 | + /// This can be validated with [`World::validate_non_send_access_untyped`]. |
| 59 | + /// |
| 60 | + /// [`World::validate_non_send_access_untyped`]: crate::world::World::validate_non_send_access_untyped |
| 61 | + #[inline] |
| 62 | + pub(crate) unsafe fn insert(&mut self, value: OwningPtr<'_>, change_tick: u32) { |
| 63 | + if self.is_present() { |
| 64 | + self.column.replace(0, value, change_tick); |
| 65 | + } else { |
| 66 | + self.column.push(value, ComponentTicks::new(change_tick)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Inserts a value into the resource with a pre-existing change tick. If a |
| 71 | + /// value is already present it will be replaced. |
| 72 | + /// |
| 73 | + /// # Safety |
| 74 | + /// `value` must be valid for the underlying type for the resource. |
| 75 | + /// |
| 76 | + /// The underlying type must be [`Send`] or be inserted from the main thread. |
| 77 | + /// This can be validated with [`World::validate_non_send_access_untyped`]. |
| 78 | + /// |
| 79 | + /// [`World::validate_non_send_access_untyped`]: crate::world::World::validate_non_send_access_untyped |
| 80 | + #[inline] |
| 81 | + pub(crate) unsafe fn insert_with_ticks( |
| 82 | + &mut self, |
| 83 | + value: OwningPtr<'_>, |
| 84 | + change_ticks: ComponentTicks, |
| 85 | + ) { |
| 86 | + if self.is_present() { |
| 87 | + self.column.replace_untracked(0, value); |
| 88 | + *self.column.get_ticks_unchecked(0).deref_mut() = change_ticks; |
| 89 | + } else { |
| 90 | + self.column.push(value, change_ticks); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// Removes a value from the resource, if present. |
| 95 | + /// |
| 96 | + /// # Safety |
| 97 | + /// The underlying type must be [`Send`] or be removed from the main thread. |
| 98 | + /// This can be validated with [`World::validate_non_send_access_untyped`]. |
| 99 | + /// |
| 100 | + /// The removed value must be used or dropped. |
| 101 | + /// |
| 102 | + /// [`World::validate_non_send_access_untyped`]: crate::world::World::validate_non_send_access_untyped |
| 103 | + #[inline] |
| 104 | + #[must_use = "The returned pointer to the removed component should be used or dropped"] |
| 105 | + pub(crate) unsafe fn remove(&mut self) -> Option<(OwningPtr<'_>, ComponentTicks)> { |
| 106 | + self.column.swap_remove_and_forget(0) |
| 107 | + } |
| 108 | + |
| 109 | + /// Removes a value from the resource, if present, and drops it. |
| 110 | + /// |
| 111 | + /// # Safety |
| 112 | + /// The underlying type must be [`Send`] or be removed from the main thread. |
| 113 | + /// This can be validated with [`World::validate_non_send_access_untyped`]. |
| 114 | + /// |
| 115 | + /// [`World::validate_non_send_access_untyped`]: crate::world::World::validate_non_send_access_untyped |
| 116 | + #[inline] |
| 117 | + pub(crate) unsafe fn remove_and_drop(&mut self) { |
| 118 | + self.column.clear(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// The backing store for all [`Resource`]s stored in the [`World`]. |
| 123 | +/// |
| 124 | +/// [`Resource`]: crate::system::Resource |
| 125 | +/// [`World`]: crate::world::World |
| 126 | +#[derive(Default)] |
| 127 | +pub struct Resources { |
| 128 | + resources: SparseSet<ComponentId, ResourceData>, |
| 129 | +} |
| 130 | + |
| 131 | +impl Resources { |
| 132 | + /// The total number of resources stored in the [`World`] |
| 133 | + /// |
| 134 | + /// [`World`]: crate::world::World |
| 135 | + #[inline] |
| 136 | + pub fn len(&self) -> usize { |
| 137 | + self.resources.len() |
| 138 | + } |
| 139 | + |
| 140 | + /// Returns true if there are no resources stored in the [`World`], |
| 141 | + /// false otherwise. |
| 142 | + /// |
| 143 | + /// [`World`]: crate::world::World |
| 144 | + #[inline] |
| 145 | + pub fn is_empty(&self) -> bool { |
| 146 | + self.resources.is_empty() |
| 147 | + } |
| 148 | + |
| 149 | + /// Gets read-only access to a resource, if it exists. |
| 150 | + #[inline] |
| 151 | + pub fn get(&self, component_id: ComponentId) -> Option<&ResourceData> { |
| 152 | + self.resources.get(component_id) |
| 153 | + } |
| 154 | + |
| 155 | + /// Gets mutable access to a resource, if it exists. |
| 156 | + #[inline] |
| 157 | + pub(crate) fn get_mut(&mut self, component_id: ComponentId) -> Option<&mut ResourceData> { |
| 158 | + self.resources.get_mut(component_id) |
| 159 | + } |
| 160 | + |
| 161 | + /// Fetches or initializes a new resource and returns back it's underlying column. |
| 162 | + /// |
| 163 | + /// # Panics |
| 164 | + /// Will panic if `component_id` is not valid for the provided `components` |
| 165 | + pub(crate) fn initialize_with( |
| 166 | + &mut self, |
| 167 | + component_id: ComponentId, |
| 168 | + components: &Components, |
| 169 | + f: impl FnOnce() -> ArchetypeComponentId, |
| 170 | + ) -> &mut ResourceData { |
| 171 | + self.resources.get_or_insert_with(component_id, || { |
| 172 | + let component_info = components.get_info(component_id).unwrap(); |
| 173 | + ResourceData { |
| 174 | + column: Column::with_capacity(component_info, 1), |
| 175 | + id: f(), |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | + pub(crate) fn check_change_ticks(&mut self, change_tick: u32) { |
| 181 | + for info in self.resources.values_mut() { |
| 182 | + info.column.check_change_ticks(change_tick); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments