Skip to content

Commit 05b11bf

Browse files
committed
Merge pull request #423 from dcdillon/master
Added as() and clone() to Nullable
2 parents a8bbece + 1292d07 commit 05b11bf

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

inst/include/Rcpp/Nullable.h

Lines changed: 17 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
*
@@ -116,6 +123,16 @@ namespace Rcpp {
116123
*/
117124
inline bool isSet(void) const { return m_set; }
118125

126+
/**
127+
* Returns m_sexp as a T
128+
*/
129+
inline T as() { return get(); }
130+
131+
/**
132+
* Return a clone of m_sexp as a T
133+
*/
134+
inline T clone() const { return Rcpp::clone(get()); }
135+
119136
private:
120137
SEXP m_sexp;
121138
bool m_set;

inst/unitTests/cpp/misc.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,21 @@ SEXP testNullableGet(const Nullable<NumericMatrix>& M) {
198198
return M.get();
199199
}
200200

201+
// [[Rcpp::export]]
202+
NumericMatrix testNullableAs(Nullable<NumericMatrix>& M) {
203+
return M.as();
204+
}
205+
206+
// [[Rcpp::export]]
207+
NumericMatrix testNullableClone(const Nullable<NumericMatrix>& M) {
208+
return M.clone();
209+
}
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,23 @@ if (.runThisTest) {
185185
M <- matrix(1:4, 2, 2)
186186
checkEquals( testNullableGet(M), M )
187187
}
188+
189+
test.NullableAccessAs <- function() {
190+
M <- matrix(1:4, 2, 2)
191+
checkEquals( testNullableAs(M), M )
192+
}
193+
194+
test.NullableAccessClone <- function() {
195+
M <- matrix(1:4, 2, 2)
196+
checkEquals( testNullableClone(M), M )
197+
}
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+
}
188207
}

0 commit comments

Comments
 (0)