use super::{BasicSet, Context, Error, LibISLError};
use libc::uintptr_t;
pub struct Cell {
pub ptr: uintptr_t,
pub should_free_on_drop: bool,
}
extern "C" {
fn isl_cell_free(cell: uintptr_t) -> uintptr_t;
fn isl_cell_get_ctx(cell: uintptr_t) -> uintptr_t;
fn isl_cell_get_domain(cell: uintptr_t) -> uintptr_t;
}
impl Cell {
pub fn free(self) -> Result<Cell, LibISLError> {
let cell = self;
let isl_rs_ctx = cell.get_ctx();
let mut cell = cell;
cell.do_not_free_on_drop();
let cell = cell.ptr;
let isl_rs_result = unsafe { isl_cell_free(cell) };
let isl_rs_result = Cell { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn get_ctx(&self) -> Context {
let cell = self;
let cell = cell.ptr;
let isl_rs_result = unsafe { isl_cell_get_ctx(cell) };
let isl_rs_result = Context { ptr: isl_rs_result,
should_free_on_drop: false };
isl_rs_result
}
pub fn get_domain(&self) -> Result<BasicSet, LibISLError> {
let cell = self;
let isl_rs_ctx = cell.get_ctx();
let cell = cell.ptr;
let isl_rs_result = unsafe { isl_cell_get_domain(cell) };
let isl_rs_result = BasicSet { ptr: isl_rs_result,
should_free_on_drop: true };
let err = isl_rs_ctx.last_error();
if err != Error::None_ {
let err_msg = isl_rs_ctx.last_error_msg();
isl_rs_ctx.reset_error();
return Err(LibISLError::new(err, err_msg));
}
Ok(isl_rs_result)
}
pub fn do_not_free_on_drop(&mut self) {
self.should_free_on_drop = false;
}
}
impl Drop for Cell {
fn drop(&mut self) {
if self.should_free_on_drop {
unsafe {
isl_cell_free(self.ptr);
}
}
}
}