diff --git a/NEWS.md b/NEWS.md index db636373..dcf2c689 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # cpp11 (development version) +* 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 73cac5ca..68483bbf 100644 --- a/cpp11test/src/test-list.cpp +++ b/cpp11test/src/test-list.cpp @@ -122,4 +122,14 @@ 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..1b0d4d27 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,11 @@ 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_;