Skip to content

Commit 3e74cdd

Browse files
committed
Add std::string variants for all const char* accessors
Fixes #34
1 parent a322eaa commit 3e74cdd

File tree

9 files changed

+44
-0
lines changed

9 files changed

+44
-0
lines changed

cpp11test/src/test-strings.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ context("strings-C++") {
166166
x.at(0) = "bar";
167167
}
168168

169+
test_that("strings::operator=() works with std:strings") {
170+
cpp11::writable::strings x(Rf_mkChar("foo"));
171+
std::string y("bar");
172+
173+
x[0] = y;
174+
expect_true(x[0] == y);
175+
}
176+
169177
// test_that("writable::strings(ALTREP_SEXP)") {
170178
// SEXP x = PROTECT(R_compact_uint8_trange(1, 5));
171179
//// Need to find (or create) an altrep class that implements duplicate.

inst/include/cpp11/attribute_proxy.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class attribute_proxy {
1919
attribute_proxy(const T& parent, const char* index)
2020
: parent_(parent), symbol_(safe[Rf_install](index)) {}
2121

22+
attribute_proxy(const T& parent, const std::string& index)
23+
: parent_(parent), symbol_(safe[Rf_install](index.c_str())) {}
24+
2225
attribute_proxy(const T& parent, SEXP index) : parent_(parent), symbol_(index) {}
2326

2427
template <typename C>

inst/include/cpp11/environment.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ class environment {
3939
environment(SEXP env) : env_(env) {}
4040
proxy operator[](SEXP name) const { return {env_, name}; }
4141
proxy operator[](const char* name) const { return operator[](safe[Rf_install](name)); }
42+
proxy operator[](const std::string& name) const { return operator[](name.c_str()); }
4243

4344
bool exists(SEXP name) const {
4445
SEXP res = safe[Rf_findVarInFrame3](env_, name, FALSE);
4546
return res != R_UnboundValue;
4647
}
4748
bool exists(const char* name) const { return exists(safe[Rf_install](name)); }
4849

50+
bool exists(const std::string& name) const { return exists(name.c_str()); }
51+
4952
void remove(SEXP name) {
5053
#ifdef HAS_REMOVE_VAR_FROM_FRAME
5154
R_removeVarFromFrame(name, env_);

inst/include/cpp11/function.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ class function {
5151
class package {
5252
public:
5353
package(const char* name) : data_(get_namespace(name)) {}
54+
package(const std::string& name) : data_(get_namespace(name.c_str())) {}
5455
function operator[](const char* name) {
5556
return safe[Rf_findFun](safe[Rf_install](name), data_);
5657
}
58+
function operator[](const std::string& name) { return operator[](name.c_str()); }
5759

5860
private:
5961
static SEXP get_namespace(const char* name) {

inst/include/cpp11/list_of.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class list_of : public list {
1616
T operator[](R_xlen_t pos) { return list::operator[](pos); }
1717

1818
T operator[](const char* pos) { return list::operator[](pos); }
19+
20+
T operator[](const std::string& pos) { return list::operator[](pos.c_str()); }
1921
};
2022

2123
namespace writable {
@@ -36,6 +38,10 @@ class list_of : public writable::list {
3638
T operator[](const char* pos) {
3739
return static_cast<SEXP>(writable::list::operator[](pos));
3840
}
41+
42+
T operator[](const std::string& pos) {
43+
return static_cast<SEXP>(writable::list::operator[](pos.c_str()));
44+
}
3945
};
4046
} // namespace writable
4147

inst/include/cpp11/protect.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "R_ext/Utils.h" // for R_CheckUserInterrupt
99
#include "Rversion.h" // for R_VERSION, R_Version
1010

11+
#include <string>
12+
1113
#if defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0)
1214
#define HAS_UNWIND_PROTECT
1315
#endif
@@ -197,9 +199,19 @@ void stop(const char* fmt, Args... args) {
197199
unwind_protect([&] { Rf_error(fmt, args...); });
198200
}
199201

202+
template <typename... Args>
203+
void stop(const std::string& fmt, Args... args) {
204+
unwind_protect([&] { Rf_error(fmt.c_str(), args...); });
205+
}
206+
200207
template <typename... Args>
201208
void warning(const char* fmt, Args... args) {
202209
unwind_protect([&] { Rf_warning(fmt, args...); });
203210
}
204211

212+
template <typename... Args>
213+
void warning(const std::string& fmt, Args... args) {
214+
unwind_protect([&] { Rf_warning(fmt.c_str(), args...); });
215+
}
216+
205217
} // namespace cpp11

inst/include/cpp11/r_string.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class r_string {
1414
r_string() = default;
1515
r_string(SEXP data) : data_(data) {}
1616
r_string(const char* data) : data_(safe[Rf_mkCharCE](data, CE_UTF8)) {}
17+
r_string(const std::string& data) : data_(safe[Rf_mkCharCE](data.c_str(), CE_UTF8)) {}
1718

1819
operator SEXP() const { return data_; }
1920
operator std::string() const {
@@ -35,6 +36,10 @@ class r_string {
3536
return static_cast<std::string>(*this) == rhs;
3637
}
3738

39+
bool operator==(const std::string& rhs) const {
40+
return static_cast<std::string>(*this) == rhs;
41+
}
42+
3843
R_xlen_t size() const { return Rf_xlength(data_); }
3944

4045
private:

inst/include/cpp11/r_vector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class r_vector : public cpp11::r_vector<T> {
278278
r_vector(std::initializer_list<T> il);
279279
r_vector(std::initializer_list<named_arg> il);
280280
r_vector(std::initializer_list<const char*> il);
281+
r_vector(std::initializer_list<std::string> il);
281282

282283
template <typename Iter>
283284
r_vector(Iter first, Iter last);

inst/include/cpp11/strings.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ template <>
114114
inline r_vector<r_string>::r_vector(std::initializer_list<const char*> il)
115115
: cpp11::r_vector<r_string>(as_sexp(il)), capacity_(il.size()) {}
116116

117+
template <>
118+
inline r_vector<r_string>::r_vector(std::initializer_list<std::string> il)
119+
: cpp11::r_vector<r_string>(as_sexp(il)), capacity_(il.size()) {}
120+
117121
template <>
118122
inline r_vector<r_string>::r_vector(std::initializer_list<named_arg> il)
119123
: cpp11::r_vector<r_string>(safe[Rf_allocVector](STRSXP, il.size())),

0 commit comments

Comments
 (0)