Brian Silverman | 4e662aa | 2022-05-11 23:10:19 -0700 | [diff] [blame^] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | use autocxx::prelude::*; |
| 10 | |
| 11 | include_cpp! { |
| 12 | // C++ headers we want to include. |
| 13 | #include "cpp.h" |
| 14 | // Safety policy. We are marking that this whole C++ inclusion is unsafe |
| 15 | // which means the functions themselves do not need to be marked |
| 16 | // as unsafe. Other policies are possible. |
| 17 | safety!(unsafe) |
| 18 | // What types and functions we want to generate |
| 19 | generate_pod!("Rect") |
| 20 | generate!("print_point") |
| 21 | } |
| 22 | |
| 23 | use ffi::{Point, Rect}; |
| 24 | |
| 25 | // A simple example dealing with plain-old-data types. |
| 26 | |
| 27 | fn main() { |
| 28 | let r = Rect { |
| 29 | top_left: Point { x: 3, y: 3 }, |
| 30 | bottom_right: Point { x: 12, y: 15 }, |
| 31 | }; |
| 32 | // r.width() and r.height() return an autocxx::c_int |
| 33 | // which we need to unpackage. It is hoped that one day cxx will |
| 34 | // natively support 'int' and friends, and that won't be necessary. |
| 35 | let center = Point { |
| 36 | x: r.top_left.x + r.width().0 / 2, |
| 37 | y: r.top_left.y + r.height().0 / 2, |
| 38 | }; |
| 39 | ffi::print_point(center); |
| 40 | } |