FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

replace protect_sexp with preserved.insert · mpadge/cpp11@76009ff · GitHub

Repository navigation

Commit 76009ff

Browse files
committed
replace protect_sexp with preserved.insert
1 parent 692242f commit 76009ff

14 files changed

Lines changed: 162 additions & 211 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ cpp11test/src/*.so
99
compile_flags.txt
1010
inst/doc
1111
vignettes/*_cache
12+
compile_commands.json

‎R/utils.R‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ is_interactive <- function() {
6767
}
6868
interactive()
6969
}
70+
71+
`.preserve_list` <- pairlist(NA, NA)

‎cpp11test/src/protect.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
[[cpp11::register]] void protect_one_cpp11_(SEXP x, int n) {
2020
for (R_xlen_t i = 0; i < n; ++i) {
21-
SEXP p = cpp11::protect_sexp(x);
22-
cpp11::release_protect(p);
21+
SEXP p = cpp11::preserved.insert(x);
22+
cpp11::preserved.release(p);
2323
}
2424
}
2525

@@ -63,12 +63,12 @@
6363
[[cpp11::register]] void protect_many_cpp11_(int n) {
6464
std::vector<SEXP> res;
6565
for (R_xlen_t i = 0; i < n; ++i) {
66-
res.push_back(cpp11::protect_sexp(Rf_ScalarInteger(n)));
66+
res.push_back(cpp11::preserved.insert(Rf_ScalarInteger(n)));
6767
}
6868

6969
for (R_xlen_t i = n - 1; i >= 0; --i) {
7070
SEXP x = res[i];
71-
cpp11::release_protect(x);
71+
cpp11::preserved.release(x);
7272
res.pop_back();
7373
}
7474
}

‎cpp11test/src/test-sexp.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ context("sexp-C++") {
1515

1616
expect_true(Rf_inherits(out, "data.frame"));
1717
}
18+
1819
test_that("scalar constructors work") {
1920
using namespace cpp11::literals;
2021
cpp11::writable::list out({

‎inst/include/cpp11/doubles.hpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ template <>
7979
inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
8080
: cpp11::r_vector<double>(safe[Rf_allocVector](REALSXP, il.size())),
8181
capacity_(il.size()) {
82-
protect_ = protect_sexp(data_);
82+
protect_ = preserved.insert(data_);
8383
int n_protected = 0;
8484

8585
try {
@@ -95,7 +95,7 @@ inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
9595
UNPROTECT(n_protected);
9696
});
9797
} catch (const unwind_exception& e) {
98-
release_protect(protect_);
98+
preserved.release(protect_);
9999
UNPROTECT(n_protected);
100100
throw e;
101101
}
@@ -106,8 +106,8 @@ inline void r_vector<double>::reserve(R_xlen_t new_capacity) {
106106
data_ = data_ == R_NilValue ? safe[Rf_allocVector](REALSXP, new_capacity)
107107
: safe[Rf_xlengthgets](data_, new_capacity);
108108
SEXP old_protect = protect_;
109-
protect_ = protect_sexp(data_);
110-
release_protect(old_protect);
109+
protect_ = preserved.insert(data_);
110+
preserved.release(old_protect);
111111

112112
data_p_ = REAL(data_);
113113
capacity_ = new_capacity;

‎inst/include/cpp11/integers.hpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "cpp11/as.hpp" // for as_sexp
1010
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
1111
#include "cpp11/named_arg.hpp" // for named_arg
12-
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
12+
#include "cpp11/protect.hpp" // for preserved
1313
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1414
#include "cpp11/sexp.hpp" // for sexp
1515

@@ -83,10 +83,10 @@ inline void r_vector<int>::reserve(R_xlen_t new_capacity) {
8383
SEXP old_protect = protect_;
8484

8585
// Protect the new data
86-
protect_ = protect_sexp(data_);
86+
protect_ = preserved.insert(data_);
8787

8888
// Release the old protection;
89-
release_protect(old_protect);
89+
preserved.release(old_protect);
9090

9191
data_p_ = INTEGER(data_);
9292
capacity_ = new_capacity;
@@ -96,7 +96,7 @@ template <>
9696
inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
9797
: cpp11::r_vector<int>(safe[Rf_allocVector](INTSXP, il.size())),
9898
capacity_(il.size()) {
99-
protect_ = protect_sexp(data_);
99+
protect_ = preserved.insert(data_);
100100
int n_protected = 0;
101101

102102
try {
@@ -112,7 +112,7 @@ inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
112112
UNPROTECT(n_protected);
113113
});
114114
} catch (const unwind_exception& e) {
115-
release_protect(protect_);
115+
preserved.release(protect_);
116116
UNPROTECT(n_protected);
117117
throw e;
118118
}

‎inst/include/cpp11/list.hpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "cpp11/R.hpp" // for SEXP, SEXPREC, SET_VECTOR_ELT
66
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
77
#include "cpp11/named_arg.hpp" // for named_arg
8-
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
8+
#include "cpp11/protect.hpp" // for protect_sexp, preserved.release
99
#include "cpp11/r_string.hpp" // for r_string
1010
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1111
#include "cpp11/sexp.hpp" // for sexp
@@ -75,7 +75,7 @@ template <>
7575
inline r_vector<SEXP>::r_vector(std::initializer_list<SEXP> il)
7676
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
7777
capacity_(il.size()) {
78-
protect_ = protect_sexp(data_);
78+
protect_ = preserved.insert(data_);
7979
auto it = il.begin();
8080
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
8181
SET_VECTOR_ELT(data_, i, *it);
@@ -86,7 +86,7 @@ template <>
8686
inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
8787
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
8888
capacity_(il.size()) {
89-
protect_ = protect_sexp(data_);
89+
protect_ = preserved.insert(data_);
9090
int n_protected = 0;
9191

9292
try {
@@ -102,7 +102,7 @@ inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
102102
UNPROTECT(n_protected);
103103
});
104104
} catch (const unwind_exception& e) {
105-
release_protect(protect_);
105+
preserved.release(protect_);
106106
UNPROTECT(n_protected);
107107
throw e;
108108
}
@@ -114,8 +114,8 @@ inline void r_vector<SEXP>::reserve(R_xlen_t new_capacity) {
114114
: safe[Rf_xlengthgets](data_, new_capacity);
115115

116116
SEXP old_protect = protect_;
117-
protect_ = protect_sexp(data_);
118-
release_protect(old_protect);
117+
protect_ = preserved.insert(data_);
118+
preserved.release(old_protect);
119119

120120
capacity_ = new_capacity;
121121
}

‎inst/include/cpp11/logicals.hpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "cpp11/R.hpp" // for Rboolean, SEXP, SEXPREC, Rf_all...
88
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
99
#include "cpp11/named_arg.hpp" // for named_arg
10-
#include "cpp11/protect.hpp" // for protect_sexp, release_protect
10+
#include "cpp11/protect.hpp" // for preserved
1111
#include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy
1212
#include "cpp11/sexp.hpp" // for sexp
1313

@@ -71,7 +71,7 @@ inline r_vector<Rboolean>::proxy::operator Rboolean() const {
7171
template <>
7272
inline r_vector<Rboolean>::r_vector(std::initializer_list<Rboolean> il)
7373
: cpp11::r_vector<Rboolean>(Rf_allocVector(LGLSXP, il.size())), capacity_(il.size()) {
74-
protect_ = protect_sexp(data_);
74+
protect_ = preserved.insert(data_);
7575
auto it = il.begin();
7676
for (R_xlen_t i = 0; i < capacity_; ++i, ++it) {
7777
SET_LOGICAL_ELT(data_, i, *it);
@@ -82,7 +82,7 @@ template <>
8282
inline r_vector<Rboolean>::r_vector(std::initializer_list<named_arg> il)
8383
: cpp11::r_vector<Rboolean>(safe[Rf_allocVector](LGLSXP, il.size())),
8484
capacity_(il.size()) {
85-
protect_ = protect_sexp(data_);
85+
protect_ = preserved.insert(data_);
8686
int n_protected = 0;
8787

8888
try {
@@ -98,7 +98,7 @@ inline r_vector<Rboolean>::r_vector(std::initializer_list<named_arg> il)
9898
UNPROTECT(n_protected);
9999
});
100100
} catch (const unwind_exception& e) {
101-
release_protect(protect_);
101+
preserved.release(protect_);
102102
UNPROTECT(n_protected);
103103
throw e;
104104
}
@@ -109,9 +109,9 @@ inline void r_vector<Rboolean>::reserve(R_xlen_t new_capacity) {
109109
data_ = data_ == R_NilValue ? safe[Rf_allocVector](LGLSXP, new_capacity)
110110
: safe[Rf_xlengthgets](data_, new_capacity);
111111
SEXP old_protect = protect_;
112-
protect_ = protect_sexp(data_);
112+
protect_ = preserved.insert(data_);
113113

114-
release_protect(old_protect);
114+
preserved.release(old_protect);
115115

116116
data_p_ = reinterpret_cast<Rboolean*>(LOGICAL(data_));
117117
capacity_ = new_capacity;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL