Skip to content

Commit

Permalink
Ensure we call operator SEXP() when copying or moving writable vectors
Browse files Browse the repository at this point in the history
The previous code was unintentially avoiding this call by copying the
`.data_` member directly.

Fixes #91
  • Loading branch information
jimhester committed Aug 13, 2020
1 parent 97cd93a commit 07b7ba7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cpp11 (development version)

* `data_frame()` objects now have the number of rows correctly set as real length, not the reserved length (#91)

# cpp11 0.2.1

* Ensures backwards compatibility with code generation from cpp11 0.1.0 (#88)
Expand Down
13 changes: 13 additions & 0 deletions cpp11test/src/test-data_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,17 @@ context("data_frame-C++") {
expect_true(cpp11::integers(df["a"])[0] == 1);
expect_true(cpp11::strings(df["b"])[2] == "c");
}

test_that("growing vectors uses proper length") {
using namespace cpp11::literals;

cpp11::writable::integers x, y;
for (int i = 0; i < 10; ++i) {
x.push_back(i);
y.push_back(i);
}
cpp11::writable::data_frame out({"x"_nm = x, "y"_nm = y});

expect_true(out.nrow() == 10);
}
}
8 changes: 3 additions & 5 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,22 +760,20 @@ inline typename r_vector<T>::iterator r_vector<T>::find(const r_string& name) co

template <typename T>
inline r_vector<T>::r_vector(const r_vector<T>& rhs)
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs.data_)),
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs)),
protect_(protect_sexp(data_)),
capacity_(rhs.capacity_) {}

template <typename T>
inline r_vector<T>::r_vector(r_vector<T>&& rhs)
: cpp11::r_vector<T>(rhs.data_),
protect_(protect_sexp(data_)),
capacity_(rhs.capacity_) {
: cpp11::r_vector<T>(rhs), protect_(protect_sexp(data_)), capacity_(rhs.capacity_) {
rhs.data_ = R_NilValue;
rhs.protect_ = R_NilValue;
}

template <typename T>
inline r_vector<T>::r_vector(const cpp11::r_vector<T>& rhs)
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs.data_)),
: cpp11::r_vector<T>(safe[Rf_shallow_duplicate](rhs)),
protect_(protect_sexp(data_)),
capacity_(rhs.length_) {}

Expand Down

0 comments on commit 07b7ba7

Please sign in to comment.