Warning, /sdk/rust-qt-binding-generator/tests/rust_object_types/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 pub enum QByteArray {}
0063 
0064 
0065 fn to_usize(n: c_int) -> usize {
0066     if n < 0 {
0067         panic!("Cannot cast {} to usize", n);
0068     }
0069     n as usize
0070 }
0071 
0072 
0073 fn to_c_int(n: usize) -> c_int {
0074     if n > c_int::max_value() as usize {
0075         panic!("Cannot cast {} to c_int", n);
0076     }
0077     n as c_int
0078 }
0079 
0080 
0081 pub struct ObjectQObject {}
0082 
0083 pub struct ObjectEmitter {
0084     qobject: Arc<AtomicPtr<ObjectQObject>>,
0085     boolean_changed: extern fn(*mut ObjectQObject),
0086     bytearray_changed: extern fn(*mut ObjectQObject),
0087     f32_changed: extern fn(*mut ObjectQObject),
0088     f64_changed: extern fn(*mut ObjectQObject),
0089     i16_changed: extern fn(*mut ObjectQObject),
0090     i32_changed: extern fn(*mut ObjectQObject),
0091     i64_changed: extern fn(*mut ObjectQObject),
0092     i8_changed: extern fn(*mut ObjectQObject),
0093     optional_boolean_changed: extern fn(*mut ObjectQObject),
0094     optional_bytearray_changed: extern fn(*mut ObjectQObject),
0095     optional_string_changed: extern fn(*mut ObjectQObject),
0096     optional_u64_changed: extern fn(*mut ObjectQObject),
0097     string_changed: extern fn(*mut ObjectQObject),
0098     string_by_function_changed: extern fn(*mut ObjectQObject),
0099     u16_changed: extern fn(*mut ObjectQObject),
0100     u32_changed: extern fn(*mut ObjectQObject),
0101     u64_changed: extern fn(*mut ObjectQObject),
0102     u8_changed: extern fn(*mut ObjectQObject),
0103 }
0104 
0105 unsafe impl Send for ObjectEmitter {}
0106 
0107 impl ObjectEmitter {
0108     /// Clone the emitter
0109     ///
0110     /// The emitter can only be cloned when it is mutable. The emitter calls
0111     /// into C++ code which may call into Rust again. If emmitting is possible
0112     /// from immutable structures, that might lead to access to a mutable
0113     /// reference. That is undefined behaviour and forbidden.
0114     pub fn clone(&mut self) -> ObjectEmitter {
0115         ObjectEmitter {
0116             qobject: self.qobject.clone(),
0117             boolean_changed: self.boolean_changed,
0118             bytearray_changed: self.bytearray_changed,
0119             f32_changed: self.f32_changed,
0120             f64_changed: self.f64_changed,
0121             i16_changed: self.i16_changed,
0122             i32_changed: self.i32_changed,
0123             i64_changed: self.i64_changed,
0124             i8_changed: self.i8_changed,
0125             optional_boolean_changed: self.optional_boolean_changed,
0126             optional_bytearray_changed: self.optional_bytearray_changed,
0127             optional_string_changed: self.optional_string_changed,
0128             optional_u64_changed: self.optional_u64_changed,
0129             string_changed: self.string_changed,
0130             string_by_function_changed: self.string_by_function_changed,
0131             u16_changed: self.u16_changed,
0132             u32_changed: self.u32_changed,
0133             u64_changed: self.u64_changed,
0134             u8_changed: self.u8_changed,
0135         }
0136     }
0137     fn clear(&self) {
0138         let n: *const ObjectQObject = null();
0139         self.qobject.store(n as *mut ObjectQObject, Ordering::SeqCst);
0140     }
0141     pub fn boolean_changed(&mut self) {
0142         let ptr = self.qobject.load(Ordering::SeqCst);
0143         if !ptr.is_null() {
0144             (self.boolean_changed)(ptr);
0145         }
0146     }
0147     pub fn bytearray_changed(&mut self) {
0148         let ptr = self.qobject.load(Ordering::SeqCst);
0149         if !ptr.is_null() {
0150             (self.bytearray_changed)(ptr);
0151         }
0152     }
0153     pub fn f32_changed(&mut self) {
0154         let ptr = self.qobject.load(Ordering::SeqCst);
0155         if !ptr.is_null() {
0156             (self.f32_changed)(ptr);
0157         }
0158     }
0159     pub fn f64_changed(&mut self) {
0160         let ptr = self.qobject.load(Ordering::SeqCst);
0161         if !ptr.is_null() {
0162             (self.f64_changed)(ptr);
0163         }
0164     }
0165     pub fn i16_changed(&mut self) {
0166         let ptr = self.qobject.load(Ordering::SeqCst);
0167         if !ptr.is_null() {
0168             (self.i16_changed)(ptr);
0169         }
0170     }
0171     pub fn i32_changed(&mut self) {
0172         let ptr = self.qobject.load(Ordering::SeqCst);
0173         if !ptr.is_null() {
0174             (self.i32_changed)(ptr);
0175         }
0176     }
0177     pub fn i64_changed(&mut self) {
0178         let ptr = self.qobject.load(Ordering::SeqCst);
0179         if !ptr.is_null() {
0180             (self.i64_changed)(ptr);
0181         }
0182     }
0183     pub fn i8_changed(&mut self) {
0184         let ptr = self.qobject.load(Ordering::SeqCst);
0185         if !ptr.is_null() {
0186             (self.i8_changed)(ptr);
0187         }
0188     }
0189     pub fn optional_boolean_changed(&mut self) {
0190         let ptr = self.qobject.load(Ordering::SeqCst);
0191         if !ptr.is_null() {
0192             (self.optional_boolean_changed)(ptr);
0193         }
0194     }
0195     pub fn optional_bytearray_changed(&mut self) {
0196         let ptr = self.qobject.load(Ordering::SeqCst);
0197         if !ptr.is_null() {
0198             (self.optional_bytearray_changed)(ptr);
0199         }
0200     }
0201     pub fn optional_string_changed(&mut self) {
0202         let ptr = self.qobject.load(Ordering::SeqCst);
0203         if !ptr.is_null() {
0204             (self.optional_string_changed)(ptr);
0205         }
0206     }
0207     pub fn optional_u64_changed(&mut self) {
0208         let ptr = self.qobject.load(Ordering::SeqCst);
0209         if !ptr.is_null() {
0210             (self.optional_u64_changed)(ptr);
0211         }
0212     }
0213     pub fn string_changed(&mut self) {
0214         let ptr = self.qobject.load(Ordering::SeqCst);
0215         if !ptr.is_null() {
0216             (self.string_changed)(ptr);
0217         }
0218     }
0219     pub fn string_by_function_changed(&mut self) {
0220         let ptr = self.qobject.load(Ordering::SeqCst);
0221         if !ptr.is_null() {
0222             (self.string_by_function_changed)(ptr);
0223         }
0224     }
0225     pub fn u16_changed(&mut self) {
0226         let ptr = self.qobject.load(Ordering::SeqCst);
0227         if !ptr.is_null() {
0228             (self.u16_changed)(ptr);
0229         }
0230     }
0231     pub fn u32_changed(&mut self) {
0232         let ptr = self.qobject.load(Ordering::SeqCst);
0233         if !ptr.is_null() {
0234             (self.u32_changed)(ptr);
0235         }
0236     }
0237     pub fn u64_changed(&mut self) {
0238         let ptr = self.qobject.load(Ordering::SeqCst);
0239         if !ptr.is_null() {
0240             (self.u64_changed)(ptr);
0241         }
0242     }
0243     pub fn u8_changed(&mut self) {
0244         let ptr = self.qobject.load(Ordering::SeqCst);
0245         if !ptr.is_null() {
0246             (self.u8_changed)(ptr);
0247         }
0248     }
0249 }
0250 
0251 pub trait ObjectTrait {
0252     fn new(emit: ObjectEmitter) -> Self;
0253     fn emit(&mut self) -> &mut ObjectEmitter;
0254     fn boolean(&self) -> bool;
0255     fn set_boolean(&mut self, value: bool);
0256     fn bytearray(&self) -> &[u8];
0257     fn set_bytearray(&mut self, value: &[u8]);
0258     fn f32(&self) -> f32;
0259     fn set_f32(&mut self, value: f32);
0260     fn f64(&self) -> f64;
0261     fn set_f64(&mut self, value: f64);
0262     fn i16(&self) -> i16;
0263     fn set_i16(&mut self, value: i16);
0264     fn i32(&self) -> i32;
0265     fn set_i32(&mut self, value: i32);
0266     fn i64(&self) -> i64;
0267     fn set_i64(&mut self, value: i64);
0268     fn i8(&self) -> i8;
0269     fn set_i8(&mut self, value: i8);
0270     fn optional_boolean(&self) -> Option<bool>;
0271     fn set_optional_boolean(&mut self, value: Option<bool>);
0272     fn optional_bytearray(&self) -> Option<&[u8]>;
0273     fn set_optional_bytearray(&mut self, value: Option<&[u8]>);
0274     fn optional_string(&self) -> Option<&str>;
0275     fn set_optional_string(&mut self, value: Option<String>);
0276     fn optional_u64(&self) -> Option<u64>;
0277     fn set_optional_u64(&mut self, value: Option<u64>);
0278     fn string(&self) -> &str;
0279     fn set_string(&mut self, value: String);
0280     fn string_by_function<F>(&self, getter: F) where F: FnOnce(&str);    fn set_string_by_function(&mut self, value: String);
0281     fn u16(&self) -> u16;
0282     fn set_u16(&mut self, value: u16);
0283     fn u32(&self) -> u32;
0284     fn set_u32(&mut self, value: u32);
0285     fn u64(&self) -> u64;
0286     fn set_u64(&mut self, value: u64);
0287     fn u8(&self) -> u8;
0288     fn set_u8(&mut self, value: u8);
0289 }
0290 
0291 #[no_mangle]
0292 pub extern "C" fn object_new(
0293     object: *mut ObjectQObject,
0294     object_boolean_changed: extern fn(*mut ObjectQObject),
0295     object_bytearray_changed: extern fn(*mut ObjectQObject),
0296     object_f32_changed: extern fn(*mut ObjectQObject),
0297     object_f64_changed: extern fn(*mut ObjectQObject),
0298     object_i16_changed: extern fn(*mut ObjectQObject),
0299     object_i32_changed: extern fn(*mut ObjectQObject),
0300     object_i64_changed: extern fn(*mut ObjectQObject),
0301     object_i8_changed: extern fn(*mut ObjectQObject),
0302     object_optional_boolean_changed: extern fn(*mut ObjectQObject),
0303     object_optional_bytearray_changed: extern fn(*mut ObjectQObject),
0304     object_optional_string_changed: extern fn(*mut ObjectQObject),
0305     object_optional_u64_changed: extern fn(*mut ObjectQObject),
0306     object_string_changed: extern fn(*mut ObjectQObject),
0307     object_string_by_function_changed: extern fn(*mut ObjectQObject),
0308     object_u16_changed: extern fn(*mut ObjectQObject),
0309     object_u32_changed: extern fn(*mut ObjectQObject),
0310     object_u64_changed: extern fn(*mut ObjectQObject),
0311     object_u8_changed: extern fn(*mut ObjectQObject),
0312 ) -> *mut Object {
0313     let object_emit = ObjectEmitter {
0314         qobject: Arc::new(AtomicPtr::new(object)),
0315         boolean_changed: object_boolean_changed,
0316         bytearray_changed: object_bytearray_changed,
0317         f32_changed: object_f32_changed,
0318         f64_changed: object_f64_changed,
0319         i16_changed: object_i16_changed,
0320         i32_changed: object_i32_changed,
0321         i64_changed: object_i64_changed,
0322         i8_changed: object_i8_changed,
0323         optional_boolean_changed: object_optional_boolean_changed,
0324         optional_bytearray_changed: object_optional_bytearray_changed,
0325         optional_string_changed: object_optional_string_changed,
0326         optional_u64_changed: object_optional_u64_changed,
0327         string_changed: object_string_changed,
0328         string_by_function_changed: object_string_by_function_changed,
0329         u16_changed: object_u16_changed,
0330         u32_changed: object_u32_changed,
0331         u64_changed: object_u64_changed,
0332         u8_changed: object_u8_changed,
0333     };
0334     let d_object = Object::new(object_emit);
0335     Box::into_raw(Box::new(d_object))
0336 }
0337 
0338 #[no_mangle]
0339 pub unsafe extern "C" fn object_free(ptr: *mut Object) {
0340     Box::from_raw(ptr).emit().clear();
0341 }
0342 
0343 #[no_mangle]
0344 pub unsafe extern "C" fn object_boolean_get(ptr: *const Object) -> bool {
0345     (&*ptr).boolean()
0346 }
0347 
0348 #[no_mangle]
0349 pub unsafe extern "C" fn object_boolean_set(ptr: *mut Object, v: bool) {
0350     (&mut *ptr).set_boolean(v);
0351 }
0352 
0353 #[no_mangle]
0354 pub unsafe extern "C" fn object_bytearray_get(
0355     ptr: *const Object,
0356     p: *mut QByteArray,
0357     set: extern fn(*mut QByteArray, *const c_char, c_int),
0358 ) {
0359     let o = &*ptr;
0360     let v = o.bytearray();
0361     let s: *const c_char = v.as_ptr() as *const c_char;
0362     set(p, s, to_c_int(v.len()));
0363 }
0364 
0365 #[no_mangle]
0366 pub unsafe extern "C" fn object_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) {
0367     let o = &mut *ptr;
0368     let v = slice::from_raw_parts(v as *const u8, to_usize(len));
0369     o.set_bytearray(v);
0370 }
0371 
0372 #[no_mangle]
0373 pub unsafe extern "C" fn object_f32_get(ptr: *const Object) -> f32 {
0374     (&*ptr).f32()
0375 }
0376 
0377 #[no_mangle]
0378 pub unsafe extern "C" fn object_f32_set(ptr: *mut Object, v: f32) {
0379     (&mut *ptr).set_f32(v);
0380 }
0381 
0382 #[no_mangle]
0383 pub unsafe extern "C" fn object_f64_get(ptr: *const Object) -> f64 {
0384     (&*ptr).f64()
0385 }
0386 
0387 #[no_mangle]
0388 pub unsafe extern "C" fn object_f64_set(ptr: *mut Object, v: f64) {
0389     (&mut *ptr).set_f64(v);
0390 }
0391 
0392 #[no_mangle]
0393 pub unsafe extern "C" fn object_i16_get(ptr: *const Object) -> i16 {
0394     (&*ptr).i16()
0395 }
0396 
0397 #[no_mangle]
0398 pub unsafe extern "C" fn object_i16_set(ptr: *mut Object, v: i16) {
0399     (&mut *ptr).set_i16(v);
0400 }
0401 
0402 #[no_mangle]
0403 pub unsafe extern "C" fn object_i32_get(ptr: *const Object) -> i32 {
0404     (&*ptr).i32()
0405 }
0406 
0407 #[no_mangle]
0408 pub unsafe extern "C" fn object_i32_set(ptr: *mut Object, v: i32) {
0409     (&mut *ptr).set_i32(v);
0410 }
0411 
0412 #[no_mangle]
0413 pub unsafe extern "C" fn object_i64_get(ptr: *const Object) -> i64 {
0414     (&*ptr).i64()
0415 }
0416 
0417 #[no_mangle]
0418 pub unsafe extern "C" fn object_i64_set(ptr: *mut Object, v: i64) {
0419     (&mut *ptr).set_i64(v);
0420 }
0421 
0422 #[no_mangle]
0423 pub unsafe extern "C" fn object_i8_get(ptr: *const Object) -> i8 {
0424     (&*ptr).i8()
0425 }
0426 
0427 #[no_mangle]
0428 pub unsafe extern "C" fn object_i8_set(ptr: *mut Object, v: i8) {
0429     (&mut *ptr).set_i8(v);
0430 }
0431 
0432 #[no_mangle]
0433 pub unsafe extern "C" fn object_optional_boolean_get(ptr: *const Object) -> COption<bool> {
0434     match (&*ptr).optional_boolean() {
0435         Some(value) => COption { data: value, some: true },
0436         None => COption { data: bool::default(), some: false}
0437     }
0438 }
0439 
0440 #[no_mangle]
0441 pub unsafe extern "C" fn object_optional_boolean_set(ptr: *mut Object, v: bool) {
0442     (&mut *ptr).set_optional_boolean(Some(v));
0443 }
0444 
0445 #[no_mangle]
0446 pub unsafe extern "C" fn object_optional_boolean_set_none(ptr: *mut Object) {
0447     let o = &mut *ptr;
0448     o.set_optional_boolean(None);
0449 }
0450 
0451 #[no_mangle]
0452 pub unsafe extern "C" fn object_optional_bytearray_get(
0453     ptr: *const Object,
0454     p: *mut QByteArray,
0455     set: extern fn(*mut QByteArray, *const c_char, c_int),
0456 ) {
0457     let o = &*ptr;
0458     let v = o.optional_bytearray();
0459     if let Some(v) = v {
0460         let s: *const c_char = v.as_ptr() as *const c_char;
0461         set(p, s, to_c_int(v.len()));
0462     }
0463 }
0464 
0465 #[no_mangle]
0466 pub unsafe extern "C" fn object_optional_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) {
0467     let o = &mut *ptr;
0468     let v = slice::from_raw_parts(v as *const u8, to_usize(len));
0469     o.set_optional_bytearray(Some(v.into()));
0470 }
0471 
0472 #[no_mangle]
0473 pub unsafe extern "C" fn object_optional_bytearray_set_none(ptr: *mut Object) {
0474     let o = &mut *ptr;
0475     o.set_optional_bytearray(None);
0476 }
0477 
0478 #[no_mangle]
0479 pub unsafe extern "C" fn object_optional_string_get(
0480     ptr: *const Object,
0481     p: *mut QString,
0482     set: extern fn(*mut QString, *const c_char, c_int),
0483 ) {
0484     let o = &*ptr;
0485     let v = o.optional_string();
0486     if let Some(v) = v {
0487         let s: *const c_char = v.as_ptr() as *const c_char;
0488         set(p, s, to_c_int(v.len()));
0489     }
0490 }
0491 
0492 #[no_mangle]
0493 pub unsafe extern "C" fn object_optional_string_set(ptr: *mut Object, v: *const c_ushort, len: c_int) {
0494     let o = &mut *ptr;
0495     let mut s = String::new();
0496     set_string_from_utf16(&mut s, v, len);
0497     o.set_optional_string(Some(s));
0498 }
0499 
0500 #[no_mangle]
0501 pub unsafe extern "C" fn object_optional_string_set_none(ptr: *mut Object) {
0502     let o = &mut *ptr;
0503     o.set_optional_string(None);
0504 }
0505 
0506 #[no_mangle]
0507 pub unsafe extern "C" fn object_optional_u64_get(ptr: *const Object) -> COption<u64> {
0508     match (&*ptr).optional_u64() {
0509         Some(value) => COption { data: value, some: true },
0510         None => COption { data: u64::default(), some: false}
0511     }
0512 }
0513 
0514 #[no_mangle]
0515 pub unsafe extern "C" fn object_optional_u64_set(ptr: *mut Object, v: u64) {
0516     (&mut *ptr).set_optional_u64(Some(v));
0517 }
0518 
0519 #[no_mangle]
0520 pub unsafe extern "C" fn object_optional_u64_set_none(ptr: *mut Object) {
0521     let o = &mut *ptr;
0522     o.set_optional_u64(None);
0523 }
0524 
0525 #[no_mangle]
0526 pub unsafe extern "C" fn object_string_get(
0527     ptr: *const Object,
0528     p: *mut QString,
0529     set: extern fn(*mut QString, *const c_char, c_int),
0530 ) {
0531     let o = &*ptr;
0532     let v = o.string();
0533     let s: *const c_char = v.as_ptr() as *const c_char;
0534     set(p, s, to_c_int(v.len()));
0535 }
0536 
0537 #[no_mangle]
0538 pub unsafe extern "C" fn object_string_set(ptr: *mut Object, v: *const c_ushort, len: c_int) {
0539     let o = &mut *ptr;
0540     let mut s = String::new();
0541     set_string_from_utf16(&mut s, v, len);
0542     o.set_string(s);
0543 }
0544 
0545 #[no_mangle]
0546 pub unsafe extern "C" fn object_string_by_function_get(
0547     ptr: *const Object,
0548     p: *mut QString,
0549     set: extern fn(*mut QString, *const c_char, c_int),
0550 ) {
0551     let o = &*ptr;
0552     o.string_by_function(|v| {
0553         let s: *const c_char = v.as_ptr() as *const c_char;
0554         set(p, s, to_c_int(v.len()));
0555     });
0556 }
0557 
0558 #[no_mangle]
0559 pub unsafe extern "C" fn object_string_by_function_set(ptr: *mut Object, v: *const c_ushort, len: c_int) {
0560     let o = &mut *ptr;
0561     let mut s = String::new();
0562     set_string_from_utf16(&mut s, v, len);
0563     o.set_string_by_function(s);
0564 }
0565 
0566 #[no_mangle]
0567 pub unsafe extern "C" fn object_u16_get(ptr: *const Object) -> u16 {
0568     (&*ptr).u16()
0569 }
0570 
0571 #[no_mangle]
0572 pub unsafe extern "C" fn object_u16_set(ptr: *mut Object, v: u16) {
0573     (&mut *ptr).set_u16(v);
0574 }
0575 
0576 #[no_mangle]
0577 pub unsafe extern "C" fn object_u32_get(ptr: *const Object) -> u32 {
0578     (&*ptr).u32()
0579 }
0580 
0581 #[no_mangle]
0582 pub unsafe extern "C" fn object_u32_set(ptr: *mut Object, v: u32) {
0583     (&mut *ptr).set_u32(v);
0584 }
0585 
0586 #[no_mangle]
0587 pub unsafe extern "C" fn object_u64_get(ptr: *const Object) -> u64 {
0588     (&*ptr).u64()
0589 }
0590 
0591 #[no_mangle]
0592 pub unsafe extern "C" fn object_u64_set(ptr: *mut Object, v: u64) {
0593     (&mut *ptr).set_u64(v);
0594 }
0595 
0596 #[no_mangle]
0597 pub unsafe extern "C" fn object_u8_get(ptr: *const Object) -> u8 {
0598     (&*ptr).u8()
0599 }
0600 
0601 #[no_mangle]
0602 pub unsafe extern "C" fn object_u8_set(ptr: *mut Object, v: u8) {
0603     (&mut *ptr).set_u8(v);
0604 }