Skip to content

Commit 83d9dae

Browse files
authored
adding empty to vector classes (#188)
1 parent fe9850f commit 83d9dae

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# cpp11 (development version)
22

3+
* Added `x.empty()` method to check if a vector is empty (@sbearrows, #182)
34
* New `cpp11::na()` function to return the NA sentinals for R objects(@sbearrows, #179)
45
* Incorrectly formatted cpp11 decorators now output a more informative error message (@sbearrows, #127)
56
* Generated registration code now uses C collation to avoid spurious diffs from `tools::package_native_routine_registration_skeleton()` (@sbearrows, #171)

cpp11test/src/test-list.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,14 @@ context("list-C++") {
122122
expect_true(first[0] == 1);
123123
expect_true(first[1] == 2);
124124
}
125+
126+
test_that("empty() works") {
127+
cpp11::writable::list x;
128+
129+
expect_true(x.empty());
130+
131+
cpp11::writable::list y(1);
132+
133+
expect_false(y.empty());
134+
}
125135
}

inst/include/cpp11/r_vector.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class r_vector {
112112

113113
operator sexp() const;
114114

115+
bool empty() const;
116+
115117
/// Provide access to the underlying data, mainly for interface
116118
/// compatibility with std::vector
117119
SEXP data() const;
@@ -389,6 +391,11 @@ inline r_vector<T>::operator SEXP() const {
389391
return data_;
390392
}
391393

394+
template <typename T>
395+
inline bool r_vector<T>::empty() const {
396+
return (!(this->size() > 0));
397+
}
398+
392399
template <typename T>
393400
inline r_vector<T>::operator sexp() const {
394401
return data_;

0 commit comments

Comments
 (0)