From 056e146a513ca184d17ae9f6364e8d18d1229e45 Mon Sep 17 00:00:00 2001 From: sbearrows Date: Wed, 23 Jun 2021 10:25:55 -0600 Subject: [PATCH 1/2] adding empty to vector classes --- NEWS.md | 2 ++ cpp11test/src/test-list.cpp | 11 +++++++++++ inst/include/cpp11/r_vector.hpp | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/NEWS.md b/NEWS.md index 17a55d2d..0c4d30bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ # cpp11 0.2.7 + +*Added `named()` and `empty()` methods to r_vector classes, returning true if named/empty (@sbearrows, #182, #186) * Outputting more informative error message when cpp11 decorators are incorrectly formatted (@sbearrows, #127) * Fix spurious diffs from `tools::package_native_routine_registration_skeleton()` by temporarily using C collation (@sbearrows, #171) * Fix a transient memory leak for functions that return values from `cpp11::unwind_protect()` and `cpp11::safe` (#154) diff --git a/cpp11test/src/test-list.cpp b/cpp11test/src/test-list.cpp index d856e8b8..98fdf1cc 100644 --- a/cpp11test/src/test-list.cpp +++ b/cpp11test/src/test-list.cpp @@ -126,4 +126,15 @@ context("list-C++") { expect_true(first[0] == 1); expect_true(first[1] == 2); } + + test_that("empty() works") { + cpp11::writable::list x; + + expect_true(x.empty()); + + cpp11::writable::list y(1); + + expect_false(y.empty()); + } + } diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index 3d9afdd9..e6b200e8 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -112,6 +112,8 @@ class r_vector { operator sexp() const; + bool empty() const; + /// Provide access to the underlying data, mainly for interface /// compatibility with std::vector SEXP data() const; @@ -389,6 +391,12 @@ inline r_vector::operator SEXP() const { return data_; } +template +inline bool r_vector::empty() const { + return (!(this->size() > 0)); +} + + template inline r_vector::operator sexp() const { return data_; From 7ddb5fd6377861602c19dc6fa7e7c2b53555c2d5 Mon Sep 17 00:00:00 2001 From: sbearrows Date: Wed, 23 Jun 2021 15:24:10 -0600 Subject: [PATCH 2/2] reformat and update news --- NEWS.md | 2 +- cpp11test/src/test-list.cpp | 1 - inst/include/cpp11/r_vector.hpp | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0d2c50d7..dcf2c689 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # cpp11 (development version) -*Added `named()` and `empty()` methods to r_vector classes, returning true if named/empty (@sbearrows, #182, #186) +* Added `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) diff --git a/cpp11test/src/test-list.cpp b/cpp11test/src/test-list.cpp index b418267f..68483bbf 100644 --- a/cpp11test/src/test-list.cpp +++ b/cpp11test/src/test-list.cpp @@ -132,5 +132,4 @@ context("list-C++") { expect_false(y.empty()); } - } diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index e6b200e8..1b0d4d27 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -396,7 +396,6 @@ inline bool r_vector::empty() const { return (!(this->size() > 0)); } - template inline r_vector::operator sexp() const { return data_;