rust-qt-binding-generator/examples/todos/rust/src/interface.rs

320 lines
8.5 KiB
Rust

/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
use libc::{c_char, c_ushort, c_int};
use std::slice;
use std::char::decode_utf16;
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
#[repr(C)]
pub struct COption<T> {
data: T,
some: bool,
}
impl<T> From<Option<T>> for COption<T>
where
T: Default,
{
fn from(t: Option<T>) -> COption<T> {
if let Some(v) = t {
COption {
data: v,
some: true,
}
} else {
COption {
data: T::default(),
some: false,
}
}
}
}
pub enum QString {}
fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
let characters = decode_utf16(utf16.iter().cloned())
.into_iter()
.map(|r| r.unwrap());
s.clear();
s.extend(characters);
}
#[repr(C)]
pub enum SortOrder {
Ascending = 0,
Descending = 1,
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
fn to_usize(n: c_int) -> usize {
if n < 0 {
panic!("Cannot cast {} to usize", n);
}
n as usize
}
fn to_c_int(n: usize) -> c_int {
if n > c_int::max_value() as usize {
panic!("Cannot cast {} to c_int", n);
}
n as c_int
}
pub struct TodosQObject {}
#[derive(Clone)]
pub struct TodosEmitter {
qobject: Arc<Mutex<*const TodosQObject>>,
active_count_changed: fn(*const TodosQObject),
count_changed: fn(*const TodosQObject),
new_data_ready: fn(*const TodosQObject),
}
unsafe impl Send for TodosEmitter {}
impl TodosEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn active_count_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.active_count_changed)(ptr);
}
}
pub fn count_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.count_changed)(ptr);
}
}
pub fn new_data_ready(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr);
}
}
}
pub struct TodosList {
qobject: *const TodosQObject,
data_changed: fn(*const TodosQObject, usize, usize),
begin_reset_model: fn(*const TodosQObject),
end_reset_model: fn(*const TodosQObject),
begin_insert_rows: fn(*const TodosQObject, usize, usize),
end_insert_rows: fn(*const TodosQObject),
begin_remove_rows: fn(*const TodosQObject, usize, usize),
end_remove_rows: fn(*const TodosQObject),
}
impl TodosList {
pub fn data_changed(&self, first: usize, last: usize) {
(self.data_changed)(self.qobject, first, last);
}
pub fn begin_reset_model(&self) {
(self.begin_reset_model)(self.qobject);
}
pub fn end_reset_model(&self) {
(self.end_reset_model)(self.qobject);
}
pub fn begin_insert_rows(&self, first: usize, last: usize) {
(self.begin_insert_rows)(self.qobject, first, last);
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_remove_rows(&self, first: usize, last: usize) {
(self.begin_remove_rows)(self.qobject, first, last);
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
pub trait TodosTrait {
fn new(emit: TodosEmitter, model: TodosList) -> Self;
fn emit(&self) -> &TodosEmitter;
fn active_count(&self) -> u64;
fn count(&self) -> u64;
fn add(&mut self, description: String) -> ();
fn clear_completed(&mut self) -> ();
fn remove(&mut self, index: u64) -> bool;
fn set_all(&mut self, completed: bool) -> ();
fn row_count(&self) -> usize;
fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false }
fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false }
fn can_fetch_more(&self) -> bool {
false
}
fn fetch_more(&mut self) {}
fn sort(&mut self, u8, SortOrder) {}
fn completed(&self, item: usize) -> bool;
fn set_completed(&mut self, item: usize, bool) -> bool;
fn description(&self, item: usize) -> &str;
fn set_description(&mut self, item: usize, String) -> bool;
}
#[no_mangle]
pub extern "C" fn todos_new(
todos: *mut TodosQObject,
active_count_changed: fn(*const TodosQObject),
count_changed: fn(*const TodosQObject),
todos_new_data_ready: fn(*const TodosQObject),
todos_data_changed: fn(*const TodosQObject, usize, usize),
todos_begin_reset_model: fn(*const TodosQObject),
todos_end_reset_model: fn(*const TodosQObject),
todos_begin_insert_rows: fn(*const TodosQObject, usize, usize),
todos_end_insert_rows: fn(*const TodosQObject),
todos_begin_remove_rows: fn(*const TodosQObject, usize, usize),
todos_end_remove_rows: fn(*const TodosQObject),
) -> *mut Todos {
let todos_emit = TodosEmitter {
qobject: Arc::new(Mutex::new(todos)),
active_count_changed: active_count_changed,
count_changed: count_changed,
new_data_ready: todos_new_data_ready,
};
let model = TodosList {
qobject: todos,
data_changed: todos_data_changed,
begin_reset_model: todos_begin_reset_model,
end_reset_model: todos_end_reset_model,
begin_insert_rows: todos_begin_insert_rows,
end_insert_rows: todos_end_insert_rows,
begin_remove_rows: todos_begin_remove_rows,
end_remove_rows: todos_end_remove_rows,
};
let d_todos = Todos::new(todos_emit, model);
Box::into_raw(Box::new(d_todos))
}
#[no_mangle]
pub unsafe extern "C" fn todos_free(ptr: *mut Todos) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn todos_active_count_get(ptr: *const Todos) -> u64 {
(&*ptr).active_count()
}
#[no_mangle]
pub unsafe extern "C" fn todos_count_get(ptr: *const Todos) -> u64 {
(&*ptr).count()
}
#[no_mangle]
pub extern "C" fn todos_add(ptr: *mut Todos, description_str: *const c_ushort, description_len: c_int) -> () {
let mut description = String::new();
set_string_from_utf16(&mut description, description_str, description_len);
let o = unsafe { &mut *ptr };
let r = o.add(description);
r
}
#[no_mangle]
pub extern "C" fn todos_clear_completed(ptr: *mut Todos) -> () {
let o = unsafe { &mut *ptr };
let r = o.clear_completed();
r
}
#[no_mangle]
pub extern "C" fn todos_remove(ptr: *mut Todos, index: u64) -> bool {
let o = unsafe { &mut *ptr };
let r = o.remove(index);
r
}
#[no_mangle]
pub extern "C" fn todos_set_all(ptr: *mut Todos, completed: bool) -> () {
let o = unsafe { &mut *ptr };
let r = o.set_all(completed);
r
}
#[no_mangle]
pub unsafe extern "C" fn todos_row_count(ptr: *const Todos) -> c_int {
to_c_int((&*ptr).row_count())
}
#[no_mangle]
pub unsafe extern "C" fn todos_insert_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool {
(&mut *ptr).insert_rows(to_usize(row), to_usize(count))
}
#[no_mangle]
pub unsafe extern "C" fn todos_remove_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool {
(&mut *ptr).remove_rows(to_usize(row), to_usize(count))
}
#[no_mangle]
pub unsafe extern "C" fn todos_can_fetch_more(ptr: *const Todos) -> bool {
(&*ptr).can_fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn todos_fetch_more(ptr: *mut Todos) {
(&mut *ptr).fetch_more()
}
#[no_mangle]
pub unsafe extern "C" fn todos_sort(
ptr: *mut Todos,
column: u8,
order: SortOrder,
) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub extern "C" fn todos_data_completed(ptr: *const Todos, row: c_int) -> bool {
let o = unsafe { &*ptr };
o.completed(to_usize(row)).into()
}
#[no_mangle]
pub unsafe extern "C" fn todos_set_data_completed(
ptr: *mut Todos, row: c_int,
v: bool,
) -> bool {
(&mut *ptr).set_completed(to_usize(row), v)
}
#[no_mangle]
pub extern "C" fn todos_data_description(
ptr: *const Todos, row: c_int,
d: *mut QString,
set: fn(*mut QString, *const c_char, len: c_int),
) {
let o = unsafe { &*ptr };
let data = o.description(to_usize(row));
let s: *const c_char = data.as_ptr() as (*const c_char);
set(d, s, to_c_int(data.len()));
}
#[no_mangle]
pub extern "C" fn todos_set_data_description(
ptr: *mut Todos, row: c_int,
s: *const c_ushort, len: c_int,
) -> bool {
let o = unsafe { &mut *ptr };
let mut v = String::new();
set_string_from_utf16(&mut v, s, len);
o.set_description(to_usize(row), v)
}