Skip to content

Commit ac7defc

Browse files
committed
new class Nullable
1 parent e4868b8 commit ac7defc

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

inst/include/Rcpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
#include <Rcpp/Module.h>
6565
#include <Rcpp/InternalFunction.h>
6666

67+
#include <Rcpp/Nullable.h>
68+
6769
#ifndef RCPP_NO_SUGAR
6870
#include <Rcpp/sugar/sugar.h>
6971
#include <Rcpp/stats/stats.h>

inst/include/Rcpp/Nullable.h

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2+
//
3+
// Nullable.h: Rcpp R/C++ interface class library -- SEXP container which can be NULL
4+
//
5+
// Copyright (C) 2015 Dirk Eddelbuettel
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#ifndef Rcpp_Nullable_h
23+
#define Rcpp_Nullable_h
24+
25+
namespace Rcpp {
26+
class Nullable {
27+
public:
28+
29+
/**
30+
* Empty no-argument constructor of a Nullable object
31+
*
32+
* set validator to FALSE
33+
*/
34+
inline Nullable() : m_sexp(NULL), m_set(false) {}
35+
36+
/**
37+
* Standard constructor of a Nullable object
38+
*
39+
* @param SEXP is stored
40+
*/
41+
inline Nullable(SEXP t) : m_sexp(t), m_set(true) {}
42+
43+
/**
44+
* Copy constructor for Nullable object
45+
*
46+
* @param SEXP is used to update internal copy
47+
*/
48+
inline Nullable &operator=(SEXP sexp) {
49+
m_sexp = sexp;
50+
m_set = true;
51+
return *this;
52+
}
53+
54+
/**
55+
* operator SEXP() to return nullable object
56+
*
57+
* @throw 'not initialized' if object has not been set
58+
*/
59+
inline operator SEXP() {
60+
checkIfSet();
61+
return m_sexp;
62+
}
63+
64+
/**
65+
* get() accessor for object
66+
*
67+
* @throw 'not initialized' if object has not been set
68+
*/
69+
inline SEXP get() {
70+
checkIfSet();
71+
return m_sexp;
72+
}
73+
74+
/**
75+
* boolean test for NULL
76+
*
77+
* @throw 'not initialized' if object has not been set
78+
*/
79+
inline bool isNull() {
80+
checkIfSet();
81+
return Rf_isNull(m_sexp);
82+
}
83+
84+
/**
85+
* boolean test for not NULL
86+
*
87+
* @throw 'not initialized' if object has not been set
88+
*/
89+
inline bool isNotNull() {
90+
return ! isNull();
91+
}
92+
93+
private:
94+
SEXP m_sexp;
95+
bool m_set;
96+
97+
inline void checkIfSet(void) {
98+
if (!m_set) {
99+
throw ::Rcpp::exception("Not initialized");
100+
}
101+
}
102+
};
103+
}
104+
105+
#endif

0 commit comments

Comments
 (0)