Squashed 'third_party/autocxx/' content from commit 629e8fa53
git-subtree-dir: third_party/autocxx
git-subtree-split: 629e8fa531a633164c0b52e2a3cab536d4cd0849
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
Change-Id: I62a03b0049f49adf029e0204639cdb5468dde1a1
diff --git a/examples/s2/Cargo.toml b/examples/s2/Cargo.toml
new file mode 100644
index 0000000..994a381
--- /dev/null
+++ b/examples/s2/Cargo.toml
@@ -0,0 +1,23 @@
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+# option. This file may not be copied, modified, or distributed
+# except according to those terms.
+
+[package]
+name = "autocxx-s2-example"
+version = "0.1.0"
+authors = ["Adrian Taylor <adetaylor@chromium.org>"]
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+cxx = "1.0.54"
+autocxx = { path = "../..", version="0.22.0" }
+
+[build-dependencies]
+autocxx-build = { path = "../../gen/build", version="0.22.0" }
+miette = { version="4.3", features = [ "fancy" ] }
diff --git a/examples/s2/README.md b/examples/s2/README.md
new file mode 100644
index 0000000..36a56ca
--- /dev/null
+++ b/examples/s2/README.md
@@ -0,0 +1,5 @@
+To build this example:
+* `git submodule update --init --recursive s2geometry`
+* `cargo run`
+
+Thanks to @nside for inspiring this example.
diff --git a/examples/s2/build.rs b/examples/s2/build.rs
new file mode 100644
index 0000000..e61c1af
--- /dev/null
+++ b/examples/s2/build.rs
@@ -0,0 +1,20 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() -> miette::Result<()> {
+ // It's necessary to use an absolute path here because the
+ // C++ codegen and the macro codegen appears to be run from different
+ // working directories.
+ let path = std::path::PathBuf::from("s2geometry/src");
+ let path2 = std::path::PathBuf::from("src");
+ let mut b = autocxx_build::Builder::new("src/main.rs", &[&path, &path2]).build()?;
+ b.flag_if_supported("-std=c++14")
+ .compile("autocxx-s2-example");
+ println!("cargo:rerun-if-changed=src/main.rs");
+ Ok(())
+}
diff --git a/examples/s2/s2geometry b/examples/s2/s2geometry
new file mode 160000
index 0000000..0c4c460
--- /dev/null
+++ b/examples/s2/s2geometry
@@ -0,0 +1 @@
+Subproject commit 0c4c460bdfe696da303641771f9def900b3e440f
diff --git a/examples/s2/src/extras.h b/examples/s2/src/extras.h
new file mode 100644
index 0000000..927a30d
--- /dev/null
+++ b/examples/s2/src/extras.h
@@ -0,0 +1,27 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#include "s2/r2.h"
+#include <sstream>
+
+// autocxx isn't yet smart enough to do anything with the R2Point
+// structure, so here we've manually made a cheeky little API to
+// do something useful with it.
+inline std::string describe_point(R2Point pt) {
+ std::ostringstream oss;
+ oss << pt.x() << ", " << pt.y();
+ return oss.str();
+}
diff --git a/examples/s2/src/main.rs b/examples/s2/src/main.rs
new file mode 100644
index 0000000..141b80e
--- /dev/null
+++ b/examples/s2/src/main.rs
@@ -0,0 +1,53 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use autocxx::prelude::*;
+
+include_cpp! {
+ // C++ headers we want to include.
+ #include "s2/r2rect.h"
+ #include "extras.h"
+ // Safety policy. We are marking that this whole C++ inclusion is unsafe
+ // which means the functions themselves do not need to be marked
+ // as unsafe. Other policies are possible.
+ safety!(unsafe)
+ // What types and functions we want to generate
+ generate!("R1Interval")
+ generate!("R2Rect")
+ generate!("describe_point")
+}
+
+// Everything that we care about is inlined, so we don't have to do
+// anything fancy to build or link any external code.
+fn main() {
+ // Create a couple of R1Intervals using their pre-existing C++
+ // constructors. Actually these will be cxx::UniquePtr<R1Interval>s.
+ let i1 = ffi::R1Interval::new(1.0f64, 2.0f64).within_unique_ptr();
+ let i2 = ffi::R1Interval::new(5.0f64, 6.0f64).within_unique_ptr();
+ // Create a rect, passing references to the intervals.
+ // Note this is 'new1' because R2Rect has multiple
+ // overloaded constructors. 'cargo expand', `cargo doc`
+ // or a rust-analyzer IDE is useful here.
+ let r = ffi::R2Rect::new1(&i1, &i2).within_unique_ptr();
+ // Call a method on one of these objects. As it happens,
+ // this returns a
+ // UniquePtr< ... opaque object representing a point ...>.
+ let center = r.GetCenter();
+ // As the object is too complex for autocxx to understand,
+ // we can't do much with it except to send it into other
+ // C++ APIs. We'll make our own which describes the point.
+ // This will return a std::string, which autocxx will
+ // convert to a UniquePtr<CxxString>. We can convert that
+ // back to a Rust string and print it, so long as we
+ // take care to decide how to deal with non-UTF8
+ // characters (hence the unwrap).
+ println!(
+ "Center of rectangle is {}",
+ ffi::describe_point(center).to_str().unwrap()
+ );
+}