1use super::{Context, Error, IdList, LibISLError};
5use libc::uintptr_t;
6use std::ffi::{CStr, CString};
7use std::os::raw::c_char;
8
9pub struct Id {
11 pub ptr: uintptr_t,
12 pub should_free_on_drop: bool,
13}
14
15extern "C" {
16
17 fn isl_id_copy(id: uintptr_t) -> uintptr_t;
18
19 fn isl_id_dump(id: uintptr_t) -> ();
20
21 fn isl_id_free(id: uintptr_t) -> uintptr_t;
22
23 fn isl_id_get_ctx(id: uintptr_t) -> uintptr_t;
24
25 fn isl_id_get_free_user(id: uintptr_t) -> ();
26
27 fn isl_id_get_hash(id: uintptr_t) -> u32;
28
29 fn isl_id_get_name(id: uintptr_t) -> *const c_char;
30
31 fn isl_id_read_from_str(ctx: uintptr_t, str_: *const c_char) -> uintptr_t;
32
33 fn isl_id_to_list(el: uintptr_t) -> uintptr_t;
34
35 fn isl_id_to_str(id: uintptr_t) -> *const c_char;
36
37}
38
39impl Id {
40 pub fn copy(&self) -> Result<Id, LibISLError> {
42 let id = self;
43 let isl_rs_ctx = id.get_ctx();
44 let id = id.ptr;
45 let isl_rs_result = unsafe { isl_id_copy(id) };
46 let isl_rs_result = Id { ptr: isl_rs_result,
47 should_free_on_drop: true };
48 let err = isl_rs_ctx.last_error();
49 if err != Error::None_ {
50 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
51 }
52 Ok(isl_rs_result)
53 }
54
55 pub fn dump(&self) -> Result<(), LibISLError> {
57 let id = self;
58 let isl_rs_ctx = id.get_ctx();
59 let id = id.ptr;
60 let isl_rs_result = unsafe { isl_id_dump(id) };
61 let err = isl_rs_ctx.last_error();
62 if err != Error::None_ {
63 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
64 }
65 Ok(isl_rs_result)
66 }
67
68 pub fn free(self) -> Result<Id, LibISLError> {
70 let id = self;
71 let isl_rs_ctx = id.get_ctx();
72 let mut id = id;
73 id.do_not_free_on_drop();
74 let id = id.ptr;
75 let isl_rs_result = unsafe { isl_id_free(id) };
76 let isl_rs_result = Id { ptr: isl_rs_result,
77 should_free_on_drop: true };
78 let err = isl_rs_ctx.last_error();
79 if err != Error::None_ {
80 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
81 }
82 Ok(isl_rs_result)
83 }
84
85 pub fn get_ctx(&self) -> Context {
87 let id = self;
88 let id = id.ptr;
89 let isl_rs_result = unsafe { isl_id_get_ctx(id) };
90 let isl_rs_result = Context { ptr: isl_rs_result,
91 should_free_on_drop: false };
92 isl_rs_result
93 }
94
95 pub fn get_free_user(&self) -> Result<(), LibISLError> {
97 let id = self;
98 let isl_rs_ctx = id.get_ctx();
99 let id = id.ptr;
100 let isl_rs_result = unsafe { isl_id_get_free_user(id) };
101 let err = isl_rs_ctx.last_error();
102 if err != Error::None_ {
103 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
104 }
105 Ok(isl_rs_result)
106 }
107
108 pub fn get_hash(&self) -> Result<u32, LibISLError> {
110 let id = self;
111 let isl_rs_ctx = id.get_ctx();
112 let id = id.ptr;
113 let isl_rs_result = unsafe { isl_id_get_hash(id) };
114 let err = isl_rs_ctx.last_error();
115 if err != Error::None_ {
116 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
117 }
118 Ok(isl_rs_result)
119 }
120
121 pub fn get_name(&self) -> Result<&str, LibISLError> {
123 let id = self;
124 let isl_rs_ctx = id.get_ctx();
125 let id = id.ptr;
126 let isl_rs_result = unsafe { isl_id_get_name(id) };
127 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
128 let isl_rs_result = isl_rs_result.to_str().unwrap();
129 let err = isl_rs_ctx.last_error();
130 if err != Error::None_ {
131 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
132 }
133 Ok(isl_rs_result)
134 }
135
136 pub fn read_from_str(ctx: &Context, str_: &str) -> Result<Id, LibISLError> {
138 let isl_rs_ctx = Context { ptr: ctx.ptr,
139 should_free_on_drop: false };
140 let ctx = ctx.ptr;
141 let str_ = CString::new(str_).unwrap();
142 let str_ = str_.as_ptr();
143 let isl_rs_result = unsafe { isl_id_read_from_str(ctx, str_) };
144 let isl_rs_result = Id { ptr: isl_rs_result,
145 should_free_on_drop: true };
146 let err = isl_rs_ctx.last_error();
147 if err != Error::None_ {
148 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
149 }
150 Ok(isl_rs_result)
151 }
152
153 pub fn to_list(self) -> Result<IdList, LibISLError> {
155 let el = self;
156 let isl_rs_ctx = el.get_ctx();
157 let mut el = el;
158 el.do_not_free_on_drop();
159 let el = el.ptr;
160 let isl_rs_result = unsafe { isl_id_to_list(el) };
161 let isl_rs_result = IdList { ptr: isl_rs_result,
162 should_free_on_drop: true };
163 let err = isl_rs_ctx.last_error();
164 if err != Error::None_ {
165 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
166 }
167 Ok(isl_rs_result)
168 }
169
170 pub fn to_str(&self) -> Result<&str, LibISLError> {
172 let id = self;
173 let isl_rs_ctx = id.get_ctx();
174 let id = id.ptr;
175 let isl_rs_result = unsafe { isl_id_to_str(id) };
176 let isl_rs_result = unsafe { CStr::from_ptr(isl_rs_result) };
177 let isl_rs_result = isl_rs_result.to_str().unwrap();
178 let err = isl_rs_ctx.last_error();
179 if err != Error::None_ {
180 return Err(LibISLError::new(err, isl_rs_ctx.last_error_msg()));
181 }
182 Ok(isl_rs_result)
183 }
184
185 pub fn do_not_free_on_drop(&mut self) {
187 self.should_free_on_drop = false;
188 }
189}
190
191impl Drop for Id {
192 fn drop(&mut self) {
193 if self.should_free_on_drop {
194 unsafe {
195 isl_id_free(self.ptr);
196 }
197 }
198 }
199}