Skip to content

Commit 5b21f0e

Browse files
authored
Merge branch 'pacha' into issue406
2 parents 008ab7c + ccc6a00 commit 5b21f0e

30 files changed

+1871
-1511
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
config:
27+
- {os: macos-13, r: 'oldrel'}
2728
- {os: macos-latest, r: 'release'}
2829

2930
- {os: windows-latest, r: 'release'}
3031
# use 4.1 to check with rtools40's older compiler
3132
- {os: windows-latest, r: '4.1'}
3233

3334
# Use older ubuntu to maximise backward compatibility
34-
- {os: ubuntu-20.04, r: 'devel', http-user-agent: 'release'}
35-
- {os: ubuntu-20.04, r: 'release'}
36-
- {os: ubuntu-20.04, r: 'release', custom: 'no-cpp11test'}
37-
- {os: ubuntu-20.04, r: 'oldrel-1'}
38-
- {os: ubuntu-20.04, r: 'oldrel-2'}
39-
- {os: ubuntu-20.04, r: 'oldrel-3'}
40-
- {os: ubuntu-20.04, r: 'oldrel-4'}
35+
- {os: ubuntu-22.04, r: 'devel', http-user-agent: 'release'}
36+
- {os: ubuntu-22.04, r: 'release'}
37+
- {os: ubuntu-22.04, r: 'release', custom: 'no-cpp11test'}
38+
- {os: ubuntu-22.04, r: 'oldrel-1'}
39+
- {os: ubuntu-22.04, r: 'oldrel-2'}
40+
- {os: ubuntu-22.04, r: 'oldrel-3'}
41+
- {os: ubuntu-22.04, r: 'oldrel-4'}
4142

4243
env:
4344
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/format.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ name: format_check
99

1010
jobs:
1111
format_check:
12-
runs-on: ubuntu-20.04
12+
runs-on: ubuntu-22.04
1313
steps:
1414
- uses: actions/checkout@v2
1515

1616
- name: Install ClangFormat
17-
run: sudo apt-get install -y clang-format-10
17+
run: sudo apt-get install -y clang-format-12
1818

1919
- name: Run ClangFormat
20-
run: make format clang_format=clang-format-10
20+
run: make format clang_format=clang-format-12
2121

2222
- name: Check for a non-empty diff
2323
run: git diff-files -U --exit-code

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cpp11
22
Title: A C++11 Interface for R's C Interface
3-
Version: 0.5.1.9000
3+
Version: 0.5.2.9000
44
Authors@R:
55
c(
66
person("Davis", "Vaughan", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# cpp11 (development version)
22

3+
# cpp11 0.5.2
4+
5+
* Fixed an issue related to `-Wdeprecated-literal-operator` (#447, @andrjohns).
6+
37
# cpp11 0.5.1
48

59
* cpp11 now requires R >=4.0.0, in line with the

R/source.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#' uses 'CXX11' if unset.
1919
#' @param dir The directory to store the generated source files. `tempfile()` is
2020
#' used by default. The directory will be removed if `clean` is `TRUE`.
21+
#' @param local Passed to [dyn.load()]. If `TRUE` (the default) the shared
22+
#' library is loaded with local symbols; if `FALSE` symbols are made global
23+
#' (equivalent to `dyn.load(..., local = FALSE)`), which can be required when
24+
#' other shared objects need to see RTTI/vtable symbols from this library.
25+
#' @note See the unit test that demonstrates this usage at
26+
#' \code{tests/testthat/test-source-local.R} (shows how `local = FALSE` exports
27+
#' the necessary symbols so separate shared objects can link against them).
2128
#' @return For [cpp_source()] and `[cpp_function()]` the results of
2229
#' [dyn.load()] (invisibly). For `[cpp_eval()]` the results of the evaluated
2330
#' expression.
@@ -65,7 +72,7 @@
6572
#' }
6673
#'
6774
#' @export
68-
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile()) {
75+
cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), dir = tempfile(), local = TRUE) {
6976
stop_unless_installed(c("brio", "callr", "cli", "decor", "desc", "glue", "tibble", "vctrs"))
7077
if (!missing(file) && !file.exists(file)) {
7178
stop("Can't find `file` at this path:\n", file, "\n", call. = FALSE)
@@ -145,7 +152,7 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu
145152
brio::write_lines(r_functions, r_path)
146153
source(r_path, local = env)
147154

148-
dyn.load(shared_lib, local = TRUE, now = TRUE)
155+
dyn.load(shared_lib, local = local, now = TRUE)
149156
}
150157

151158
the <- new.env(parent = emptyenv())
@@ -183,7 +190,7 @@ generate_makevars <- function(includes, cxx_std) {
183190

184191
#' @rdname cpp_source
185192
#' @export
186-
cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11")) {
193+
cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), local = TRUE) {
187194
cpp_source(code = paste(c('#include "cpp11.hpp"',
188195
"using namespace ::cpp11;",
189196
"namespace writable = ::cpp11::writable;",
@@ -193,15 +200,16 @@ cpp_function <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE,
193200
env = env,
194201
clean = clean,
195202
quiet = quiet,
196-
cxx_std = cxx_std
203+
cxx_std = cxx_std,
204+
local = local
197205
)
198206
}
199207

200208
utils::globalVariables("f")
201209

202210
#' @rdname cpp_source
203211
#' @export
204-
cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11")) {
212+
cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx_std = Sys.getenv("CXX_STD", "CXX11"), local = TRUE) {
205213
cpp_source(code = paste(c('#include "cpp11.hpp"',
206214
"using namespace ::cpp11;",
207215
"namespace writable = ::cpp11::writable;",
@@ -214,7 +222,8 @@ cpp_eval <- function(code, env = parent.frame(), clean = TRUE, quiet = TRUE, cxx
214222
env = env,
215223
clean = clean,
216224
quiet = quiet,
217-
cxx_std = cxx_std
225+
cxx_std = cxx_std,
226+
local = local
218227
)
219228
f()
220229
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ Please note that the cpp11 project is released with a [Contributor Code of Condu
7878

7979
cpp11 would not exist without Rcpp.
8080
Thanks to the Rcpp authors, Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou, Nathan Russell, Douglas Bates and John Chambers for their work writing and maintaining Rcpp.
81+
82+
## Clang format
83+
84+
To match GHA, use clang-format-12 to format C++ code. With systems that provide clang-format-14 or newer, you can use Docker:
85+
86+
```bash
87+
docker run --rm -v "$PWD":/work -w /work ubuntu:22.04 bash -lc "\
88+
apt-get update && apt-get install -y clang-format-12 && \
89+
find . -name '*.cpp' -o -name '*.hpp' -o -name '*.h' | xargs -r clang-format-12 -i"
90+
```

cpp11test/R/cpp11.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ cpp11_insert_ <- function(num_sxp) {
8888
.Call(`_cpp11test_cpp11_insert_`, num_sxp)
8989
}
9090

91+
ordered_map_to_list_ <- function(x) {
92+
.Call(`_cpp11test_ordered_map_to_list_`, x)
93+
}
94+
95+
unordered_map_to_list_ <- function(x) {
96+
.Call(`_cpp11test_unordered_map_to_list_`, x)
97+
}
98+
9199
gibbs_cpp <- function(N, thin) {
92100
.Call(`_cpp11test_gibbs_cpp`, N, thin)
93101
}
@@ -108,6 +116,18 @@ row_sums <- function(x) {
108116
.Call(`_cpp11test_row_sums`, x)
109117
}
110118

119+
mat_mat_copy_dimnames <- function(x) {
120+
.Call(`_cpp11test_mat_mat_copy_dimnames`, x)
121+
}
122+
123+
mat_sexp_copy_dimnames <- function(x) {
124+
.Call(`_cpp11test_mat_sexp_copy_dimnames`, x)
125+
}
126+
127+
mat_mat_create_dimnames <- function() {
128+
.Call(`_cpp11test_mat_mat_create_dimnames`)
129+
}
130+
111131
col_sums <- function(x) {
112132
.Call(`_cpp11test_col_sums`, x)
113133
}
@@ -256,6 +276,14 @@ rcpp_push_and_truncate_ <- function(size_sxp) {
256276
.Call(`_cpp11test_rcpp_push_and_truncate_`, size_sxp)
257277
}
258278

279+
nullable_extptr_1 <- function() {
280+
.Call(`_cpp11test_nullable_extptr_1`)
281+
}
282+
283+
nullable_extptr_2 <- function() {
284+
.Call(`_cpp11test_nullable_extptr_2`)
285+
}
286+
259287
test_destruction_inner <- function() {
260288
invisible(.Call(`_cpp11test_test_destruction_inner`))
261289
}

cpp11test/src/cpp11.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ extern "C" SEXP _cpp11test_cpp11_insert_(SEXP num_sxp) {
173173
return cpp11::as_sexp(cpp11_insert_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(num_sxp)));
174174
END_CPP11
175175
}
176+
// map.cpp
177+
SEXP ordered_map_to_list_(cpp11::doubles x);
178+
extern "C" SEXP _cpp11test_ordered_map_to_list_(SEXP x) {
179+
BEGIN_CPP11
180+
return cpp11::as_sexp(ordered_map_to_list_(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles>>(x)));
181+
END_CPP11
182+
}
183+
// map.cpp
184+
SEXP unordered_map_to_list_(cpp11::doubles x);
185+
extern "C" SEXP _cpp11test_unordered_map_to_list_(SEXP x) {
186+
BEGIN_CPP11
187+
return cpp11::as_sexp(unordered_map_to_list_(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles>>(x)));
188+
END_CPP11
189+
}
176190
// matrix.cpp
177191
SEXP gibbs_cpp(int N, int thin);
178192
extern "C" SEXP _cpp11test_gibbs_cpp(SEXP N, SEXP thin) {
@@ -209,6 +223,27 @@ extern "C" SEXP _cpp11test_row_sums(SEXP x) {
209223
END_CPP11
210224
}
211225
// matrix.cpp
226+
cpp11::doubles_matrix<> mat_mat_copy_dimnames(cpp11::doubles_matrix<> x);
227+
extern "C" SEXP _cpp11test_mat_mat_copy_dimnames(SEXP x) {
228+
BEGIN_CPP11
229+
return cpp11::as_sexp(mat_mat_copy_dimnames(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
230+
END_CPP11
231+
}
232+
// matrix.cpp
233+
SEXP mat_sexp_copy_dimnames(cpp11::doubles_matrix<> x);
234+
extern "C" SEXP _cpp11test_mat_sexp_copy_dimnames(SEXP x) {
235+
BEGIN_CPP11
236+
return cpp11::as_sexp(mat_sexp_copy_dimnames(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles_matrix<>>>(x)));
237+
END_CPP11
238+
}
239+
// matrix.cpp
240+
cpp11::doubles_matrix<> mat_mat_create_dimnames();
241+
extern "C" SEXP _cpp11test_mat_mat_create_dimnames() {
242+
BEGIN_CPP11
243+
return cpp11::as_sexp(mat_mat_create_dimnames());
244+
END_CPP11
245+
}
246+
// matrix.cpp
212247
cpp11::doubles col_sums(cpp11::doubles_matrix<cpp11::by_column> x);
213248
extern "C" SEXP _cpp11test_col_sums(SEXP x) {
214249
BEGIN_CPP11
@@ -478,6 +513,20 @@ extern "C" SEXP _cpp11test_rcpp_push_and_truncate_(SEXP size_sxp) {
478513
return cpp11::as_sexp(rcpp_push_and_truncate_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(size_sxp)));
479514
END_CPP11
480515
}
516+
// test-external_pointer.cpp
517+
cpp11::external_pointer<int> nullable_extptr_1();
518+
extern "C" SEXP _cpp11test_nullable_extptr_1() {
519+
BEGIN_CPP11
520+
return cpp11::as_sexp(nullable_extptr_1());
521+
END_CPP11
522+
}
523+
// test-external_pointer.cpp
524+
cpp11::external_pointer<int> nullable_extptr_2();
525+
extern "C" SEXP _cpp11test_nullable_extptr_2() {
526+
BEGIN_CPP11
527+
return cpp11::as_sexp(nullable_extptr_2());
528+
END_CPP11
529+
}
481530
// test-protect-nested.cpp
482531
void test_destruction_inner();
483532
extern "C" SEXP _cpp11test_test_destruction_inner() {
@@ -528,6 +577,9 @@ static const R_CallMethodDef CallEntries[] = {
528577
{"_cpp11test_grow_strings_cpp11_", (DL_FUNC) &_cpp11test_grow_strings_cpp11_, 2},
529578
{"_cpp11test_grow_strings_manual_", (DL_FUNC) &_cpp11test_grow_strings_manual_, 2},
530579
{"_cpp11test_grow_strings_rcpp_", (DL_FUNC) &_cpp11test_grow_strings_rcpp_, 2},
580+
{"_cpp11test_mat_mat_copy_dimnames", (DL_FUNC) &_cpp11test_mat_mat_copy_dimnames, 1},
581+
{"_cpp11test_mat_mat_create_dimnames", (DL_FUNC) &_cpp11test_mat_mat_create_dimnames, 0},
582+
{"_cpp11test_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp11test_mat_sexp_copy_dimnames, 1},
531583
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
532584
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
533585
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
@@ -540,6 +592,9 @@ static const R_CallMethodDef CallEntries[] = {
540592
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
541593
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
542594
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
595+
{"_cpp11test_nullable_extptr_1", (DL_FUNC) &_cpp11test_nullable_extptr_1, 0},
596+
{"_cpp11test_nullable_extptr_2", (DL_FUNC) &_cpp11test_nullable_extptr_2, 0},
597+
{"_cpp11test_ordered_map_to_list_", (DL_FUNC) &_cpp11test_ordered_map_to_list_, 1},
543598
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
544599
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
545600
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
@@ -573,6 +628,7 @@ static const R_CallMethodDef CallEntries[] = {
573628
{"_cpp11test_sum_int_foreach_", (DL_FUNC) &_cpp11test_sum_int_foreach_, 1},
574629
{"_cpp11test_test_destruction_inner", (DL_FUNC) &_cpp11test_test_destruction_inner, 0},
575630
{"_cpp11test_test_destruction_outer", (DL_FUNC) &_cpp11test_test_destruction_outer, 0},
631+
{"_cpp11test_unordered_map_to_list_", (DL_FUNC) &_cpp11test_unordered_map_to_list_, 1},
576632
{"_cpp11test_upper_bound", (DL_FUNC) &_cpp11test_upper_bound, 2},
577633
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},
578634
{NULL, NULL, 0}

cpp11test/src/map.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "cpp11/as.hpp"
2+
#include "cpp11/doubles.hpp"
3+
4+
[[cpp11::register]] SEXP ordered_map_to_list_(cpp11::doubles x) {
5+
std::map<double, int> counts;
6+
int n = x.size();
7+
for (int i = 0; i < n; i++) {
8+
counts[x[i]]++;
9+
}
10+
return cpp11::as_sexp(counts);
11+
}
12+
13+
[[cpp11::register]] SEXP unordered_map_to_list_(cpp11::doubles x) {
14+
std::unordered_map<double, int> counts;
15+
int n = x.size();
16+
for (int i = 0; i < n; i++) {
17+
counts[x[i]]++;
18+
}
19+
return cpp11::as_sexp(counts);
20+
}

cpp11test/src/matrix.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "cpp11/matrix.hpp"
22
#include "Rmath.h"
33
#include "cpp11/doubles.hpp"
4+
#include "cpp11/list.hpp"
5+
#include "cpp11/strings.hpp"
46
using namespace cpp11;
57

68
[[cpp11::register]] SEXP gibbs_cpp(int N, int thin) {
@@ -86,6 +88,40 @@ using namespace Rcpp;
8688
return sums;
8789
}
8890

91+
[[cpp11::register]] cpp11::doubles_matrix<> mat_mat_copy_dimnames(
92+
cpp11::doubles_matrix<> x) {
93+
cpp11::writable::doubles_matrix<> out = x;
94+
95+
out.attr("dimnames") = x.attr("dimnames");
96+
97+
return out;
98+
}
99+
100+
[[cpp11::register]] SEXP mat_sexp_copy_dimnames(cpp11::doubles_matrix<> x) {
101+
cpp11::writable::doubles_matrix<> out = x;
102+
103+
out.attr("dimnames") = x.attr("dimnames");
104+
105+
return out;
106+
}
107+
108+
[[cpp11::register]] cpp11::doubles_matrix<> mat_mat_create_dimnames() {
109+
cpp11::writable::doubles_matrix<> out(2, 2);
110+
111+
out(0, 0) = 1;
112+
out(0, 1) = 2;
113+
out(1, 0) = 3;
114+
out(1, 1) = 4;
115+
116+
cpp11::writable::list dimnames(2);
117+
dimnames[0] = cpp11::strings({"a", "b"});
118+
dimnames[1] = cpp11::strings({"c", "d"});
119+
120+
out.attr("dimnames") = dimnames;
121+
122+
return out;
123+
}
124+
89125
[[cpp11::register]] cpp11::doubles col_sums(cpp11::doubles_matrix<cpp11::by_column> x) {
90126
cpp11::writable::doubles sums(x.ncol());
91127

0 commit comments

Comments
 (0)