Attribute Macro bridge

#[bridge]
Expand description

Generates a C++ bridge for a given struct with functions defined in the enclosed implementation block.

C++ header will only be generated when kanzi crate is built with generate_bridge feature. For Kanzi templates generate_bridge feature is available on the template itself which allows executing cargo build --features generate_bridge for rebuilding C++ headers.

§Attributes

  • header_path - Specifies a path at which the C++ header for the given Rust struct will be written. Note that relative paths use the current workspace folder as base.
#[kanzi::bridge(header_path = "./include/application.hpp")]
impl Application { .. }

§Requirements

  • new function is required and needs to be provided. Its first argument should be domain: &kanzi::Domain and it should return either Self or kanzi::Result<Self>. Any other arguments can be provided as well.
#[kanzi::bridge(header_path = "...")]
impl #TYPE {
    fn new(domain: &kanzi::Domain) -> Self { ... }
    // or
    fn new(domain: &kanzi::Domain) -> kanzi::Result<Self> { ... }
    // or
    fn new(domain: &kanzi::Domain, screen: kanzi::Screen, width: f32) -> Self { ... }
}

§Arguments

All bridge functions should accept:

  • Any Kanzi class derived from object. It can be optional (wrapped in Option) or weak (wrapped in Weak) or both.
  • Any default Rust types and FFI types, such as bool, i32, float, std::ffi::c_int, etc.
  • *mut std::ffi::c_void pointers.
  • Owned types including PropertyType and MessageArguments.
  • Reference types including &Domain, &KanziStr and &Variant.
#[kanzi::bridge(header_path = "...")]
impl #TYPE {
   pub fn allowed_argument_types(
       &self,
       arg_1: kanzi::Object,
       arg_2: kanzi::Weak<kanzi::AbstractDataObject>,
       arg_3: Option<kanzi::Node>,
       arg_4: Option<kanzi::Weak<kanzi::ColorBrush>>,
       arg_5: kanzi::AbstractPropertyType,
       arg_6: kanzi::MessageArguments,
       arg_7: &kanzi::Domain,
       arg_8: &kanzi::KanziStr,
       arg_9: &kanzi::AbstractVariant,
       arg_a: usize,
       arg_b: u8,
       arg_c: c_int,
       arg_d: *mut std::ffi::c_void,
   ) { .. }
}

§Return value

All (except for new) bridge functions should return a value. List of allowed values includes:

  • Unit value.
  • Any default Rust type or FFI type, such as bool, i32, float, std::ffi::c_int, etc.
  • Pointers, such as *mut std::ffi::c_void
  • Any Kanzi class derived from Object. It can be optional (wrapped in Option) or weak (wrapped in Weak) or both.

The values can be either wrapped in kanzi::Result or not. If the function returns an Error or panics, the exception will be thrown in Kanzi. Note that Kanzi is not exception safe and calling into it after the exception was thrown is undefined.

#[kanzi::bridge(header_path = "...")]
impl #TYPE {
    pub fn ret_1(&self) { .. }
    pub fn ret_2(&self) -> usize { .. }
    pub fn ret_3(&self) -> Option<usize> { .. }
    pub fn ret_4(&self) -> *mut std::ffi::c_void { .. }
    pub fn ret_5(&self) -> kanzi::Node { .. }
    pub fn ret_6(&self) -> kanzi::Weak<kanzi::Screen> { .. }
    pub fn ret_7(&self) -> Option<kanzi::AbstractDataObject> { .. }
    pub fn ret_8(&self) -> Option<kanzi::Weak<kanzi::Screen>> { .. }

    // Using `Result` also works
    pub fn rrt_1(&self) -> kanzi::Result<()> { .. }
    pub fn rrt_2(&self) -> kanzi::Result<usize> { .. }
    pub fn rrt_3(&self) -> kanzi::Result<Option<usize>> { .. }
    pub fn rrt_4(&self) -> kanzi::Result<*mut std::ffi::c_void> { .. }
    pub fn rrt_5(&self) -> kanzi::Result<kanzi::Node> { .. }
    pub fn rrt_6(&self) -> kanzi::Result<kanzi::Weak<kanzi::Screen>> { .. }
    pub fn rrt_7(&self) -> kanzi::Result<Option<kanzi::AbstractDataObject>> { .. }
    pub fn rrt_8(&self) -> kanzi::Result<Option<kanzi::Weak<kanzi::Screen>>> { .. }
}