Rcpp Version 1.1.2
Loading...
Searching...
No Matches
Nullable.h
Go to the documentation of this file.
1
2// Nullable.h: Rcpp R/C++ interface class library -- SEXP container which can be NULL
3//
4// Copyright (C) 2015-2026 Dirk Eddelbuettel and Daniel C. Dillon
5//
6// This file is part of Rcpp.
7//
8// Rcpp is free software: you can redistribute it and/or modify it
9// under the terms of the GNU General Public License as published by
10// the Free Software Foundation, either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rcpp is distributed in the hope that it will be useful, but
14// WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef Rcpp_Nullable_h
22#define Rcpp_Nullable_h
23
24// We looked into the safe_bool_idiom [1] but found that more trouble than is
25// warranted here. We first and foremost want an operator SEXP() which got in
26// the way of redefining operator bool.
27// [1] http://www.artima.com/cppsource/safebool.html)
28
29namespace Rcpp {
30
31 template<class T>
32 class Nullable {
33 public:
34
40 inline Nullable() : m_sexp(R_NilValue), m_set(false) {}
41
47
48 inline Nullable(const T &t) : m_sexp(t), m_set(true) {}
49
55 inline Nullable(SEXP t) {
56 m_sexp = t;
57 m_set = true;
58 }
59
60 public:
61
67 inline Nullable &operator=(SEXP sexp) {
68 m_sexp = sexp;
69 m_set = true;
70 return *this;
71 }
72
78 inline operator SEXP() const {
79 checkIfSet();
80 return m_sexp;
81 }
82
83#if R_VERSION > R_Version(4,3,0) && !defined(RCPP_DISABLE_NULLABLE_T)
89 inline operator T() const {
90 checkIfSet();
91 return Rcpp::as<T>(m_sexp);
92 }
93#endif
94
100 inline SEXP get() const {
101 checkIfSet();
102 return m_sexp;
103 }
104
108 inline bool isUsable() const {
109 return m_set && !Rf_isNull(m_sexp);
110 }
111
117 inline bool isNull() const {
118 checkIfSet();
119 return Rf_isNull(m_sexp);
120 }
121
127 inline bool isNotNull() const {
128 return ! isNull();
129 }
130
135 inline bool isSet(void) const { return m_set; }
136
140 inline T as() { return Rcpp::as<T>(get()); }
141
145 inline T clone() const { return Rcpp::clone(get()); }
146
147 private:
148 SEXP m_sexp;
149 bool m_set;
150
151 inline void checkIfSet(void) const {
152 if (!m_set) {
153 throw ::Rcpp::exception("Not initialized");
154 }
155 }
156 };
157}
158
159#endif
bool isUsable() const
Definition Nullable.h:108
Nullable & operator=(SEXP sexp)
Definition Nullable.h:67
SEXP get() const
Definition Nullable.h:100
void checkIfSet(void) const
Definition Nullable.h:151
bool isSet(void) const
Definition Nullable.h:135
bool isNotNull() const
Definition Nullable.h:127
Nullable(const T &t)
Definition Nullable.h:48
bool isNull() const
Definition Nullable.h:117
Nullable(SEXP t)
Definition Nullable.h:55
T clone() const
Definition Nullable.h:145
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:150
T clone(const T &object)
Definition clone.h:33