Skip to content

added r_vector named() method #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cpp11 (development version)

* Added `x.empty()` method to check if a vector is empty (@sbearrows, #182)
* New `x.named()` method to check if a vector is named (@sbearrows, #186)
* New `x.empty()` method to check if a vector is empty (@sbearrows, #182)
* New `cpp11::na()` function to return the NA sentinals for R objects(@sbearrows, #179)
* Incorrectly formatted cpp11 decorators now output a more informative error message (@sbearrows, #127)
* Generated registration code now uses C collation to avoid spurious diffs from `tools::package_native_routine_registration_skeleton()` (@sbearrows, #171)
Expand Down
10 changes: 9 additions & 1 deletion cpp11test/src/test-list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ context("list-C++") {
expect_true(first[1] == 2);
}

test_that("empty() works") {
test_that("list.named() works") {
cpp11::writable::list x({"bar"_nm = 2});
expect_true(x.named());

cpp11::writable::list y(1);
expect_false(y.named());
}

test_that("list.empty() works") {
cpp11::writable::list x;

expect_true(x.empty());
Expand Down
7 changes: 7 additions & 0 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class r_vector {

bool is_altrep() const;

bool named() const;

R_xlen_t size() const;

operator SEXP() const;
Expand Down Expand Up @@ -381,6 +383,11 @@ inline bool r_vector<T>::is_altrep() const {
return is_altrep_;
}

template <typename T>
inline bool r_vector<T>::named() const {
return ((this->names()) != R_NilValue);
}

template <typename T>
inline R_xlen_t r_vector<T>::size() const {
return length_;
Expand Down