Skip to content

Commit d35ecde

Browse files
committed
Support pkg_types.h if it is included in inst/include
Fixes #32
1 parent 26d9d4f commit d35ecde

File tree

4 files changed

+107
-64
lines changed

4 files changed

+107
-64
lines changed

R/register.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ cpp_register <- function(path = ".", quiet = FALSE) {
6161
extra_includes <- c(extra_includes, "#include <cpp11/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
6262
}
6363

64-
pkg_types <- file.path(path, "src", paste0(package, "_types.h"))
65-
if (file.exists(pkg_types)) {
64+
pkg_types <- c(
65+
file.path(path, "src", paste0(package, "_types.h")),
66+
file.path(path, "inst", "include", paste0(package, "_types.h"))
67+
)
68+
if (any(file.exists(pkg_types))) {
6669
extra_includes <- c(
67-
sprintf('#include "%s"', basename(pkg_types)),
70+
sprintf('#include "%s"', basename(pkg_types[[1]])),
6871
extra_includes
6972
)
7073
}

cpp11test/R/cpp11.R

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,6 @@ cpp11_safe_ <- function(x_sxp) {
108108
.Call("_cpp11test_cpp11_safe_", x_sxp)
109109
}
110110

111-
sum_int_for_ <- function(x) {
112-
.Call("_cpp11test_sum_int_for_", x)
113-
}
114-
115-
rcpp_sum_dbl_for_ <- function(x_sxp) {
116-
.Call("_cpp11test_rcpp_sum_dbl_for_", x_sxp)
117-
}
118-
119-
rcpp_sum_dbl_foreach_ <- function(x_sxp) {
120-
.Call("_cpp11test_rcpp_sum_dbl_foreach_", x_sxp)
121-
}
122-
123-
rcpp_sum_dbl_accumulate_ <- function(x_sxp) {
124-
.Call("_cpp11test_rcpp_sum_dbl_accumulate_", x_sxp)
125-
}
126-
127-
rcpp_grow_ <- function(n_sxp) {
128-
.Call("_cpp11test_rcpp_grow_", n_sxp)
129-
}
130-
131111
sum_dbl_for_ <- function(x) {
132112
.Call("_cpp11test_sum_dbl_for_", x)
133113
}
@@ -155,3 +135,23 @@ sum_dbl_accumulate_ <- function(x) {
155135
sum_dbl_accumulate2_ <- function(x_sxp) {
156136
.Call("_cpp11test_sum_dbl_accumulate2_", x_sxp)
157137
}
138+
139+
sum_int_for_ <- function(x) {
140+
.Call("_cpp11test_sum_int_for_", x)
141+
}
142+
143+
rcpp_sum_dbl_for_ <- function(x_sxp) {
144+
.Call("_cpp11test_rcpp_sum_dbl_for_", x_sxp)
145+
}
146+
147+
rcpp_sum_dbl_foreach_ <- function(x_sxp) {
148+
.Call("_cpp11test_rcpp_sum_dbl_foreach_", x_sxp)
149+
}
150+
151+
rcpp_sum_dbl_accumulate_ <- function(x_sxp) {
152+
.Call("_cpp11test_rcpp_sum_dbl_accumulate_", x_sxp)
153+
}
154+
155+
rcpp_grow_ <- function(n_sxp) {
156+
.Call("_cpp11test_rcpp_grow_", n_sxp)
157+
}

cpp11test/src/cpp11.cpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -205,41 +205,6 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
205205
return cpp11::as_sexp(cpp11_safe_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
206206
END_CPP11
207207
}
208-
// sum_int.cpp
209-
int sum_int_for_(cpp11::integers x);
210-
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
211-
BEGIN_CPP11
212-
return cpp11::as_sexp(sum_int_for_(cpp11::unmove(cpp11::as_cpp<cpp11::integers>(x))));
213-
END_CPP11
214-
}
215-
// sum_rcpp.cpp
216-
SEXP rcpp_sum_dbl_for_(SEXP x_sxp);
217-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP x_sxp) {
218-
BEGIN_CPP11
219-
return cpp11::as_sexp(rcpp_sum_dbl_for_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
220-
END_CPP11
221-
}
222-
// sum_rcpp.cpp
223-
SEXP rcpp_sum_dbl_foreach_(SEXP x_sxp);
224-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP x_sxp) {
225-
BEGIN_CPP11
226-
return cpp11::as_sexp(rcpp_sum_dbl_foreach_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
227-
END_CPP11
228-
}
229-
// sum_rcpp.cpp
230-
SEXP rcpp_sum_dbl_accumulate_(SEXP x_sxp);
231-
extern "C" SEXP _cpp11test_rcpp_sum_dbl_accumulate_(SEXP x_sxp) {
232-
BEGIN_CPP11
233-
return cpp11::as_sexp(rcpp_sum_dbl_accumulate_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
234-
END_CPP11
235-
}
236-
// sum_rcpp.cpp
237-
SEXP rcpp_grow_(SEXP n_sxp);
238-
extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
239-
BEGIN_CPP11
240-
return cpp11::as_sexp(rcpp_grow_(cpp11::unmove(cpp11::as_cpp<SEXP>(n_sxp))));
241-
END_CPP11
242-
}
243208
// sum.cpp
244209
double sum_dbl_for_(cpp11::doubles x);
245210
extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) {
@@ -289,6 +254,41 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
289254
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
290255
END_CPP11
291256
}
257+
// sum_int.cpp
258+
int sum_int_for_(cpp11::integers x);
259+
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
260+
BEGIN_CPP11
261+
return cpp11::as_sexp(sum_int_for_(cpp11::unmove(cpp11::as_cpp<cpp11::integers>(x))));
262+
END_CPP11
263+
}
264+
// sum_rcpp.cpp
265+
SEXP rcpp_sum_dbl_for_(SEXP x_sxp);
266+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP x_sxp) {
267+
BEGIN_CPP11
268+
return cpp11::as_sexp(rcpp_sum_dbl_for_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
269+
END_CPP11
270+
}
271+
// sum_rcpp.cpp
272+
SEXP rcpp_sum_dbl_foreach_(SEXP x_sxp);
273+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP x_sxp) {
274+
BEGIN_CPP11
275+
return cpp11::as_sexp(rcpp_sum_dbl_foreach_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
276+
END_CPP11
277+
}
278+
// sum_rcpp.cpp
279+
SEXP rcpp_sum_dbl_accumulate_(SEXP x_sxp);
280+
extern "C" SEXP _cpp11test_rcpp_sum_dbl_accumulate_(SEXP x_sxp) {
281+
BEGIN_CPP11
282+
return cpp11::as_sexp(rcpp_sum_dbl_accumulate_(cpp11::unmove(cpp11::as_cpp<SEXP>(x_sxp))));
283+
END_CPP11
284+
}
285+
// sum_rcpp.cpp
286+
SEXP rcpp_grow_(SEXP n_sxp);
287+
extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) {
288+
BEGIN_CPP11
289+
return cpp11::as_sexp(rcpp_grow_(cpp11::unmove(cpp11::as_cpp<SEXP>(n_sxp))));
290+
END_CPP11
291+
}
292292

293293
extern "C" {
294294
/* .Call calls */
@@ -322,13 +322,13 @@ extern SEXP _cpp11test_rcpp_sum_dbl_for_(SEXP);
322322
extern SEXP _cpp11test_rcpp_sum_dbl_foreach_(SEXP);
323323
extern SEXP _cpp11test_remove_altrep(SEXP);
324324
extern SEXP _cpp11test_row_sums(SEXP);
325-
extern SEXP _cpp11test_sum_dbl_accumulate_(SEXP);
326325
extern SEXP _cpp11test_sum_dbl_accumulate2_(SEXP);
327-
extern SEXP _cpp11test_sum_dbl_for_(SEXP);
326+
extern SEXP _cpp11test_sum_dbl_accumulate_(SEXP);
328327
extern SEXP _cpp11test_sum_dbl_for2_(SEXP);
329328
extern SEXP _cpp11test_sum_dbl_for3_(SEXP);
330-
extern SEXP _cpp11test_sum_dbl_foreach_(SEXP);
329+
extern SEXP _cpp11test_sum_dbl_for_(SEXP);
331330
extern SEXP _cpp11test_sum_dbl_foreach2_(SEXP);
331+
extern SEXP _cpp11test_sum_dbl_foreach_(SEXP);
332332
extern SEXP _cpp11test_sum_int_for_(SEXP);
333333
extern SEXP _cpp11test_upper_bound(SEXP, SEXP);
334334
extern SEXP run_testthat_tests(SEXP);
@@ -364,13 +364,13 @@ static const R_CallMethodDef CallEntries[] = {
364364
{"_cpp11test_rcpp_sum_dbl_foreach_", (DL_FUNC) &_cpp11test_rcpp_sum_dbl_foreach_, 1},
365365
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
366366
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
367-
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
368367
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
369-
{"_cpp11test_sum_dbl_for_", (DL_FUNC) &_cpp11test_sum_dbl_for_, 1},
368+
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
370369
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},
371370
{"_cpp11test_sum_dbl_for3_", (DL_FUNC) &_cpp11test_sum_dbl_for3_, 1},
372-
{"_cpp11test_sum_dbl_foreach_", (DL_FUNC) &_cpp11test_sum_dbl_foreach_, 1},
371+
{"_cpp11test_sum_dbl_for_", (DL_FUNC) &_cpp11test_sum_dbl_for_, 1},
373372
{"_cpp11test_sum_dbl_foreach2_", (DL_FUNC) &_cpp11test_sum_dbl_foreach2_, 1},
373+
{"_cpp11test_sum_dbl_foreach_", (DL_FUNC) &_cpp11test_sum_dbl_foreach_, 1},
374374
{"_cpp11test_sum_int_for_", (DL_FUNC) &_cpp11test_sum_int_for_, 1},
375375
{"_cpp11test_upper_bound", (DL_FUNC) &_cpp11test_upper_bound, 2},
376376
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},

tests/testthat/test-register.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,46 @@ extern \"C\" void R_init_testPkg(DllInfo* dll){
564564
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
565565
expect_message(cpp_register(p), "1 functions decorated with [[cpp11::register]]", fixed = TRUE)
566566
})
567+
568+
it("includes pkg_types.h if included in src", {
569+
pkg <- local_package()
570+
p <- pkg_path(pkg)
571+
dir.create(file.path(p, "src"))
572+
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
573+
writeLines("#include <sstream>", file.path(p, "src", "testPkg_types.h"))
574+
cpp_register(p)
575+
576+
expect_true(
577+
any(
578+
grepl(
579+
pattern = '#include "testPkg_types.h"',
580+
x = readLines(file.path(p, "src", "cpp11.cpp")),
581+
fixed = TRUE
582+
)
583+
)
584+
)
585+
})
586+
587+
it("includes pkg_types.h if included in inst/include", {
588+
pkg <- local_package()
589+
p <- pkg_path(pkg)
590+
dir.create(file.path(p, "src"))
591+
file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp"))
592+
593+
dir.create(file.path(p, "inst", "include"), recursive = TRUE)
594+
writeLines("#include <sstream>", file.path(p, "inst", "include", "testPkg_types.h"))
595+
cpp_register(p)
596+
597+
expect_true(
598+
any(
599+
grepl(
600+
pattern = '#include "testPkg_types.h"',
601+
x = readLines(file.path(p, "src", "cpp11.cpp")),
602+
fixed = TRUE
603+
)
604+
)
605+
)
606+
})
567607
})
568608

569609
describe("generate_init_functions", {

0 commit comments

Comments
 (0)