Skip to content

Commit 1292d07

Browse files
committed
Added Nullable::isUsable() function
1 parent f78b08b commit 1292d07

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

inst/include/Rcpp/Nullable.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ namespace Rcpp {
9191
return m_sexp;
9292
}
9393

94+
/**
95+
* Boolean test for usability as a T
96+
*/
97+
inline bool isUsable() const {
98+
return m_set && !Rf_isNull(m_sexp);
99+
}
100+
94101
/**
95102
* Boolean test for NULL
96103
*

inst/unitTests/cpp/misc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,12 @@ NumericMatrix testNullableAs(Nullable<NumericMatrix>& M) {
207207
NumericMatrix testNullableClone(const Nullable<NumericMatrix>& M) {
208208
return M.clone();
209209
}
210+
211+
// [[Rcpp::export]]
212+
SEXP testNullableIsUsable(const Nullable<NumericMatrix>& M) {
213+
if (M.isUsable()) {
214+
return M.clone();
215+
} else {
216+
return R_NilValue;
217+
}
218+
}

inst/unitTests/runit.misc.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,13 @@ if (.runThisTest) {
195195
M <- matrix(1:4, 2, 2)
196196
checkEquals( testNullableClone(M), M )
197197
}
198+
199+
test.NullableIsUsableTrue <- function() {
200+
M <- matrix(1:4, 2, 2)
201+
checkEquals( testNullableIsUsable(M), M)
202+
}
203+
204+
test.NullableIsUsableFalse <- function() {
205+
checkTrue(is.null(testNullableIsUsable(NULL)))
206+
}
198207
}

0 commit comments

Comments
 (0)