Skip to content

Commit 3f90f02

Browse files
committed
Added as() and clone() member functions to Nullable
1 parent a8bbece commit 3f90f02

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

inst/include/Rcpp/Nullable.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ namespace Rcpp {
116116
*/
117117
inline bool isSet(void) const { return m_set; }
118118

119+
/**
120+
* Returns m_sexp as a T
121+
*/
122+
inline T as() { return Rcpp::as< T >(get()); }
123+
124+
/**
125+
* Return a clone of m_sexp as a T
126+
*/
127+
inline T clone() const { return Rcpp::clone(as()); }
128+
119129
private:
120130
SEXP m_sexp;
121131
bool m_set;

inst/unitTests/cpp/misc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,12 @@ 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+
}

inst/unitTests/runit.misc.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,14 @@ 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.NullableAccessAs <- function() {
195+
M <- matrix(1:4, 2, 2)
196+
checkEquals( testNullableClone(M), M )
197+
}
188198
}

0 commit comments

Comments
 (0)