-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
// 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." | ||
); | ||
} |