/
RSexp.hpp
246 lines (199 loc) · 6.22 KB
/
RSexp.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
/*
* RSexp.hpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#ifndef R_R_SEXP_HPP
#define R_R_SEXP_HPP
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/any.hpp>
#include <boost/utility.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <core/Error.hpp>
#include <core/json/Json.hpp>
#include <r/RErrorCategory.hpp>
#include <r/RInternal.hpp>
// IMPORTANT NOTE: all code in r::sexp must provide "no jump" guarantee.
// See comment in RInternal.hpp for more info on this
namespace r {
namespace sexp {
class Protect;
// environments and namespaces
SEXP findNamespace(const std::string& name);
// variables within an environment
typedef std::pair<std::string,SEXP> Variable ;
void listEnvironment(SEXP env,
bool includeAll,
Protect* pProtect,
std::vector<Variable>* pVariables);
// object info
SEXP findVar(const std::string& name,
const std::string& ns = std::string());
SEXP findFunction(const std::string& name,
const std::string& ns = std::string());
std::string typeAsString(SEXP object);
std::string classOf(SEXP objectSEXP);
int length(SEXP object);
SEXP getNames(SEXP sexp);
core::Error getNames(SEXP sexp, std::vector<std::string>* pNames);
// type checking
bool isString(SEXP object);
bool isLanguage(SEXP object);
bool isMatrix(SEXP object);
bool isDataFrame(SEXP object);
bool isNull(SEXP object);
// type coercions
std::string asString(SEXP object);
std::string safeAsString(SEXP object,
const std::string& defValue = std::string());
int asInteger(SEXP object);
double asReal(SEXP object);
bool asLogical(SEXP object);
SEXP getAttrib(SEXP object, SEXP attrib);
// extract c++ type from R SEXP
core::Error extract(SEXP valueSEXP, int* pInt);
core::Error extract(SEXP valueSEXP, bool* pBool);
core::Error extract(SEXP valueSEXP, std::vector<int>* pVector);
core::Error extract(SEXP valueSEXP, std::string* pString);
core::Error extract(SEXP valueSEXP, std::vector<std::string>* pVector);
// create SEXP from c++ type
SEXP create(const core::json::Value& value, Protect* pProtect);
SEXP create(const char* value, Protect* pProtect);
SEXP create(const std::string& value, Protect* pProtect);
SEXP create(int value, Protect* pProtect);
SEXP create(double value, Protect* pProtect);
SEXP create(bool value, Protect* pProtect);
SEXP create(const std::vector<std::string>& value, Protect* pProtect);
SEXP create(const std::vector<int>& value, Protect*pProtect);
SEXP create(const std::vector<double>& value, Protect*pProtect);
SEXP create(const std::vector<bool>& value, Protect*pProtect);
SEXP create(const std::vector<boost::posix_time::ptime>& value,
Protect* pProtect);
SEXP create(const std::vector<std::pair<std::string,std::string> >& value,
Protect* pProtect);
SEXP create(const core::json::Array& value, Protect* pProtect);
SEXP create(const core::json::Object& value, Protect* pProtect);
inline int indexOfElementNamed(SEXP listSEXP, const std::string& name)
{
// get the names so we can determine which slot the element is in are in
std::vector<std::string> names;
core::Error error = r::sexp::getNames(listSEXP, &names);
if (error)
return -1;
// find the index
int valueIndex = -1;
for (int i = 0; i<(int)names.size(); i++)
{
if (names[i] == name)
{
valueIndex = i;
break;
}
}
// return
return valueIndex;
}
template <typename T>
core::Error getNamedListElement(SEXP listSEXP,
const std::string& name,
T* pValue)
{
// find the element
int valueIndex = indexOfElementNamed(listSEXP, name);
if (valueIndex != -1)
{
// get the appropriate value
SEXP valueSEXP = VECTOR_ELT(listSEXP, valueIndex);
return sexp::extract(valueSEXP, pValue);
}
else
{
// otherwise an error
core::Error error(r::errc::ListElementNotFoundError, ERROR_LOCATION);
error.addProperty("element", name);
return error;
}
}
// set list element by name. note that the specified element MUST already
// exist before the call
template <typename T>
core::Error setNamedListElement(SEXP listSEXP,
const std::string& name,
const T& value)
{
// convert to SEXP
r::sexp::Protect rProtect;
SEXP valueSEXP = create(value, &rProtect);
// find the element
int valueIndex = indexOfElementNamed(listSEXP, name);
if (valueIndex != -1)
{
// set the appropriate value and return success
SET_VECTOR_ELT(listSEXP, valueIndex, valueSEXP);
return core::Success();
}
else
{
// otherwise an error
core::Error error(r::errc::ListElementNotFoundError, ERROR_LOCATION);
error.addProperty("element", name);
return error;
}
}
// protect R expressions
class Protect : boost::noncopyable
{
public:
Protect()
: protectCount_(0)
{
}
explicit Protect(SEXP sexp)
: protectCount_(0)
{
add(sexp);
}
virtual ~Protect();
// COPYING: boost::noncopyable
void add(SEXP sexp);
void unprotectAll();
private:
int protectCount_ ;
};
class PreservedSEXP : boost::noncopyable
{
public:
PreservedSEXP();
explicit PreservedSEXP(SEXP sexp);
virtual ~PreservedSEXP();
void set(SEXP sexp);
SEXP get() const { return sexp_; }
bool isNil() const { return sexp_ == R_NilValue; }
typedef void (*unspecified_bool_type)();
static void unspecified_bool_true() {}
operator unspecified_bool_type() const
{
return isNil() ? 0 : unspecified_bool_true;
}
bool operator!() const
{
return isNil();
}
void releaseNow();
private:
SEXP sexp_;
};
} // namespace sexp
} // namespace r
#endif // R_R_SEXP_HPP