Skip to content

Commit a0321df

Browse files
committed
add cpp-interop solution
1 parent 235d7e0 commit a0321df

File tree

8 files changed

+914
-19
lines changed

8 files changed

+914
-19
lines changed

exercise-solutions/Cargo.lock

-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercise-solutions/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ members = [
1212
"tcp-server-exercises",
1313
"async-chat",
1414
"kani-linked-list",
15-
"iterators",
16-
"calculator/*",
1715
]
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "auto-rapid"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
autocxx = "0.27.1"
8+
cxx = "1.0"
9+
10+
[build-dependencies]
11+
autocxx-build = "0.27.1"
12+
miette = { version = "5", features = ["fancy"] } # optional but gives nicer error messages!
13+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() -> miette::Result<()> {
2+
let include_path = std::path::PathBuf::from("src");
3+
4+
// This assumes all your C++ bindings are in main.rs
5+
let mut b = autocxx_build::Builder::new("src/main.rs", [&include_path]).build()?;
6+
b.flag_if_supported("-std=c++20")
7+
.compile("autocxx-demo"); // arbitrary library name, pick anything
8+
println!("cargo:rerun-if-changed=src/main.rs");
9+
10+
// Add instructions to link to any C++ libraries you need.
11+
12+
Ok(())
13+
}
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Date,Open,High,Low,Close,Volume,Adj Close
2+
2017-02-24,64.529999,64.800003,64.139999,64.620003,21705200,64.620003
3+
2017-02-23,64.419998,64.730003,64.190002,64.620003,20235200,64.620003
4+
2017-02-22,64.330002,64.389999,64.050003,64.360001,19259700,64.360001
5+
2017-02-21,64.610001,64.949997,64.449997,64.489998,19384900,64.489998
6+
2017-02-17,64.470001,64.690002,64.300003,64.620003,21234600,64.620003
7+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use autocxx::prelude::*;
2+
use cxx::{let_cxx_string, CxxString};
3+
use ffi::rapidcsv::Document;
4+
5+
autocxx::include_cpp! {
6+
#include "wrapper.h"
7+
generate!("rapidcsv::Document")
8+
generate!("my_csv::open_csv")
9+
generate!("my_csv::get_string_cell")
10+
safety!(unsafe_ffi)
11+
}
12+
13+
fn main() {
14+
let_cxx_string!(file_name = "example.csv");
15+
let doc = ffi::my_csv::open_csv(&file_name).within_unique_ptr();
16+
let count = doc.GetRowCount();
17+
for i in 0..count {
18+
let date = doc.get_string_cell(0, i);
19+
println!("{}", date);
20+
}
21+
}
22+
23+
trait GetStringCell {
24+
fn get_string_cell(&self, column: usize, row: usize) -> UniquePtr<CxxString>;
25+
}
26+
27+
impl GetStringCell for Document {
28+
fn get_string_cell(&self, column: usize, row: usize) -> UniquePtr<CxxString> {
29+
ffi::my_csv::get_string_cell(self, column, row)
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "rapidcsv.h"
4+
5+
namespace my_csv {
6+
rapidcsv::Document open_csv(const std::string& pPath) {
7+
return rapidcsv::Document(pPath);
8+
}
9+
10+
std::string get_string_cell(const rapidcsv::Document& doc,const size_t pColumnIdx, const size_t pRowIdx) {
11+
return doc.GetCell<std::string>(pColumnIdx, pRowIdx);
12+
}
13+
}

0 commit comments

Comments
 (0)