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/demo/Cargo.toml b/demo/Cargo.toml
new file mode 100644
index 0000000..c6a7227
--- /dev/null
+++ b/demo/Cargo.toml
@@ -0,0 +1,21 @@
+# 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-demo"
+version = "0.22.0"
+authors = ["Adrian Taylor <adetaylor@chromium.org>"]
+edition = "2021"
+
+[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/demo/build.rs b/demo/build.rs
new file mode 100644
index 0000000..9a16a34
--- /dev/null
+++ b/demo/build.rs
@@ -0,0 +1,17 @@
+// 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<()> {
+ let path = std::path::PathBuf::from("src");
+ let mut b = autocxx_build::Builder::new("src/main.rs", &[&path]).build()?;
+ b.flag_if_supported("-std=c++14").compile("autocxx-demo");
+
+ println!("cargo:rerun-if-changed=src/main.rs");
+ println!("cargo:rerun-if-changed=src/input.h");
+ Ok(())
+}
diff --git a/demo/src/input.h b/demo/src/input.h
new file mode 100644
index 0000000..b6cdd87
--- /dev/null
+++ b/demo/src/input.h
@@ -0,0 +1,35 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <sstream>
+#include <stdint.h>
+#include <string>
+
+class Goat {
+public:
+ Goat() : horns(0) {}
+ void add_a_horn();
+ std::string describe() const;
+private:
+ uint32_t horns;
+};
+
+inline uint32_t DoMath(uint32_t a) {
+ return a * 3;
+}
+
+inline void Goat::add_a_horn() { horns++; }
+inline std::string Goat::describe() const {
+ std::ostringstream oss;
+ std::string plural = horns == 1 ? "" : "s";
+ oss << "This goat has " << horns << " horn" << plural << ".";
+ return oss.str();
+}
diff --git a/demo/src/main.rs b/demo/src/main.rs
new file mode 100644
index 0000000..798f932
--- /dev/null
+++ b/demo/src/main.rs
@@ -0,0 +1,26 @@
+// 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! {
+ #include "input.h"
+ safety!(unsafe_ffi)
+ generate!("DoMath")
+ generate!("Goat")
+}
+
+fn main() {
+ println!("Hello, world! - C++ math should say 12={}", ffi::DoMath(4));
+ let mut goat = ffi::Goat::new().within_box();
+ goat.as_mut().add_a_horn();
+ goat.as_mut().add_a_horn();
+ assert_eq!(
+ goat.describe().as_ref().unwrap().to_string_lossy(),
+ "This goat has 2 horns."
+ );
+}