-
Notifications
You must be signed in to change notification settings - Fork 46
/
as.hpp
284 lines (237 loc) · 7.9 KB
/
as.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include <cmath>
#include <initializer_list> // for initializer_list
#include <string> // for basic_string
#include <type_traits> // for enable_if, is_same, is_constructible
#include "cpp11/protect.hpp" // for SEXP, SEXPREC, R_xlen_t, Rf_allocVector
namespace cpp11 {
template <typename T>
using is_constructible_from_sexp =
typename std::enable_if<(std::is_class<typename std::decay<T>::type>::value ||
std::is_same<T, SEXP>::value) &&
std::is_constructible<T, SEXP>::value,
typename std::decay<T>::type>::type;
template <typename T>
is_constructible_from_sexp<T> as_cpp(SEXP from) {
return from;
}
template <typename T>
using is_integral = typename std::enable_if<
std::is_integral<T>::value &&
!std::is_same<typename std::decay<T>::type, bool>::value &&
!std::is_same<typename std::decay<T>::type, char>::value,
typename std::decay<T>::type>::type;
template <typename T>
using is_integral_value = typename std::enable_if<
std::is_integral<T>::value &&
!std::is_same<typename std::decay<T>::type, bool>::value &&
!std::is_same<typename std::decay<T>::type, char>::value,
typename std::decay<T>::type>::value;
// https://stackoverflow.com/a/1521682/2055486
//
inline bool is_convertable_without_loss_to_integer(long double value) {
long double int_part;
return std::modf(value, &int_part) == 0.0;
}
template <typename T>
is_integral<T> as_cpp(SEXP from) {
if (Rf_isInteger(from)) {
if (Rf_xlength(from) == 1) {
return INTEGER_ELT(from, 0);
}
} else if (Rf_isReal(from)) {
if (Rf_xlength(from) == 1) {
double value = REAL_ELT(from, 0);
if (is_convertable_without_loss_to_integer(value)) {
return value;
}
}
}
stop("Expected single integer value");
return T();
}
template <typename T>
using is_logical =
typename std::enable_if<std::is_same<typename std::decay<T>::type, bool>::value,
typename std::decay<T>::type>::type;
template <typename T>
is_logical<T> as_cpp(SEXP from) {
if (Rf_isLogical(from)) {
if (Rf_xlength(from) == 1) {
return LOGICAL_ELT(from, 0) == 1;
}
}
stop("Expected single logical value");
return T();
}
template <typename T>
using is_floating_point_value =
typename std::enable_if<std::is_floating_point<T>::value, T>::type;
template <typename T>
is_floating_point_value<T> as_cpp(SEXP from) {
if (Rf_isReal(from)) {
if (Rf_xlength(from) == 1) {
return REAL_ELT(from, 0);
}
}
// All integers can be coerced to doubles, so we just convert them.
if (Rf_isInteger(from)) {
if (Rf_xlength(from) == 1) {
return INTEGER_ELT(from, 0);
}
}
stop("Expected single double value");
return T();
}
template <typename T>
using is_char =
typename std::enable_if<std::is_same<typename std::decay<T>::type, char>::value,
typename std::decay<T>::type>::type;
template <typename T>
is_char<T> as_cpp(SEXP from) {
if (Rf_isString(from)) {
if (Rf_xlength(from) == 1) {
const char* c_p = nullptr;
unwind_protect([&] { c_p = Rf_translateCharUTF8(STRING_ELT(from, 0)); });
return c_p[0];
}
}
stop("Expected string vector of length 1");
return T();
}
template <typename T>
using is_constructible_from_const_char = typename std::enable_if<
std::is_constructible<typename std::decay<T>::type, const char*>::value &&
!std::is_constructible<T, SEXP>::value,
typename std::decay<T>::type>::type;
template <typename T>
is_constructible_from_const_char<T> as_cpp(SEXP from) {
if (Rf_isString(from)) {
if (Rf_xlength(from) == 1) {
const char* c_p = nullptr;
// TODO: use vmaxget / vmaxset here?
unwind_protect([&] { c_p = Rf_translateCharUTF8(STRING_ELT(from, 0)); });
return {c_p};
}
}
stop("Expected string vector of length 1");
return T(nullptr);
}
template <typename T, is_integral<T>* = nullptr>
inline SEXP as_sexp(T from) {
return safe[Rf_ScalarInteger](from);
}
template <typename T, is_floating_point_value<T>* = nullptr>
inline SEXP as_sexp(T from) {
return safe[Rf_ScalarReal](from);
}
inline SEXP as_sexp(const std::string& from) {
SEXP res;
unwind_protect([&] { res = Rf_ScalarString(Rf_mkCharCE(from.c_str(), CE_UTF8)); });
return res;
}
template <typename T>
using is_bool =
typename std::enable_if<std::is_same<typename std::decay<T>::type, bool>::value,
typename std::decay<T>::type>::type;
template <typename T, is_bool<T> = false>
inline SEXP as_sexp(T from) {
return safe[Rf_ScalarLogical](from);
}
template <typename C, typename T = typename C::value_type,
typename std::enable_if<!std::is_convertible<C, SEXP>::value,
is_integral<T>>::type* = nullptr>
inline SEXP as_sexp(C from) {
R_xlen_t size = from.size();
SEXP data = safe[Rf_allocVector](INTSXP, size);
auto it = from.begin();
int* data_p = INTEGER(data);
for (R_xlen_t i = 0; i < size; ++i, ++it) {
data_p[i] = *it;
}
return data;
}
inline SEXP as_sexp(std::initializer_list<int> from) {
return as_sexp<std::initializer_list<int>>(from);
}
template <typename C, typename T = typename C::value_type,
is_floating_point_value<T>* = nullptr>
inline SEXP as_sexp(C from) {
R_xlen_t size = from.size();
SEXP data = safe[Rf_allocVector](REALSXP, size);
auto it = from.begin();
double* data_p = REAL(data);
for (R_xlen_t i = 0; i < size; ++i, ++it) {
data_p[i] = *it;
}
return data;
}
inline SEXP as_sexp(std::initializer_list<double> from) {
return as_sexp<std::initializer_list<double>>(from);
}
template <typename C, typename T = typename C::value_type, is_bool<T>* = nullptr>
inline SEXP as_sexp(C from) {
R_xlen_t size = from.size();
SEXP data = safe[Rf_allocVector](LGLSXP, size);
auto it = from.begin();
int* data_p = LOGICAL(data);
for (R_xlen_t i = 0; i < size; ++i, ++it) {
data_p[i] = *it;
}
return data;
}
inline SEXP as_sexp(std::initializer_list<bool> from) {
return as_sexp<std::initializer_list<bool>>(from);
}
template <typename T>
using is_convertible_to_std_string = typename std::enable_if<
std::is_convertible<typename std::decay<T>::type, std::string>::value &&
!std::is_convertible<typename std::decay<T>::type, const char*>::value,
typename std::decay<T>::type>::type;
template <typename C, typename T = typename C::value_type,
is_convertible_to_std_string<T>* = nullptr>
inline SEXP as_sexp(C from) {
R_xlen_t size = from.size();
SEXP data;
try {
data = PROTECT(safe[Rf_allocVector](STRSXP, size));
auto it = from.begin();
for (R_xlen_t i = 0; i < size; ++i, ++it) {
SET_STRING_ELT(data, i,
safe[Rf_mkCharCE](static_cast<std::string>(*it).c_str(), CE_UTF8));
}
} catch (const unwind_exception& e) {
UNPROTECT(1);
throw e;
}
UNPROTECT(1);
return data;
}
template <typename T>
using is_convertible_to_const_char = typename std::enable_if<
std::is_convertible<typename std::decay<T>::type, const char*>::value &&
!std::is_convertible<typename std::decay<T>::type, SEXP>::value,
typename std::decay<T>::type>::type;
template <typename C, typename T = typename C::value_type,
is_convertible_to_const_char<T>* = nullptr>
inline SEXP as_sexp(C from) {
R_xlen_t size = from.size();
SEXP data;
try {
data = PROTECT(safe[Rf_allocVector](STRSXP, size));
auto it = from.begin();
for (R_xlen_t i = 0; i < size; ++i, ++it) {
SET_STRING_ELT(data, i, safe[Rf_mkCharCE](static_cast<const char*>(*it), CE_UTF8));
}
} catch (const unwind_exception& e) {
UNPROTECT(1);
throw e;
}
UNPROTECT(1);
return data;
}
inline SEXP as_sexp(std::initializer_list<const char*> from) {
return as_sexp<std::initializer_list<const char*>>(from);
}
inline SEXP as_sexp(SEXP from) { return from; }
} // namespace cpp11