blob: 7a8b6590fe67cee305901ed7d6a407da8b2339bb [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// 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
9use autocxx::prelude::*;
10
11include_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
23use ffi::{Point, Rect};
24
25// A simple example dealing with plain-old-data types.
26
27fn 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}