Skip to content

Commit bd239dc

Browse files
sbearrowsjimhester
andauthored
added r_vector named() method (#189)
Co-authored-by: Jim Hester <[email protected]>
1 parent 83d9dae commit bd239dc

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# cpp11 (development version)
22

3-
* Added `x.empty()` method to check if a vector is empty (@sbearrows, #182)
3+
* New `x.named()` method to check if a vector is named (@sbearrows, #186)
4+
* New `x.empty()` method to check if a vector is empty (@sbearrows, #182)
45
* New `cpp11::na()` function to return the NA sentinals for R objects(@sbearrows, #179)
56
* Incorrectly formatted cpp11 decorators now output a more informative error message (@sbearrows, #127)
67
* 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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ context("list-C++") {
123123
expect_true(first[1] == 2);
124124
}
125125

126-
test_that("empty() works") {
126+
test_that("list.named() works") {
127+
cpp11::writable::list x({"bar"_nm = 2});
128+
expect_true(x.named());
129+
130+
cpp11::writable::list y(1);
131+
expect_false(y.named());
132+
}
133+
134+
test_that("list.empty() works") {
127135
cpp11::writable::list x;
128136

129137
expect_true(x.empty());

inst/include/cpp11/r_vector.hpp

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

107107
bool is_altrep() const;
108108

109+
bool named() const;
110+
109111
R_xlen_t size() const;
110112

111113
operator SEXP() const;
@@ -381,6 +383,11 @@ inline bool r_vector<T>::is_altrep() const {
381383
return is_altrep_;
382384
}
383385

386+
template <typename T>
387+
inline bool r_vector<T>::named() const {
388+
return ((this->names()) != R_NilValue);
389+
}
390+
384391
template <typename T>
385392
inline R_xlen_t r_vector<T>::size() const {
386393
return length_;

0 commit comments

Comments
 (0)