Warning, /sdk/rust-qt-binding-generator/tests/rust_objects/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 pub enum QString {}
0014 
0015 fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
0016     let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
0017     let characters = decode_utf16(utf16.iter().cloned())
0018         .map(|r| r.unwrap());
0019     s.clear();
0020     s.extend(characters);
0021 }
0022 
0023 
0024 
0025 fn to_usize(n: c_int) -> usize {
0026     if n < 0 {
0027         panic!("Cannot cast {} to usize", n);
0028     }
0029     n as usize
0030 }
0031 
0032 
0033 fn to_c_int(n: usize) -> c_int {
0034     if n > c_int::max_value() as usize {
0035         panic!("Cannot cast {} to c_int", n);
0036     }
0037     n as c_int
0038 }
0039 
0040 
0041 pub struct GroupQObject {}
0042 
0043 pub struct GroupEmitter {
0044     qobject: Arc<AtomicPtr<GroupQObject>>,
0045 }
0046 
0047 unsafe impl Send for GroupEmitter {}
0048 
0049 impl GroupEmitter {
0050     /// Clone the emitter
0051     ///
0052     /// The emitter can only be cloned when it is mutable. The emitter calls
0053     /// into C++ code which may call into Rust again. If emmitting is possible
0054     /// from immutable structures, that might lead to access to a mutable
0055     /// reference. That is undefined behaviour and forbidden.
0056     pub fn clone(&mut self) -> GroupEmitter {
0057         GroupEmitter {
0058             qobject: self.qobject.clone(),
0059         }
0060     }
0061     fn clear(&self) {
0062         let n: *const GroupQObject = null();
0063         self.qobject.store(n as *mut GroupQObject, Ordering::SeqCst);
0064     }
0065 }
0066 
0067 pub trait GroupTrait {
0068     fn new(emit: GroupEmitter,
0069         person: Person) -> Self;
0070     fn emit(&mut self) -> &mut GroupEmitter;
0071     fn person(&self) -> &Person;
0072     fn person_mut(&mut self) -> &mut Person;
0073 }
0074 
0075 #[no_mangle]
0076 pub extern "C" fn group_new(
0077     group: *mut GroupQObject,
0078     person: *mut PersonQObject,
0079     object: *mut InnerObjectQObject,
0080     object_description_changed: extern fn(*mut InnerObjectQObject),
0081 ) -> *mut Group {
0082     let object_emit = InnerObjectEmitter {
0083         qobject: Arc::new(AtomicPtr::new(object)),
0084         description_changed: object_description_changed,
0085     };
0086     let d_object = InnerObject::new(object_emit);
0087     let person_emit = PersonEmitter {
0088         qobject: Arc::new(AtomicPtr::new(person)),
0089     };
0090     let d_person = Person::new(person_emit,
0091         d_object);
0092     let group_emit = GroupEmitter {
0093         qobject: Arc::new(AtomicPtr::new(group)),
0094     };
0095     let d_group = Group::new(group_emit,
0096         d_person);
0097     Box::into_raw(Box::new(d_group))
0098 }
0099 
0100 #[no_mangle]
0101 pub unsafe extern "C" fn group_free(ptr: *mut Group) {
0102     Box::from_raw(ptr).emit().clear();
0103 }
0104 
0105 #[no_mangle]
0106 pub unsafe extern "C" fn group_person_get(ptr: *mut Group) -> *mut Person {
0107     (&mut *ptr).person_mut()
0108 }
0109 
0110 pub struct InnerObjectQObject {}
0111 
0112 pub struct InnerObjectEmitter {
0113     qobject: Arc<AtomicPtr<InnerObjectQObject>>,
0114     description_changed: extern fn(*mut InnerObjectQObject),
0115 }
0116 
0117 unsafe impl Send for InnerObjectEmitter {}
0118 
0119 impl InnerObjectEmitter {
0120     /// Clone the emitter
0121     ///
0122     /// The emitter can only be cloned when it is mutable. The emitter calls
0123     /// into C++ code which may call into Rust again. If emmitting is possible
0124     /// from immutable structures, that might lead to access to a mutable
0125     /// reference. That is undefined behaviour and forbidden.
0126     pub fn clone(&mut self) -> InnerObjectEmitter {
0127         InnerObjectEmitter {
0128             qobject: self.qobject.clone(),
0129             description_changed: self.description_changed,
0130         }
0131     }
0132     fn clear(&self) {
0133         let n: *const InnerObjectQObject = null();
0134         self.qobject.store(n as *mut InnerObjectQObject, Ordering::SeqCst);
0135     }
0136     pub fn description_changed(&mut self) {
0137         let ptr = self.qobject.load(Ordering::SeqCst);
0138         if !ptr.is_null() {
0139             (self.description_changed)(ptr);
0140         }
0141     }
0142 }
0143 
0144 pub trait InnerObjectTrait {
0145     fn new(emit: InnerObjectEmitter) -> Self;
0146     fn emit(&mut self) -> &mut InnerObjectEmitter;
0147     fn description(&self) -> &str;
0148     fn set_description(&mut self, value: String);
0149 }
0150 
0151 #[no_mangle]
0152 pub extern "C" fn inner_object_new(
0153     inner_object: *mut InnerObjectQObject,
0154     inner_object_description_changed: extern fn(*mut InnerObjectQObject),
0155 ) -> *mut InnerObject {
0156     let inner_object_emit = InnerObjectEmitter {
0157         qobject: Arc::new(AtomicPtr::new(inner_object)),
0158         description_changed: inner_object_description_changed,
0159     };
0160     let d_inner_object = InnerObject::new(inner_object_emit);
0161     Box::into_raw(Box::new(d_inner_object))
0162 }
0163 
0164 #[no_mangle]
0165 pub unsafe extern "C" fn inner_object_free(ptr: *mut InnerObject) {
0166     Box::from_raw(ptr).emit().clear();
0167 }
0168 
0169 #[no_mangle]
0170 pub unsafe extern "C" fn inner_object_description_get(
0171     ptr: *const InnerObject,
0172     p: *mut QString,
0173     set: extern fn(*mut QString, *const c_char, c_int),
0174 ) {
0175     let o = &*ptr;
0176     let v = o.description();
0177     let s: *const c_char = v.as_ptr() as *const c_char;
0178     set(p, s, to_c_int(v.len()));
0179 }
0180 
0181 #[no_mangle]
0182 pub unsafe extern "C" fn inner_object_description_set(ptr: *mut InnerObject, v: *const c_ushort, len: c_int) {
0183     let o = &mut *ptr;
0184     let mut s = String::new();
0185     set_string_from_utf16(&mut s, v, len);
0186     o.set_description(s);
0187 }
0188 
0189 pub struct PersonQObject {}
0190 
0191 pub struct PersonEmitter {
0192     qobject: Arc<AtomicPtr<PersonQObject>>,
0193 }
0194 
0195 unsafe impl Send for PersonEmitter {}
0196 
0197 impl PersonEmitter {
0198     /// Clone the emitter
0199     ///
0200     /// The emitter can only be cloned when it is mutable. The emitter calls
0201     /// into C++ code which may call into Rust again. If emmitting is possible
0202     /// from immutable structures, that might lead to access to a mutable
0203     /// reference. That is undefined behaviour and forbidden.
0204     pub fn clone(&mut self) -> PersonEmitter {
0205         PersonEmitter {
0206             qobject: self.qobject.clone(),
0207         }
0208     }
0209     fn clear(&self) {
0210         let n: *const PersonQObject = null();
0211         self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst);
0212     }
0213 }
0214 
0215 pub trait PersonTrait {
0216     fn new(emit: PersonEmitter,
0217         object: InnerObject) -> Self;
0218     fn emit(&mut self) -> &mut PersonEmitter;
0219     fn object(&self) -> &InnerObject;
0220     fn object_mut(&mut self) -> &mut InnerObject;
0221 }
0222 
0223 #[no_mangle]
0224 pub extern "C" fn person_new(
0225     person: *mut PersonQObject,
0226     object: *mut InnerObjectQObject,
0227     object_description_changed: extern fn(*mut InnerObjectQObject),
0228 ) -> *mut Person {
0229     let object_emit = InnerObjectEmitter {
0230         qobject: Arc::new(AtomicPtr::new(object)),
0231         description_changed: object_description_changed,
0232     };
0233     let d_object = InnerObject::new(object_emit);
0234     let person_emit = PersonEmitter {
0235         qobject: Arc::new(AtomicPtr::new(person)),
0236     };
0237     let d_person = Person::new(person_emit,
0238         d_object);
0239     Box::into_raw(Box::new(d_person))
0240 }
0241 
0242 #[no_mangle]
0243 pub unsafe extern "C" fn person_free(ptr: *mut Person) {
0244     Box::from_raw(ptr).emit().clear();
0245 }
0246 
0247 #[no_mangle]
0248 pub unsafe extern "C" fn person_object_get(ptr: *mut Person) -> *mut InnerObject {
0249     (&mut *ptr).object_mut()
0250 }