| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,7 @@ Suggests: | |||
| 36 | 36 | knitr, | |
| 37 | 37 | lobstr, | |
| 38 | 38 | mockery, | |
| 39 | + progress, | ||
| 39 | 40 | rmarkdown, | |
| 40 | 41 | scales, | |
| 41 | 42 | testthat, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | # cpp11 (development version) | |
| 2 | 2 | ||
| 3 | + * `cpp_source()`, `cpp_function()` and `cpp_eval()` now support `[[cpp11::linking_to()]]` syntax to link to third party packages with C++ headers. (#48) | ||
| 4 | + | ||
| 3 | 5 | * `safe[]` can now work with functions that return any type (#70, @bkietz) | |
| 4 | 6 | ||
| 5 | 7 | * cpp11 is now able to compile on gcc 4.8.5 (#69, @bkietz) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,10 @@ | |||
| 4 | 4 | #' [cpp_function()] compiles and loads a single function for use in R. | |
| 5 | 5 | #' [cpp_eval()] evaluates a single C++ expression and returns the result. | |
| 6 | 6 | #' | |
| 7 | + #' Within C++ code you can use `[[cpp11::linking_to(pkgxyz)]]` to link to | ||
| 8 | + #' external packages. This is equivalent to putting those packages in the | ||
| 9 | + #' `LinkingTo` field in a package DESCRIPTION. | ||
| 10 | + #' | ||
| 7 | 11 | #' @param file A file containing C++ code to compile | |
| 8 | 12 | #' @param code If non-null, the C++ code to compile | |
| 9 | 13 | #' @param env The R environment where the R wrapping functions should be defined. | |
@@ -30,6 +34,30 @@ | |||
| 30 | 34 | #' ') | |
| 31 | 35 | #' | |
| 32 | 36 | #' num_odd(as.integer(c(1:10, 15, 23))) | |
| 37 | + #' | ||
| 38 | + #' if (require("progress")) { | ||
| 39 | + #' | ||
| 40 | + #' cpp_source( | ||
| 41 | + #' code = ' | ||
| 42 | + #' #include <cpp11/R.hpp> | ||
| 43 | + #' #include <RProgress.h> | ||
| 44 | + #' | ||
| 45 | + #' [[cpp11::linking_to(progress)]] | ||
| 46 | + #' | ||
| 47 | + #' [[cpp11::register]] void | ||
| 48 | + #' show_progress() { | ||
| 49 | + #' RProgress::RProgress pb("Downloading [:bar] ETA: :eta"); | ||
| 50 | + #' | ||
| 51 | + #' pb.tick(0); | ||
| 52 | + #' for (int i = 0; i < 100; i++) { | ||
| 53 | + #' usleep(2.0 / 100 * 1000000); | ||
| 54 | + #' pb.tick(); | ||
| 55 | + #' } | ||
| 56 | + #' } | ||
| 57 | + #' ') | ||
| 58 | + #' | ||
| 59 | + #' show_progress() | ||
| 60 | + #' } | ||
| 33 | 61 | #' } | |
| 34 | 62 | #' @export | |
| 35 | 63 | cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE) { | |
@@ -65,7 +93,9 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu | |||
| 65 | 93 | cpp_path <- file.path(dir, "src", "cpp11.cpp") | |
| 66 | 94 | brio::write_lines(c('#include "cpp11/declarations.hpp"', "using namespace cpp11;", cpp_functions_definitions), cpp_path) | |
| 67 | 95 | ||
| 68 | - includes <- generate_include_paths("cpp11") | ||
| 96 | + linking_to <- union(get_linking_to(all_decorations), "cpp11") | ||
| 97 | + | ||
| 98 | + includes <- generate_include_paths(linking_to) | ||
| 69 | 99 | ||
| 70 | 100 | if (isTRUE(clean)) { | |
| 71 | 101 | on.exit(unlink(dir, recursive = TRUE)) | |
@@ -142,3 +172,13 @@ cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE) { | |||
| 142 | 172 | quiet = quiet) | |
| 143 | 173 | f() | |
| 144 | 174 | } | |
| 175 | + | ||
| 176 | + get_linking_to <- function(decorations) { | ||
| 177 | + out <- decorations[decorations$decoration == "cpp11::linking_to", ] | ||
| 178 | + | ||
| 179 | + if (NROW(decorations) == 0) { | ||
| 180 | + return(character()) | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + as.character(unlist(out$params)) | ||
| 184 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,10 @@ | |||
| 1 | 1 | #include <numeric> | |
| 2 | 2 | #include "cpp11/doubles.hpp" | |
| 3 | 3 | ||
| 4 | - [[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) { | ||
| 4 | + [[cpp11::linking_to(foo)]] | ||
| 5 | + | ||
| 6 | + [[cpp11::register]] double | ||
| 7 | + sum_dbl_for_(cpp11::doubles x) { | ||
| 5 | 8 | double sum = 0.; | |
| 6 | 9 | R_xlen_t n = x.size(); | |
| 7 | 10 | for (R_xlen_t i = 0; i < n; ++i) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments