use crate::entity_table::EntityMeta;
use crate::object::EntityId;
use crate::string_store::StringStore;
pub struct DualStringStore {
pub global: StringStore,
pub local: StringStore,
}
impl DualStringStore {
pub fn new() -> Self {
Self {
global: StringStore::new(),
local: StringStore::new(),
}
}
#[inline]
fn store(&self, entity: EntityId) -> &StringStore {
if entity.is_global() {
&self.global
} else {
&self.local
}
}
#[inline]
fn store_mut(&mut self, entity: EntityId) -> &mut StringStore {
if entity.is_global() {
&mut self.global
} else {
&mut self.local
}
}
pub fn allocate(&mut self, len: usize) -> EntityId {
self.local.allocate(len)
}
pub fn allocate_from(&mut self, bytes: &[u8]) -> EntityId {
self.local.allocate_from(bytes)
}
pub fn allocate_from_with(
&mut self,
bytes: &[u8],
save_level: u16,
global: bool,
created_after_save: u32,
) -> EntityId {
if global {
self.global
.allocate_from_with(bytes, save_level, global, created_after_save)
} else {
self.local
.allocate_from_with(bytes, save_level, global, created_after_save)
}
}
pub fn allocate_with(
&mut self,
len: usize,
save_level: u16,
global: bool,
created_after_save: u32,
) -> EntityId {
if global {
self.global
.allocate_with(len, save_level, global, created_after_save)
} else {
self.local
.allocate_with(len, save_level, global, created_after_save)
}
}
pub fn get(&self, entity: EntityId, start: u32, len: u32) -> &[u8] {
self.store(entity).get(entity, start, len)
}
pub fn get_mut(&mut self, entity: EntityId, start: u32, len: u32) -> &mut [u8] {
self.store_mut(entity).get_mut(entity, start, len)
}
pub fn put_byte(&mut self, entity: EntityId, offset: u32, byte: u8) {
self.store_mut(entity).put_byte(entity, offset, byte);
}
pub fn get_byte(&self, entity: EntityId, offset: u32) -> u8 {
self.store(entity).get_byte(entity, offset)
}
pub fn cow_copy(&mut self, entity: EntityId) -> EntityId {
debug_assert!(!entity.is_global(), "COW copy on global entity");
self.local.cow_copy(entity)
}
pub fn swap_offsets(&mut self, a: EntityId, b: EntityId) {
debug_assert!(
!a.is_global() && !b.is_global(),
"swap_offsets on global entity"
);
self.local.swap_offsets(a, b);
}
pub fn entity_meta(&self, entity: EntityId) -> &EntityMeta {
self.store(entity).entities.get(entity)
}
pub fn entity_meta_mut(&mut self, entity: EntityId) -> &mut EntityMeta {
self.store_mut(entity).entities.get_mut(entity)
}
pub fn data(&self) -> &[u8] {
self.local.data()
}
pub fn entity_count(&self) -> usize {
self.local.entities.len() + self.global.entities.len()
}
pub fn reset_local(&mut self) {
self.local = StringStore::new();
}
}
impl Default for DualStringStore {
fn default() -> Self {
Self::new()
}
}