1use super::{BasicSet, Context, Error, LibISLError};
5use libc::uintptr_t;
6
7pub struct Cell {
9 pub ptr: uintptr_t,
10 pub should_free_on_drop: bool,
11}
12
13extern "C" {
14
15 fn isl_cell_free(cell: uintptr_t) -> uintptr_t;
16
17 fn isl_cell_get_ctx(cell: uintptr_t) -> uintptr_t;
18
19 fn isl_cell_get_domain(cell: uintptr_t) -> uintptr_t;
20
21}
22
23impl Cell {
24 pub fn free(self) -> Result<Cell, LibISLError> {
26 let cell = self;
27 let isl_rs_ctx = cell.get_ctx();
28 let mut cell = cell;
29 cell.do_not_free_on_drop();
30 let cell = cell.ptr;
31 let isl_rs_result = unsafe { isl_cell_free(cell) };
32 let isl_rs_result = Cell { ptr: isl_rs_result,
33 should_free_on_drop: true };
34 let err = isl_rs_ctx.last_error();
35 if err != Error::None_ {
36 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
37 }
38 Ok(isl_rs_result)
39 }
40
41 pub fn get_ctx(&self) -> Context {
43 let cell = self;
44 let cell = cell.ptr;
45 let isl_rs_result = unsafe { isl_cell_get_ctx(cell) };
46 let isl_rs_result = Context { ptr: isl_rs_result,
47 should_free_on_drop: false };
48 isl_rs_result
49 }
50
51 pub fn get_domain(&self) -> Result<BasicSet, LibISLError> {
53 let cell = self;
54 let isl_rs_ctx = cell.get_ctx();
55 let cell = cell.ptr;
56 let isl_rs_result = unsafe { isl_cell_get_domain(cell) };
57 let isl_rs_result = BasicSet { ptr: isl_rs_result,
58 should_free_on_drop: true };
59 let err = isl_rs_ctx.last_error();
60 if err != Error::None_ {
61 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
62 }
63 Ok(isl_rs_result)
64 }
65
66 pub fn do_not_free_on_drop(&mut self) {
68 self.should_free_on_drop = false;
69 }
70}
71
72impl Drop for Cell {
73 fn drop(&mut self) {
74 if self.should_free_on_drop {
75 unsafe {
76 isl_cell_free(self.ptr);
77 }
78 }
79 }
80}