File tree 4 files changed +83
-2
lines changed
exercise-templates/cpp-interop
4 files changed +83
-2
lines changed Original file line number Diff line number Diff line change @@ -4,3 +4,9 @@ version = "0.1.0"
4
4
edition = " 2021"
5
5
6
6
[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!
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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" )
3
15
}
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
+ }
You can’t perform that action at this time.
0 commit comments