Skip to content

Commit 6117bd9

Browse files
committed
updating PR #368 with a templated Nullable
with a big thanks to Dan
1 parent 523c5cf commit 6117bd9

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

inst/include/Rcpp/Nullable.h

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Nullable.h: Rcpp R/C++ interface class library -- SEXP container which can be NULL
44
//
5-
// Copyright (C) 2015 Dirk Eddelbuettel
5+
// Copyright (C) 2015 Dirk Eddelbuettel and Daniel C. Dillon
66
//
77
// This file is part of Rcpp.
88
//
@@ -34,22 +34,45 @@
3434

3535
namespace Rcpp {
3636

37+
template<class T>
3738
class Nullable {
39+
private:
40+
template<class U>
41+
friend class InputParameter;
42+
43+
template<class U>
44+
friend class traits::Exporter;
45+
3846
public:
3947

4048
/**
4149
* Empty no-argument constructor of a Nullable object
4250
*
43-
* set validator to FALSE
51+
* Assigns (R's) NULL value, and sets validator to FALSE
52+
*/
53+
inline Nullable() : m_sexp(R_NilValue), m_set(false) {}
54+
55+
/**
56+
* Template constructor of a Nullable object
57+
*
58+
* Assigns object, and set validator to TRUE
4459
*/
45-
inline Nullable() : m_sexp(NULL), m_set(false) {}
60+
61+
inline Nullable(const T &t) : m_sexp(t), m_set(true) {}
62+
63+
protected:
4664

4765
/**
4866
* Standard constructor of a Nullable object
4967
*
5068
* @param SEXP is stored
5169
*/
52-
inline Nullable(SEXP t) : m_sexp(t), m_set(true) {}
70+
inline Nullable(SEXP t) {
71+
m_sexp = t;
72+
m_set = true;
73+
}
74+
75+
public:
5376

5477
/**
5578
* Copy constructor for Nullable object
@@ -68,7 +91,7 @@ namespace Rcpp {
6891
* @throw 'not initialized' if object has not been set
6992
*/
7093
inline operator SEXP() {
71-
checkIfSet();
94+
isSet();
7295
return m_sexp;
7396
}
7497

@@ -87,7 +110,7 @@ namespace Rcpp {
87110
*
88111
* @throw 'not initialized' if object has not been set
89112
*/
90-
inline bool isNull() {
113+
inline bool isNull() const {
91114
checkIfSet();
92115
return Rf_isNull(m_sexp);
93116
}
@@ -97,21 +120,21 @@ namespace Rcpp {
97120
*
98121
* @throw 'not initialized' if object has not been set
99122
*/
100-
inline bool isNotNull() {
123+
inline bool isNotNull() const {
101124
return ! isNull();
102125
}
103126

104127
/**
105128
* Test function to check if object has been initialized
106129
*
107130
*/
108-
inline bool isSet(void) { return m_set; }
131+
inline bool isSet(void) const { return m_set; }
109132

110133
private:
111134
SEXP m_sexp;
112135
bool m_set;
113136

114-
inline void checkIfSet(void) {
137+
inline void checkIfSet(void) const {
115138
if (!m_set) {
116139
throw ::Rcpp::exception("Not initialized");
117140
}

inst/unitTests/cpp/misc.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,22 @@ void test_stop_variadic() {
157157
}
158158

159159
// [[Rcpp::export]]
160-
bool testNullableForNull(Nullable N) {
161-
return N.isNull();
160+
bool testNullableForNull(Nullable<NumericMatrix> M) {
161+
return M.isNull();
162162
}
163163

164164
// [[Rcpp::export]]
165-
bool testNullableForNotNull(Nullable N) {
166-
return N.isNotNull();
165+
bool testNullableForNotNull(Nullable<NumericMatrix> M) {
166+
return M.isNotNull();
167167
}
168168

169169
// [[Rcpp::export]]
170-
SEXP testNullableOperator(Nullable N) {
171-
return N;
170+
SEXP testNullableOperator(Nullable<NumericMatrix> M) {
171+
return M;
172172
}
173173

174174
// [[Rcpp::export]]
175-
SEXP testNullableGet(Nullable N) {
176-
return N.get();
175+
SEXP testNullableGet(Nullable<NumericMatrix> M) {
176+
return M.get();
177177
}
178178

inst/unitTests/runit.misc.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,24 @@ if (.runThisTest) {
148148
}
149149

150150
test.NullableForNull <- function() {
151-
S <- seq(1, 10)
151+
M <- matrix(1:4, 2, 2)
152152
checkTrue( testNullableForNull(NULL) )
153-
checkTrue( ! testNullableForNull(S) )
153+
checkTrue( ! testNullableForNull(M) )
154154
}
155155

156156
test.NullableForNotNull <- function() {
157-
S <- seq(1, 10)
157+
M <- matrix(1:4, 2, 2)
158158
checkTrue( ! testNullableForNotNull(NULL) )
159-
checkTrue( testNullableForNotNull(S) )
159+
checkTrue( testNullableForNotNull(M) )
160160
}
161161

162162
test.NullableAccessOperator <- function() {
163-
S <- seq(1, 10)
164-
checkEquals( testNullableOperator(S), S )
163+
M <- matrix(1:4, 2, 2)
164+
checkEquals( testNullableOperator(M), M )
165165
}
166166

167167
test.NullableAccessGet <- function() {
168-
S <- seq(1, 10)
169-
checkEquals( testNullableGet(S), S )
168+
M <- matrix(1:4, 2, 2)
169+
checkEquals( testNullableGet(M), M )
170170
}
171171
}

0 commit comments

Comments
 (0)