Skip to content

Commit 235d7e0

Browse files
committed
working autocxx local demo
1 parent bc2ac0a commit 235d7e0

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

exercise-templates/cpp-interop/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
autocxx = "0.27.0"
8+
cxx = "1.0.78"
9+
10+
[build-dependencies]
11+
autocxx-build = "0.27.0"
12+
miette = { version = "5", features = ["fancy"] } # optional but gives nicer error messages!
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2020 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+
fn main() -> miette::Result<()> {
10+
let path = std::path::PathBuf::from("src");
11+
let mut b = autocxx_build::Builder::new("src/main.rs", [&path]).build()?;
12+
b.flag_if_supported("-std=c++14").compile("autocxx-demo");
13+
14+
println!("cargo:rerun-if-changed=src/main.rs");
15+
println!("cargo:rerun-if-changed=src/input.h");
16+
Ok(())
17+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2020 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+
#pragma once
10+
11+
#include <cstdint>
12+
#include <sstream>
13+
#include <stdint.h>
14+
#include <string>
15+
16+
class Goat {
17+
public:
18+
Goat() : horns(0) {}
19+
void add_a_horn();
20+
std::string describe() const;
21+
private:
22+
uint32_t horns;
23+
};
24+
25+
inline uint32_t DoMath(uint32_t a) {
26+
return a * 3;
27+
}
28+
29+
inline void Goat::add_a_horn() { horns++; }
30+
inline std::string Goat::describe() const {
31+
std::ostringstream oss;
32+
std::string plural = horns == 1 ? "" : "s";
33+
oss << "This goat has " << horns << " horn" << plural << ".";
34+
return oss.str();
35+
}
+25-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1-
fn main() {
2-
println!("Hello, world!");
1+
// Copyright 2020 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+
include_cpp! {
11+
#include "input.h"
12+
safety!(unsafe_ffi)
13+
generate!("DoMath")
14+
generate!("Goat")
315
}
16+
17+
fn main() {
18+
println!("Hello, world! - C++ math should say 12={}", ffi::DoMath(4));
19+
let mut goat = ffi::Goat::new().within_box();
20+
goat.as_mut().add_a_horn();
21+
goat.as_mut().add_a_horn();
22+
assert_eq!(
23+
goat.describe().as_ref().unwrap().to_string_lossy(),
24+
"This goat has 2 horns."
25+
);
26+
}

0 commit comments

Comments
 (0)