1use super::{Context, Error, Id, LibISLError};
5use libc::uintptr_t;
6use std::ffi::{CStr, CString};
7use std::os::raw::c_char;
8
9pub struct IdList {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_id_list_add(list: uintptr_t, el: uintptr_t) -> uintptr_t;
18
19 fn isl_id_list_alloc(ctx: uintptr_t, n: i32) -> uintptr_t;
20
21 fn isl_id_list_clear(list: uintptr_t) -> uintptr_t;
22
23 fn isl_id_list_concat(list1: uintptr_t, list2: uintptr_t) -> uintptr_t;
24
25 fn isl_id_list_copy(list: uintptr_t) -> uintptr_t;
26
27 fn isl_id_list_drop(list: uintptr_t, first: u32, n: u32) -> uintptr_t;
28
29 fn isl_id_list_dump(list: uintptr_t) -> ();
30
31 fn isl_id_list_free(list: uintptr_t) -> uintptr_t;
32
33 fn isl_id_list_from_id(el: uintptr_t) -> uintptr_t;
34
35 fn isl_id_list_get_at(list: uintptr_t, index: i32) -> uintptr_t;
36
37 fn isl_id_list_get_ctx(list: uintptr_t) -> uintptr_t;
38
39 fn isl_id_list_get_id(list: uintptr_t, index: i32) -> uintptr_t;
40
41 fn isl_id_list_insert(list: uintptr_t, pos: u32, el: uintptr_t) -> uintptr_t;
42
43 fn isl_id_list_n_id(list: uintptr_t) -> i32;
44
45 fn isl_id_list_read_from_str(ctx: uintptr_t, str_: *const c_char) -> uintptr_t;
46
47 fn isl_id_list_reverse(list: uintptr_t) -> uintptr_t;
48
49 fn isl_id_list_set_at(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
50
51 fn isl_id_list_set_id(list: uintptr_t, index: i32, el: uintptr_t) -> uintptr_t;
52
53 fn isl_id_list_size(list: uintptr_t) -> i32;
54
55 fn isl_id_list_swap(list: uintptr_t, pos1: u32, pos2: u32) -> uintptr_t;
56
57 fn isl_id_list_to_str(list: uintptr_t) -> *const c_char;
58
59}
60
61impl IdList {
62 pub fn add(self, el: Id) -> Result<IdList, LibISLError> {
64 let list = self;
65 let isl_rs_ctx = list.get_ctx();
66 let mut list = list;
67 list.do_not_free_on_drop();
68 let list = list.ptr;
69 let mut el = el;
70 el.do_not_free_on_drop();
71 let el = el.ptr;
72 let isl_rs_result = unsafe { isl_id_list_add(list, el) };
73 let isl_rs_result = IdList { ptr: isl_rs_result,
74 should_free_on_drop: true };
75 let err = isl_rs_ctx.last_error();
76 if err != Error::None_ {
77 let err_msg = isl_rs_ctx.last_error_msg();
78 isl_rs_ctx.reset_error();
79 return Err(LibISLError::new(err, err_msg));
80 }
81 Ok(isl_rs_result)
82 }
83
84 pub fn alloc(ctx: &Context, n: i32) -> Result<IdList, LibISLError> {
86 let isl_rs_ctx = Context { ptr: ctx.ptr,
87 should_free_on_drop: false };
88 let ctx = ctx.ptr;
89 let isl_rs_result = unsafe { isl_id_list_alloc(ctx, n) };
90 let isl_rs_result = IdList { ptr: isl_rs_result,
91 should_free_on_drop: true };
92 let err = isl_rs_ctx.last_error();
93 if err != Error::None_ {
94 let err_msg = isl_rs_ctx.last_error_msg();
95 isl_rs_ctx.reset_error();
96 return Err(LibISLError::new(err, err_msg));
97 }
98 Ok(isl_rs_result)
99 }
100
101 pub fn clear(self) -> Result<IdList, LibISLError> {
103 let list = self;
104 let isl_rs_ctx = list.get_ctx();
105 let mut list = list;
106 list.do_not_free_on_drop();
107 let list = list.ptr;
108 let isl_rs_result = unsafe { isl_id_list_clear(list) };
109 let isl_rs_result = IdList { ptr: isl_rs_result,
110 should_free_on_drop: true };
111 let err = isl_rs_ctx.last_error();
112 if err != Error::None_ {
113 let err_msg = isl_rs_ctx.last_error_msg();
114 isl_rs_ctx.reset_error();
115 return Err(LibISLError::new(err, err_msg));
116 }
117 Ok(isl_rs_result)
118 }
119
120 pub fn concat(self, list2: IdList) -> Result<IdList, LibISLError> {
122 let list1 = self;
123 let isl_rs_ctx = list1.get_ctx();
124 let mut list1 = list1;
125 list1.do_not_free_on_drop();
126 let list1 = list1.ptr;
127 let mut list2 = list2;
128 list2.do_not_free_on_drop();
129 let list2 = list2.ptr;
130 let isl_rs_result = unsafe { isl_id_list_concat(list1, list2) };
131 let isl_rs_result = IdList { ptr: isl_rs_result,
132 should_free_on_drop: true };
133 let err = isl_rs_ctx.last_error();
134 if err != Error::None_ {
135 let err_msg = isl_rs_ctx.last_error_msg();
136 isl_rs_ctx.reset_error();
137 return Err(LibISLError::new(err, err_msg));
138 }
139 Ok(isl_rs_result)
140 }
141
142 pub fn copy(&self) -> Result<IdList, LibISLError> {
144 let list = self;
145 let isl_rs_ctx = list.get_ctx();
146 let list = list.ptr;
147 let isl_rs_result = unsafe { isl_id_list_copy(list) };
148 let isl_rs_result = IdList { ptr: isl_rs_result,
149 should_free_on_drop: true };
150 let err = isl_rs_ctx.last_error();
151 if err != Error::None_ {
152 let err_msg = isl_rs_ctx.last_error_msg();
153 isl_rs_ctx.reset_error();
154 return Err(LibISLError::new(err, err_msg));
155 }
156 Ok(isl_rs_result)
157 }
158
159 pub fn drop(self, first: u32, n: u32) -> Result<IdList, LibISLError> {
161 let list = self;
162 let isl_rs_ctx = list.get_ctx();
163 let mut list = list;
164 list.do_not_free_on_drop();
165 let list = list.ptr;
166 let isl_rs_result = unsafe { isl_id_list_drop(list, first, n) };
167 let isl_rs_result = IdList { ptr: isl_rs_result,
168 should_free_on_drop: true };
169 let err = isl_rs_ctx.last_error();
170 if err != Error::None_ {
171 let err_msg = isl_rs_ctx.last_error_msg();
172 isl_rs_ctx.reset_error();
173 return Err(LibISLError::new(err, err_msg));
174 }
175 Ok(isl_rs_result)
176 }
177
178 pub fn dump(&self) -> Result<(), LibISLError> {
180 let list = self;
181 let isl_rs_ctx = list.get_ctx();
182 let list = list.ptr;
183 let isl_rs_result = unsafe { isl_id_list_dump(list) };
184 let err = isl_rs_ctx.last_error();
185 if err != Error::None_ {
186 let err_msg = isl_rs_ctx.last_error_msg();
187 isl_rs_ctx.reset_error();
188 return Err(LibISLError::new(err, err_msg));
189 }
190 Ok(isl_rs_result)
191 }
192
193 pub fn free(self) -> Result<IdList, LibISLError> {
195 let list = self;
196 let isl_rs_ctx = list.get_ctx();
197 let mut list = list;
198 list.do_not_free_on_drop();
199 let list = list.ptr;
200 let isl_rs_result = unsafe { isl_id_list_free(list) };
201 let isl_rs_result = IdList { ptr: isl_rs_result,
202 should_free_on_drop: true };
203 let err = isl_rs_ctx.last_error();
204 if err != Error::None_ {
205 let err_msg = isl_rs_ctx.last_error_msg();
206 isl_rs_ctx.reset_error();
207 return Err(LibISLError::new(err, err_msg));
208 }
209 Ok(isl_rs_result)
210 }
211
212 pub fn from_id(el: Id) -> Result<IdList, LibISLError> {
214 let isl_rs_ctx = el.get_ctx();
215 let mut el = el;
216 el.do_not_free_on_drop();
217 let el = el.ptr;
218 let isl_rs_result = unsafe { isl_id_list_from_id(el) };
219 let isl_rs_result = IdList { 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 get_at(&self, index: i32) -> Result<Id, LibISLError> {
232 let list = self;
233 let isl_rs_ctx = list.get_ctx();
234 let list = list.ptr;
235 let isl_rs_result = unsafe { isl_id_list_get_at(list, index) };
236 let isl_rs_result = Id { ptr: isl_rs_result,
237 should_free_on_drop: true };
238 let err = isl_rs_ctx.last_error();
239 if err != Error::None_ {
240 let err_msg = isl_rs_ctx.last_error_msg();
241 isl_rs_ctx.reset_error();
242 return Err(LibISLError::new(err, err_msg));
243 }
244 Ok(isl_rs_result)
245 }
246
247 pub fn get_ctx(&self) -> Context {
249 let list = self;
250 let list = list.ptr;
251 let isl_rs_result = unsafe { isl_id_list_get_ctx(list) };
252 let isl_rs_result = Context { ptr: isl_rs_result,
253 should_free_on_drop: false };
254 isl_rs_result
255 }
256
257 pub fn get_id(&self, index: i32) -> Result<Id, LibISLError> {
259 let list = self;
260 let isl_rs_ctx = list.get_ctx();
261 let list = list.ptr;
262 let isl_rs_result = unsafe { isl_id_list_get_id(list, index) };
263 let isl_rs_result = Id { ptr: isl_rs_result,
264 should_free_on_drop: true };
265 let err = isl_rs_ctx.last_error();
266 if err != Error::None_ {
267 let err_msg = isl_rs_ctx.last_error_msg();
268 isl_rs_ctx.reset_error();
269 return Err(LibISLError::new(err, err_msg));
270 }
271 Ok(isl_rs_result)
272 }
273
274 pub fn insert(self, pos: u32, el: Id) -> Result<IdList, LibISLError> {
276 let list = self;
277 let isl_rs_ctx = list.get_ctx();
278 let mut list = list;
279 list.do_not_free_on_drop();
280 let list = list.ptr;
281 let mut el = el;
282 el.do_not_free_on_drop();
283 let el = el.ptr;
284 let isl_rs_result = unsafe { isl_id_list_insert(list, pos, el) };
285 let isl_rs_result = IdList { ptr: isl_rs_result,
286 should_free_on_drop: true };
287 let err = isl_rs_ctx.last_error();
288 if err != Error::None_ {
289 let err_msg = isl_rs_ctx.last_error_msg();
290 isl_rs_ctx.reset_error();
291 return Err(LibISLError::new(err, err_msg));
292 }
293 Ok(isl_rs_result)
294 }
295
296 pub fn n_id(&self) -> Result<i32, LibISLError> {
298 let list = self;
299 let isl_rs_ctx = list.get_ctx();
300 let list = list.ptr;
301 let isl_rs_result = unsafe { isl_id_list_n_id(list) };
302 let err = isl_rs_ctx.last_error();
303 if err != Error::None_ {
304 let err_msg = isl_rs_ctx.last_error_msg();
305 isl_rs_ctx.reset_error();
306 return Err(LibISLError::new(err, err_msg));
307 }
308 Ok(isl_rs_result)
309 }
310
311 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<IdList, LibISLError> {
313 let isl_rs_ctx = Context { ptr: ctx.ptr,
314 should_free_on_drop: false };
315 let ctx = ctx.ptr;
316 let str_ = CString::new(str_).unwrap();
317 let str_ = str_.as_ptr();
318 let isl_rs_result = unsafe { isl_id_list_read_from_str(ctx, str_) };
319 let isl_rs_result = IdList { ptr: isl_rs_result,
320 should_free_on_drop: true };
321 let err = isl_rs_ctx.last_error();
322 if err != Error::None_ {
323 let err_msg = isl_rs_ctx.last_error_msg();
324 isl_rs_ctx.reset_error();
325 return Err(LibISLError::new(err, err_msg));
326 }
327 Ok(isl_rs_result)
328 }
329
330 pub fn reverse(self) -> Result<IdList, LibISLError> {
332 let list = self;
333 let isl_rs_ctx = list.get_ctx();
334 let mut list = list;
335 list.do_not_free_on_drop();
336 let list = list.ptr;
337 let isl_rs_result = unsafe { isl_id_list_reverse(list) };
338 let isl_rs_result = IdList { ptr: isl_rs_result,
339 should_free_on_drop: true };
340 let err = isl_rs_ctx.last_error();
341 if err != Error::None_ {
342 let err_msg = isl_rs_ctx.last_error_msg();
343 isl_rs_ctx.reset_error();
344 return Err(LibISLError::new(err, err_msg));
345 }
346 Ok(isl_rs_result)
347 }
348
349 pub fn set_at(self, index: i32, el: Id) -> Result<IdList, LibISLError> {
351 let list = self;
352 let isl_rs_ctx = list.get_ctx();
353 let mut list = list;
354 list.do_not_free_on_drop();
355 let list = list.ptr;
356 let mut el = el;
357 el.do_not_free_on_drop();
358 let el = el.ptr;
359 let isl_rs_result = unsafe { isl_id_list_set_at(list, index, el) };
360 let isl_rs_result = IdList { ptr: isl_rs_result,
361 should_free_on_drop: true };
362 let err = isl_rs_ctx.last_error();
363 if err != Error::None_ {
364 let err_msg = isl_rs_ctx.last_error_msg();
365 isl_rs_ctx.reset_error();
366 return Err(LibISLError::new(err, err_msg));
367 }
368 Ok(isl_rs_result)
369 }
370
371 pub fn set_id(self, index: i32, el: Id) -> Result<IdList, LibISLError> {
373 let list = self;
374 let isl_rs_ctx = list.get_ctx();
375 let mut list = list;
376 list.do_not_free_on_drop();
377 let list = list.ptr;
378 let mut el = el;
379 el.do_not_free_on_drop();
380 let el = el.ptr;
381 let isl_rs_result = unsafe { isl_id_list_set_id(list, index, el) };
382 let isl_rs_result = IdList { ptr: isl_rs_result,
383 should_free_on_drop: true };
384 let err = isl_rs_ctx.last_error();
385 if err != Error::None_ {
386 let err_msg = isl_rs_ctx.last_error_msg();
387 isl_rs_ctx.reset_error();
388 return Err(LibISLError::new(err, err_msg));
389 }
390 Ok(isl_rs_result)
391 }
392
393 pub fn size(&self) -> Result<i32, LibISLError> {
395 let list = self;
396 let isl_rs_ctx = list.get_ctx();
397 let list = list.ptr;
398 let isl_rs_result = unsafe { isl_id_list_size(list) };
399 let err = isl_rs_ctx.last_error();
400 if err != Error::None_ {
401 let err_msg = isl_rs_ctx.last_error_msg();
402 isl_rs_ctx.reset_error();
403 return Err(LibISLError::new(err, err_msg));
404 }
405 Ok(isl_rs_result)
406 }
407
408 pub fn swap(self, pos1: u32, pos2: u32) -> Result<IdList, LibISLError> {
410 let list = self;
411 let isl_rs_ctx = list.get_ctx();
412 let mut list = list;
413 list.do_not_free_on_drop();
414 let list = list.ptr;
415 let isl_rs_result = unsafe { isl_id_list_swap(list, pos1, pos2) };
416 let isl_rs_result = IdList { ptr: isl_rs_result,
417 should_free_on_drop: true };
418 let err = isl_rs_ctx.last_error();
419 if err != Error::None_ {
420 let err_msg = isl_rs_ctx.last_error_msg();
421 isl_rs_ctx.reset_error();
422 return Err(LibISLError::new(err, err_msg));
423 }
424 Ok(isl_rs_result)
425 }
426
427 pub fn to_str(&self) -> Result<&str, LibISLError> {
429 let list = self;
430 let isl_rs_ctx = list.get_ctx();
431 let list = list.ptr;
432 let isl_rs_result = unsafe { isl_id_list_to_str(list) };
433 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
434 let isl_rs_result = isl_rs_result.to_str().unwrap();
435 let err = isl_rs_ctx.last_error();
436 if err != Error::None_ {
437 let err_msg = isl_rs_ctx.last_error_msg();
438 isl_rs_ctx.reset_error();
439 return Err(LibISLError::new(err, err_msg));
440 }
441 Ok(isl_rs_result)
442 }
443
444 pub fn do_not_free_on_drop(&mut self) {
447 self.should_free_on_drop = false;
448 }
449}
450
451impl Drop for IdList {
452 fn drop(&mut self) {
453 if self.should_free_on_drop {
454 unsafe {
455 isl_id_list_free(self.ptr);
456 }
457 }
458 }
459}