1use super::{Context, DimType, Error, LibISLError, MultiVal, Set, Space, Val};
5use libc::uintptr_t;
6use std::ffi::CStr;
7use std::os::raw::c_char;
8
9pub struct Point {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_point_add_ui(pnt: uintptr_t, type_: i32, pos: i32, val: u32) -> uintptr_t;
18
19 fn isl_point_copy(pnt: uintptr_t) -> uintptr_t;
20
21 fn isl_point_dump(pnt: uintptr_t) -> ();
22
23 fn isl_point_free(pnt: uintptr_t) -> uintptr_t;
24
25 fn isl_point_get_coordinate_val(pnt: uintptr_t, type_: i32, pos: i32) -> uintptr_t;
26
27 fn isl_point_get_ctx(pnt: uintptr_t) -> uintptr_t;
28
29 fn isl_point_get_multi_val(pnt: uintptr_t) -> uintptr_t;
30
31 fn isl_point_get_space(pnt: uintptr_t) -> uintptr_t;
32
33 fn isl_point_is_void(pnt: uintptr_t) -> i32;
34
35 fn isl_point_set_coordinate_val(pnt: uintptr_t, type_: i32, pos: i32, v: uintptr_t)
36 -> uintptr_t;
37
38 fn isl_point_sub_ui(pnt: uintptr_t, type_: i32, pos: i32, val: u32) -> uintptr_t;
39
40 fn isl_point_to_set(pnt: uintptr_t) -> uintptr_t;
41
42 fn isl_point_to_str(pnt: uintptr_t) -> *const c_char;
43
44 fn isl_point_void(space: uintptr_t) -> uintptr_t;
45
46 fn isl_point_zero(space: uintptr_t) -> uintptr_t;
47
48}
49
50impl Point {
51 pub fn add_ui(self, type_: DimType, pos: i32, val: u32) -> Result<Point, LibISLError> {
53 let pnt = self;
54 let isl_rs_ctx = pnt.get_ctx();
55 let mut pnt = pnt;
56 pnt.do_not_free_on_drop();
57 let pnt = pnt.ptr;
58 let type_ = type_.to_i32();
59 let isl_rs_result = unsafe { isl_point_add_ui(pnt, type_, pos, val) };
60 let isl_rs_result = Point { ptr: isl_rs_result,
61 should_free_on_drop: true };
62 let err = isl_rs_ctx.last_error();
63 if err != Error::None_ {
64 let err_msg = isl_rs_ctx.last_error_msg();
65 isl_rs_ctx.reset_error();
66 return Err(LibISLError::new(err, err_msg));
67 }
68 Ok(isl_rs_result)
69 }
70
71 pub fn copy(&self) -> Result<Point, LibISLError> {
73 let pnt = self;
74 let isl_rs_ctx = pnt.get_ctx();
75 let pnt = pnt.ptr;
76 let isl_rs_result = unsafe { isl_point_copy(pnt) };
77 let isl_rs_result = Point { ptr: isl_rs_result,
78 should_free_on_drop: true };
79 let err = isl_rs_ctx.last_error();
80 if err != Error::None_ {
81 let err_msg = isl_rs_ctx.last_error_msg();
82 isl_rs_ctx.reset_error();
83 return Err(LibISLError::new(err, err_msg));
84 }
85 Ok(isl_rs_result)
86 }
87
88 pub fn dump(&self) -> Result<(), LibISLError> {
90 let pnt = self;
91 let isl_rs_ctx = pnt.get_ctx();
92 let pnt = pnt.ptr;
93 let isl_rs_result = unsafe { isl_point_dump(pnt) };
94 let err = isl_rs_ctx.last_error();
95 if err != Error::None_ {
96 let err_msg = isl_rs_ctx.last_error_msg();
97 isl_rs_ctx.reset_error();
98 return Err(LibISLError::new(err, err_msg));
99 }
100 Ok(isl_rs_result)
101 }
102
103 pub fn free(self) -> Result<Point, LibISLError> {
105 let pnt = self;
106 let isl_rs_ctx = pnt.get_ctx();
107 let mut pnt = pnt;
108 pnt.do_not_free_on_drop();
109 let pnt = pnt.ptr;
110 let isl_rs_result = unsafe { isl_point_free(pnt) };
111 let isl_rs_result = Point { ptr: isl_rs_result,
112 should_free_on_drop: true };
113 let err = isl_rs_ctx.last_error();
114 if err != Error::None_ {
115 let err_msg = isl_rs_ctx.last_error_msg();
116 isl_rs_ctx.reset_error();
117 return Err(LibISLError::new(err, err_msg));
118 }
119 Ok(isl_rs_result)
120 }
121
122 pub fn get_coordinate_val(&self, type_: DimType, pos: i32) -> Result<Val, LibISLError> {
124 let pnt = self;
125 let isl_rs_ctx = pnt.get_ctx();
126 let pnt = pnt.ptr;
127 let type_ = type_.to_i32();
128 let isl_rs_result = unsafe { isl_point_get_coordinate_val(pnt, type_, pos) };
129 let isl_rs_result = Val { ptr: isl_rs_result,
130 should_free_on_drop: true };
131 let err = isl_rs_ctx.last_error();
132 if err != Error::None_ {
133 let err_msg = isl_rs_ctx.last_error_msg();
134 isl_rs_ctx.reset_error();
135 return Err(LibISLError::new(err, err_msg));
136 }
137 Ok(isl_rs_result)
138 }
139
140 pub fn get_ctx(&self) -> Context {
142 let pnt = self;
143 let pnt = pnt.ptr;
144 let isl_rs_result = unsafe { isl_point_get_ctx(pnt) };
145 let isl_rs_result = Context { ptr: isl_rs_result,
146 should_free_on_drop: false };
147 isl_rs_result
148 }
149
150 pub fn get_multi_val(&self) -> Result<MultiVal, LibISLError> {
152 let pnt = self;
153 let isl_rs_ctx = pnt.get_ctx();
154 let pnt = pnt.ptr;
155 let isl_rs_result = unsafe { isl_point_get_multi_val(pnt) };
156 let isl_rs_result = MultiVal { ptr: isl_rs_result,
157 should_free_on_drop: true };
158 let err = isl_rs_ctx.last_error();
159 if err != Error::None_ {
160 let err_msg = isl_rs_ctx.last_error_msg();
161 isl_rs_ctx.reset_error();
162 return Err(LibISLError::new(err, err_msg));
163 }
164 Ok(isl_rs_result)
165 }
166
167 pub fn get_space(&self) -> Result<Space, LibISLError> {
169 let pnt = self;
170 let isl_rs_ctx = pnt.get_ctx();
171 let pnt = pnt.ptr;
172 let isl_rs_result = unsafe { isl_point_get_space(pnt) };
173 let isl_rs_result = Space { ptr: isl_rs_result,
174 should_free_on_drop: true };
175 let err = isl_rs_ctx.last_error();
176 if err != Error::None_ {
177 let err_msg = isl_rs_ctx.last_error_msg();
178 isl_rs_ctx.reset_error();
179 return Err(LibISLError::new(err, err_msg));
180 }
181 Ok(isl_rs_result)
182 }
183
184 pub fn is_void(&self) -> Result<bool, LibISLError> {
186 let pnt = self;
187 let isl_rs_ctx = pnt.get_ctx();
188 let pnt = pnt.ptr;
189 let isl_rs_result = unsafe { isl_point_is_void(pnt) };
190 let isl_rs_result = match isl_rs_result {
191 0 => false,
192 1 => true,
193 _ => {
194 return Err(LibISLError::new(Error::Unknown, "Got isl_bool = -1"));
195 }
196 };
197 let err = isl_rs_ctx.last_error();
198 if err != Error::None_ {
199 let err_msg = isl_rs_ctx.last_error_msg();
200 isl_rs_ctx.reset_error();
201 return Err(LibISLError::new(err, err_msg));
202 }
203 Ok(isl_rs_result)
204 }
205
206 pub fn set_coordinate_val(self, type_: DimType, pos: i32, v: Val)
208 -> Result<Point, LibISLError> {
209 let pnt = self;
210 let isl_rs_ctx = pnt.get_ctx();
211 let mut pnt = pnt;
212 pnt.do_not_free_on_drop();
213 let pnt = pnt.ptr;
214 let type_ = type_.to_i32();
215 let mut v = v;
216 v.do_not_free_on_drop();
217 let v = v.ptr;
218 let isl_rs_result = unsafe { isl_point_set_coordinate_val(pnt, type_, pos, v) };
219 let isl_rs_result = Point { ptr: isl_rs_result,
220 should_free_on_drop: true };
221 let err = isl_rs_ctx.last_error();
222 if err != Error::None_ {
223 let err_msg = isl_rs_ctx.last_error_msg();
224 isl_rs_ctx.reset_error();
225 return Err(LibISLError::new(err, err_msg));
226 }
227 Ok(isl_rs_result)
228 }
229
230 pub fn sub_ui(self, type_: DimType, pos: i32, val: u32) -> Result<Point, LibISLError> {
232 let pnt = self;
233 let isl_rs_ctx = pnt.get_ctx();
234 let mut pnt = pnt;
235 pnt.do_not_free_on_drop();
236 let pnt = pnt.ptr;
237 let type_ = type_.to_i32();
238 let isl_rs_result = unsafe { isl_point_sub_ui(pnt, type_, pos, val) };
239 let isl_rs_result = Point { ptr: isl_rs_result,
240 should_free_on_drop: true };
241 let err = isl_rs_ctx.last_error();
242 if err != Error::None_ {
243 let err_msg = isl_rs_ctx.last_error_msg();
244 isl_rs_ctx.reset_error();
245 return Err(LibISLError::new(err, err_msg));
246 }
247 Ok(isl_rs_result)
248 }
249
250 pub fn to_set(self) -> Result<Set, LibISLError> {
252 let pnt = self;
253 let isl_rs_ctx = pnt.get_ctx();
254 let mut pnt = pnt;
255 pnt.do_not_free_on_drop();
256 let pnt = pnt.ptr;
257 let isl_rs_result = unsafe { isl_point_to_set(pnt) };
258 let isl_rs_result = Set { ptr: isl_rs_result,
259 should_free_on_drop: true };
260 let err = isl_rs_ctx.last_error();
261 if err != Error::None_ {
262 let err_msg = isl_rs_ctx.last_error_msg();
263 isl_rs_ctx.reset_error();
264 return Err(LibISLError::new(err, err_msg));
265 }
266 Ok(isl_rs_result)
267 }
268
269 pub fn to_str(&self) -> Result<&str, LibISLError> {
271 let pnt = self;
272 let isl_rs_ctx = pnt.get_ctx();
273 let pnt = pnt.ptr;
274 let isl_rs_result = unsafe { isl_point_to_str(pnt) };
275 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
276 let isl_rs_result = isl_rs_result.to_str().unwrap();
277 let err = isl_rs_ctx.last_error();
278 if err != Error::None_ {
279 let err_msg = isl_rs_ctx.last_error_msg();
280 isl_rs_ctx.reset_error();
281 return Err(LibISLError::new(err, err_msg));
282 }
283 Ok(isl_rs_result)
284 }
285
286 pub fn void(space: Space) -> Result<Point, LibISLError> {
288 let isl_rs_ctx = space.get_ctx();
289 let mut space = space;
290 space.do_not_free_on_drop();
291 let space = space.ptr;
292 let isl_rs_result = unsafe { isl_point_void(space) };
293 let isl_rs_result = Point { ptr: isl_rs_result,
294 should_free_on_drop: true };
295 let err = isl_rs_ctx.last_error();
296 if err != Error::None_ {
297 let err_msg = isl_rs_ctx.last_error_msg();
298 isl_rs_ctx.reset_error();
299 return Err(LibISLError::new(err, err_msg));
300 }
301 Ok(isl_rs_result)
302 }
303
304 pub fn zero(space: Space) -> Result<Point, LibISLError> {
306 let isl_rs_ctx = space.get_ctx();
307 let mut space = space;
308 space.do_not_free_on_drop();
309 let space = space.ptr;
310 let isl_rs_result = unsafe { isl_point_zero(space) };
311 let isl_rs_result = Point { ptr: isl_rs_result,
312 should_free_on_drop: true };
313 let err = isl_rs_ctx.last_error();
314 if err != Error::None_ {
315 let err_msg = isl_rs_ctx.last_error_msg();
316 isl_rs_ctx.reset_error();
317 return Err(LibISLError::new(err, err_msg));
318 }
319 Ok(isl_rs_result)
320 }
321
322 pub fn do_not_free_on_drop(&mut self) {
325 self.should_free_on_drop = false;
326 }
327}
328
329impl Drop for Point {
330 fn drop(&mut self) {
331 if self.should_free_on_drop {
332 unsafe {
333 isl_point_free(self.ptr);
334 }
335 }
336 }
337}