Warning, /sdk/rust-qt-binding-generator/examples/todos/rust/src/interface.rs is written in an unsupported language. File is not indexed.

0001 /* generated by rust_qt_binding_generator */
0002 use libc::{c_char, c_ushort, c_int};
0003 use std::slice;
0004 use std::char::decode_utf16;
0005 
0006 use std::sync::Arc;
0007 use std::sync::atomic::{AtomicPtr, Ordering};
0008 use std::ptr::null;
0009 
0010 use implementation::*;
0011 
0012 
0013 #[repr(C)]
0014 pub struct COption<T> {
0015     data: T,
0016     some: bool,
0017 }
0018 
0019 impl<T> COption<T> {
0020     #![allow(dead_code)]
0021     fn into(self) -> Option<T> {
0022         if self.some {
0023             Some(self.data)
0024         } else {
0025             None
0026         }
0027     }
0028 }
0029 
0030 impl<T> From<Option<T>> for COption<T>
0031 where
0032     T: Default,
0033 {
0034     fn from(t: Option<T>) -> COption<T> {
0035         if let Some(v) = t {
0036             COption {
0037                 data: v,
0038                 some: true,
0039             }
0040         } else {
0041             COption {
0042                 data: T::default(),
0043                 some: false,
0044             }
0045         }
0046     }
0047 }
0048 
0049 
0050 pub enum QString {}
0051 
0052 fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
0053     let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
0054     let characters = decode_utf16(utf16.iter().cloned())
0055         .map(|r| r.unwrap());
0056     s.clear();
0057     s.extend(characters);
0058 }
0059 
0060 
0061 
0062 #[repr(C)]
0063 #[derive(PartialEq, Eq, Debug)]
0064 pub enum SortOrder {
0065     Ascending = 0,
0066     Descending = 1,
0067 }
0068 
0069 #[repr(C)]
0070 pub struct QModelIndex {
0071     row: c_int,
0072     internal_id: usize,
0073 }
0074 
0075 
0076 fn to_usize(n: c_int) -> usize {
0077     if n < 0 {
0078         panic!("Cannot cast {} to usize", n);
0079     }
0080     n as usize
0081 }
0082 
0083 
0084 fn to_c_int(n: usize) -> c_int {
0085     if n > c_int::max_value() as usize {
0086         panic!("Cannot cast {} to c_int", n);
0087     }
0088     n as c_int
0089 }
0090 
0091 
0092 pub struct TodosQObject {}
0093 
0094 pub struct TodosEmitter {
0095     qobject: Arc<AtomicPtr<TodosQObject>>,
0096     active_count_changed: fn(*mut TodosQObject),
0097     count_changed: fn(*mut TodosQObject),
0098     new_data_ready: fn(*mut TodosQObject),
0099 }
0100 
0101 unsafe impl Send for TodosEmitter {}
0102 
0103 impl TodosEmitter {
0104     /// Clone the emitter
0105     ///
0106     /// The emitter can only be cloned when it is mutable. The emitter calls
0107     /// into C++ code which may call into Rust again. If emmitting is possible
0108     /// from immutable structures, that might lead to access to a mutable
0109     /// reference. That is undefined behaviour and forbidden.
0110     pub fn clone(&mut self) -> TodosEmitter {
0111         TodosEmitter {
0112             qobject: self.qobject.clone(),
0113             active_count_changed: self.active_count_changed,
0114             count_changed: self.count_changed,
0115             new_data_ready: self.new_data_ready,
0116         }
0117     }
0118     fn clear(&self) {
0119         let n: *const TodosQObject = null();
0120         self.qobject.store(n as *mut TodosQObject, Ordering::SeqCst);
0121     }
0122     pub fn active_count_changed(&mut self) {
0123         let ptr = self.qobject.load(Ordering::SeqCst);
0124         if !ptr.is_null() {
0125             (self.active_count_changed)(ptr);
0126         }
0127     }
0128     pub fn count_changed(&mut self) {
0129         let ptr = self.qobject.load(Ordering::SeqCst);
0130         if !ptr.is_null() {
0131             (self.count_changed)(ptr);
0132         }
0133     }
0134     pub fn new_data_ready(&mut self) {
0135         let ptr = self.qobject.load(Ordering::SeqCst);
0136         if !ptr.is_null() {
0137             (self.new_data_ready)(ptr);
0138         }
0139     }
0140 }
0141 
0142 #[derive(Clone)]
0143 pub struct TodosList {
0144     qobject: *mut TodosQObject,
0145     layout_about_to_be_changed: fn(*mut TodosQObject),
0146     layout_changed: fn(*mut TodosQObject),
0147     data_changed: fn(*mut TodosQObject, usize, usize),
0148     begin_reset_model: fn(*mut TodosQObject),
0149     end_reset_model: fn(*mut TodosQObject),
0150     begin_insert_rows: fn(*mut TodosQObject, usize, usize),
0151     end_insert_rows: fn(*mut TodosQObject),
0152     begin_move_rows: fn(*mut TodosQObject, usize, usize, usize),
0153     end_move_rows: fn(*mut TodosQObject),
0154     begin_remove_rows: fn(*mut TodosQObject, usize, usize),
0155     end_remove_rows: fn(*mut TodosQObject),
0156 }
0157 
0158 impl TodosList {
0159     pub fn layout_about_to_be_changed(&mut self) {
0160         (self.layout_about_to_be_changed)(self.qobject);
0161     }
0162     pub fn layout_changed(&mut self) {
0163         (self.layout_changed)(self.qobject);
0164     }
0165     pub fn data_changed(&mut self, first: usize, last: usize) {
0166         (self.data_changed)(self.qobject, first, last);
0167     }
0168     pub fn begin_reset_model(&mut self) {
0169         (self.begin_reset_model)(self.qobject);
0170     }
0171     pub fn end_reset_model(&mut self) {
0172         (self.end_reset_model)(self.qobject);
0173     }
0174     pub fn begin_insert_rows(&mut self, first: usize, last: usize) {
0175         (self.begin_insert_rows)(self.qobject, first, last);
0176     }
0177     pub fn end_insert_rows(&mut self) {
0178         (self.end_insert_rows)(self.qobject);
0179     }
0180     pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) {
0181         (self.begin_move_rows)(self.qobject, first, last, destination);
0182     }
0183     pub fn end_move_rows(&mut self) {
0184         (self.end_move_rows)(self.qobject);
0185     }
0186     pub fn begin_remove_rows(&mut self, first: usize, last: usize) {
0187         (self.begin_remove_rows)(self.qobject, first, last);
0188     }
0189     pub fn end_remove_rows(&mut self) {
0190         (self.end_remove_rows)(self.qobject);
0191     }
0192 }
0193 
0194 pub trait TodosTrait {
0195     fn new(emit: TodosEmitter, model: TodosList) -> Self;
0196     fn emit(&mut self) -> &mut TodosEmitter;
0197     fn active_count(&self) -> u64;
0198     fn count(&self) -> u64;
0199     fn add(&mut self, description: String) -> ();
0200     fn clear_completed(&mut self) -> ();
0201     fn remove(&mut self, index: u64) -> bool;
0202     fn set_all(&mut self, completed: bool) -> ();
0203     fn row_count(&self) -> usize;
0204     fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false }
0205     fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false }
0206     fn can_fetch_more(&self) -> bool {
0207         false
0208     }
0209     fn fetch_more(&mut self) {}
0210     fn sort(&mut self, _: u8, _: SortOrder) {}
0211     fn completed(&self, index: usize) -> bool;
0212     fn set_completed(&mut self, index: usize, _: bool) -> bool;
0213     fn description(&self, index: usize) -> &str;
0214     fn set_description(&mut self, index: usize, _: String) -> bool;
0215 }
0216 
0217 #[no_mangle]
0218 pub extern "C" fn todos_new(
0219     todos: *mut TodosQObject,
0220     todos_active_count_changed: fn(*mut TodosQObject),
0221     todos_count_changed: fn(*mut TodosQObject),
0222     todos_new_data_ready: fn(*mut TodosQObject),
0223     todos_layout_about_to_be_changed: fn(*mut TodosQObject),
0224     todos_layout_changed: fn(*mut TodosQObject),
0225     todos_data_changed: fn(*mut TodosQObject, usize, usize),
0226     todos_begin_reset_model: fn(*mut TodosQObject),
0227     todos_end_reset_model: fn(*mut TodosQObject),
0228     todos_begin_insert_rows: fn(*mut TodosQObject, usize, usize),
0229     todos_end_insert_rows: fn(*mut TodosQObject),
0230     todos_begin_move_rows: fn(*mut TodosQObject, usize, usize, usize),
0231     todos_end_move_rows: fn(*mut TodosQObject),
0232     todos_begin_remove_rows: fn(*mut TodosQObject, usize, usize),
0233     todos_end_remove_rows: fn(*mut TodosQObject),
0234 ) -> *mut Todos {
0235     let todos_emit = TodosEmitter {
0236         qobject: Arc::new(AtomicPtr::new(todos)),
0237         active_count_changed: todos_active_count_changed,
0238         count_changed: todos_count_changed,
0239         new_data_ready: todos_new_data_ready,
0240     };
0241     let model = TodosList {
0242         qobject: todos,
0243         layout_about_to_be_changed: todos_layout_about_to_be_changed,
0244         layout_changed: todos_layout_changed,
0245         data_changed: todos_data_changed,
0246         begin_reset_model: todos_begin_reset_model,
0247         end_reset_model: todos_end_reset_model,
0248         begin_insert_rows: todos_begin_insert_rows,
0249         end_insert_rows: todos_end_insert_rows,
0250         begin_move_rows: todos_begin_move_rows,
0251         end_move_rows: todos_end_move_rows,
0252         begin_remove_rows: todos_begin_remove_rows,
0253         end_remove_rows: todos_end_remove_rows,
0254     };
0255     let d_todos = Todos::new(todos_emit, model);
0256     Box::into_raw(Box::new(d_todos))
0257 }
0258 
0259 #[no_mangle]
0260 pub unsafe extern "C" fn todos_free(ptr: *mut Todos) {
0261     Box::from_raw(ptr).emit().clear();
0262 }
0263 
0264 #[no_mangle]
0265 pub unsafe extern "C" fn todos_active_count_get(ptr: *const Todos) -> u64 {
0266     (&*ptr).active_count()
0267 }
0268 
0269 #[no_mangle]
0270 pub unsafe extern "C" fn todos_count_get(ptr: *const Todos) -> u64 {
0271     (&*ptr).count()
0272 }
0273 
0274 #[no_mangle]
0275 pub unsafe extern "C" fn todos_add(ptr: *mut Todos, description_str: *const c_ushort, description_len: c_int) {
0276     let mut description = String::new();
0277     set_string_from_utf16(&mut description, description_str, description_len);
0278     let o = &mut *ptr;
0279     o.add(description)
0280 }
0281 
0282 #[no_mangle]
0283 pub unsafe extern "C" fn todos_clear_completed(ptr: *mut Todos) {
0284     let o = &mut *ptr;
0285     o.clear_completed()
0286 }
0287 
0288 #[no_mangle]
0289 pub unsafe extern "C" fn todos_remove(ptr: *mut Todos, index: u64) -> bool {
0290     let o = &mut *ptr;
0291     o.remove(index)
0292 }
0293 
0294 #[no_mangle]
0295 pub unsafe extern "C" fn todos_set_all(ptr: *mut Todos, completed: bool) {
0296     let o = &mut *ptr;
0297     o.set_all(completed)
0298 }
0299 
0300 #[no_mangle]
0301 pub unsafe extern "C" fn todos_row_count(ptr: *const Todos) -> c_int {
0302     to_c_int((&*ptr).row_count())
0303 }
0304 #[no_mangle]
0305 pub unsafe extern "C" fn todos_insert_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool {
0306     (&mut *ptr).insert_rows(to_usize(row), to_usize(count))
0307 }
0308 #[no_mangle]
0309 pub unsafe extern "C" fn todos_remove_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool {
0310     (&mut *ptr).remove_rows(to_usize(row), to_usize(count))
0311 }
0312 #[no_mangle]
0313 pub unsafe extern "C" fn todos_can_fetch_more(ptr: *const Todos) -> bool {
0314     (&*ptr).can_fetch_more()
0315 }
0316 #[no_mangle]
0317 pub unsafe extern "C" fn todos_fetch_more(ptr: *mut Todos) {
0318     (&mut *ptr).fetch_more()
0319 }
0320 #[no_mangle]
0321 pub unsafe extern "C" fn todos_sort(
0322     ptr: *mut Todos,
0323     column: u8,
0324     order: SortOrder,
0325 ) {
0326     (&mut *ptr).sort(column, order)
0327 }
0328 
0329 #[no_mangle]
0330 pub unsafe extern "C" fn todos_data_completed(ptr: *const Todos, row: c_int) -> bool {
0331     let o = &*ptr;
0332     o.completed(to_usize(row))
0333 }
0334 
0335 #[no_mangle]
0336 pub unsafe extern "C" fn todos_set_data_completed(
0337     ptr: *mut Todos, row: c_int,
0338     v: bool,
0339 ) -> bool {
0340     (&mut *ptr).set_completed(to_usize(row), v)
0341 }
0342 
0343 #[no_mangle]
0344 pub unsafe extern "C" fn todos_data_description(
0345     ptr: *const Todos, row: c_int,
0346     d: *mut QString,
0347     set: fn(*mut QString, *const c_char, len: c_int),
0348 ) {
0349     let o = &*ptr;
0350     let data = o.description(to_usize(row));
0351     let s: *const c_char = data.as_ptr() as *const c_char;
0352     set(d, s, to_c_int(data.len()));
0353 }
0354 
0355 #[no_mangle]
0356 pub unsafe extern "C" fn todos_set_data_description(
0357     ptr: *mut Todos, row: c_int,
0358     s: *const c_ushort, len: c_int,
0359 ) -> bool {
0360     let o = &mut *ptr;
0361     let mut v = String::new();
0362     set_string_from_utf16(&mut v, s, len);
0363     o.set_description(to_usize(row), v)
0364 }