Skip to content

Added as() and clone() to Nullable #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions inst/include/Rcpp/Nullable.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ namespace Rcpp {
return m_sexp;
}

/**
* Boolean test for usability as a T
*/
inline bool isUsable() const {
return m_set && !Rf_isNull(m_sexp);
}

/**
* Boolean test for NULL
*
Expand All @@ -116,6 +123,16 @@ namespace Rcpp {
*/
inline bool isSet(void) const { return m_set; }

/**
* Returns m_sexp as a T
*/
inline T as() { return get(); }

/**
* Return a clone of m_sexp as a T
*/
inline T clone() const { return Rcpp::clone(get()); }

private:
SEXP m_sexp;
bool m_set;
Expand Down
18 changes: 18 additions & 0 deletions inst/unitTests/cpp/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,21 @@ SEXP testNullableGet(const Nullable<NumericMatrix>& M) {
return M.get();
}

// [[Rcpp::export]]
NumericMatrix testNullableAs(Nullable<NumericMatrix>& M) {
return M.as();
}

// [[Rcpp::export]]
NumericMatrix testNullableClone(const Nullable<NumericMatrix>& M) {
return M.clone();
}

// [[Rcpp::export]]
SEXP testNullableIsUsable(const Nullable<NumericMatrix>& M) {
if (M.isUsable()) {
return M.clone();
} else {
return R_NilValue;
}
}
19 changes: 19 additions & 0 deletions inst/unitTests/runit.misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,23 @@ if (.runThisTest) {
M <- matrix(1:4, 2, 2)
checkEquals( testNullableGet(M), M )
}

test.NullableAccessAs <- function() {
M <- matrix(1:4, 2, 2)
checkEquals( testNullableAs(M), M )
}

test.NullableAccessClone <- function() {
M <- matrix(1:4, 2, 2)
checkEquals( testNullableClone(M), M )
}

test.NullableIsUsableTrue <- function() {
M <- matrix(1:4, 2, 2)
checkEquals( testNullableIsUsable(M), M)
}

test.NullableIsUsableFalse <- function() {
checkTrue(is.null(testNullableIsUsable(NULL)))
}
}