Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter for ColumnDynamic count #21

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::alloc::{Layout, handle_alloc_error};
use std::ffi::c_void;
use std::slice::from_raw_parts;

use crate::cache::WorldInfoCache;
use crate::*;
Expand Down Expand Up @@ -45,6 +47,7 @@ pub struct SystemBuilder<'w> {
// we need to keep these in memory until after build
name_temp: String,
expr_temp: String,
set_context: bool,

next_term_index: usize,
}
Expand Down Expand Up @@ -78,9 +81,32 @@ impl<'w> SystemBuilder<'w> {
name_temp: "".to_owned(),
expr_temp: "".to_owned(),
next_term_index: 0,
set_context: false,
}
}

pub fn context<T>(mut self, value:T) -> Self {
assert!(!self.set_context);
let layout = Layout::new::<T>();
unsafe {
let ptr = std::alloc::alloc(layout);
if ptr.is_null() {
handle_alloc_error(layout);
}
*(ptr as *mut T) = value;
self.desc.ctx = ptr.cast::<c_void>();
};
self.set_context = true;
self
}

pub fn context_ptr(mut self, ptr: *mut c_void) -> Self {
assert!(!self.set_context);
self.desc.ctx = ptr;
self.set_context = true;
self
}

pub fn named(mut self, name: &str) -> Self {
self.name_temp = name.to_owned();
self
Expand Down Expand Up @@ -297,6 +323,13 @@ impl Iter {
Self::get_field::<A>(self, index)
}

pub unsafe fn get_context<'a, T>(&'a self) -> &'a T {
let context = (*self.it).ctx.cast::<T>()
.as_ref()
.unwrap();
&context
}

fn get_field<T: Component>(&self, index: i32) -> Column<T> {
// validate that types match. could avoid this in Release builds perhaps to get max perf
let field_id = unsafe { ecs_field_id(self.it, index) };
Expand Down Expand Up @@ -358,6 +391,12 @@ impl Iter {

ColumnDynamic::new(array, count, size, is_shared)
}

#[inline]
pub fn raw_fields<'a>(&'a self) -> &'a [ecs_term_t] {
let terms = unsafe { (*self.it).terms };
unsafe { from_raw_parts(terms, self.field_count() as usize) }
}
}

pub type SystemCallback = unsafe extern "C" fn(*mut ecs_iter_t);
Expand Down