rust-qt-binding-generator/demo/rust/src/interface.rs

144 lines
4.0 KiB
Rust
Raw Normal View History

2017-08-08 11:07:24 -07:00
#![allow(unknown_lints)]
#![allow(mutex_atomic)]
2017-08-03 02:18:52 -07:00
use std::slice;
2017-08-04 14:15:58 -07:00
use libc::{c_int, uint16_t, size_t, c_void};
2017-08-04 04:21:11 -07:00
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
2017-08-03 02:18:52 -07:00
2017-08-04 13:26:53 -07:00
use implementation::*;
2017-08-03 02:18:52 -07:00
pub struct HelloQObject {}
2017-08-04 04:21:11 -07:00
pub struct HelloEmitter {
2017-08-03 02:18:52 -07:00
qobject: *const HelloQObject,
2017-08-08 11:33:46 -07:00
hello_changed: fn(*const HelloQObject),
2017-08-03 02:18:52 -07:00
}
2017-08-04 04:21:11 -07:00
impl HelloEmitter {
2017-08-03 02:18:52 -07:00
pub fn hello_changed(&self) {
(self.hello_changed)(self.qobject);
}
}
pub trait HelloTrait {
2017-08-04 04:21:11 -07:00
fn create(emit: HelloEmitter) -> Self;
2017-08-03 02:18:52 -07:00
fn get_hello(&self) -> &String;
fn set_hello(&mut self, value: String);
}
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub extern "C" fn hello_new(qobject: *const HelloQObject,
changed: fn(*const HelloQObject))
-> *mut Hello {
2017-08-04 04:21:11 -07:00
let emit = HelloEmitter {
2017-08-03 02:18:52 -07:00
qobject: qobject,
2017-08-08 11:33:46 -07:00
hello_changed: changed,
2017-08-03 02:18:52 -07:00
};
2017-08-04 04:21:11 -07:00
let hello = Hello::create(emit);
2017-08-03 02:18:52 -07:00
Box::into_raw(Box::new(hello))
}
2017-08-03 12:27:32 -07:00
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn hello_free(ptr: *mut Hello) {
if ptr.is_null() {
return;
}
2017-08-08 11:07:24 -07:00
Box::from_raw(ptr);
2017-08-03 12:27:32 -07:00
}
2017-08-03 02:18:52 -07:00
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn hello_set(ptr: *mut Hello, s: *const uint16_t, len: size_t) {
2017-08-08 11:07:24 -07:00
let data = slice::from_raw_parts(s, len as usize);
(&mut *ptr).set_hello(String::from_utf16_lossy(data));
2017-08-03 02:18:52 -07:00
}
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn hello_get(ptr: *const Hello) -> QString {
2017-08-08 11:07:24 -07:00
QString::from((&*ptr).get_hello())
2017-08-03 02:18:52 -07:00
}
2017-08-03 12:27:32 -07:00
pub struct RItemModelQObject {}
#[derive (Clone)]
2017-08-04 04:21:11 -07:00
pub struct RItemModelEmitter {
qobject: Arc<Mutex<*const RItemModelQObject>>,
2017-08-08 11:33:46 -07:00
new_data_ready: fn(*const RItemModelQObject),
2017-08-03 12:27:32 -07:00
}
unsafe impl Send for RItemModelEmitter {}
2017-08-04 04:21:11 -07:00
impl RItemModelEmitter {
pub fn new_data_ready(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr);
}
}
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
2017-08-03 12:27:32 -07:00
}
2017-08-04 13:26:53 -07:00
pub trait RItemModelTrait<T> {
fn create(emit: RItemModelEmitter, root: T) -> Self;
fn emit(&self) -> &RItemModelEmitter;
fn row_count(&mut self, row: c_int, parent: usize) -> c_int;
fn index(&mut self, row: c_int, parent: usize) -> usize;
fn parent(&self, row: c_int, parent: usize) -> QModelIndex;
fn file_name(&mut self, row: c_int, parent: usize) -> String;
fn file_permissions(&mut self, row: c_int, parent: usize) -> c_int;
2017-08-03 12:27:32 -07:00
}
2017-08-03 02:18:52 -07:00
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub extern "C" fn ritemmodel_new(qobject: *const RItemModelQObject,
new_data_ready: fn(*const RItemModelQObject))
-> *mut RItemModel {
2017-08-04 04:21:11 -07:00
let emit = RItemModelEmitter {
qobject: Arc::new(Mutex::new(qobject)),
2017-08-08 11:33:46 -07:00
new_data_ready: new_data_ready,
2017-08-03 12:27:32 -07:00
};
2017-08-04 13:26:53 -07:00
let ritemmodel = RItemModel::create(emit, DirEntry::create("/"));
2017-08-03 12:27:32 -07:00
Box::into_raw(Box::new(ritemmodel))
}
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn ritemmodel_free(ptr: *mut RItemModel) {
2017-08-08 11:07:24 -07:00
Box::from_raw(ptr).emit().clear();
2017-08-03 02:18:52 -07:00
}
2017-08-04 04:21:11 -07:00
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, row: c_int, parent: usize) -> i32 {
(&mut *ptr).row_count(row, parent)
2017-08-04 04:21:11 -07:00
}
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn ritemmodel_index(ptr: *mut RItemModel,
row: c_int, parent: usize)
-> usize {
(&mut *ptr).index(row, parent)
2017-08-04 04:21:11 -07:00
}
#[no_mangle]
2017-08-08 11:33:46 -07:00
pub unsafe extern "C" fn ritemmodel_parent(ptr: *const RItemModel,
row: c_int, parent: usize)
2017-08-08 11:33:46 -07:00
-> QModelIndex {
(&*ptr).parent(row, parent)
2017-08-04 04:21:11 -07:00
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_data_file_name(ptr: *mut RItemModel,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&mut *ptr).file_name(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn ritemmodel_data_file_permissions(ptr: *mut RItemModel,
row: c_int, parent: usize) -> c_int {
(&mut *ptr).file_permissions(row, parent)
2017-08-04 04:21:11 -07:00
}