|
1 | 1 | #pragma once
|
2 | 2 |
|
| 3 | +#include <initializer_list> // for initializer_list |
3 | 4 | #include <iterator>
|
4 | 5 | #include <string> // for string
|
5 | 6 |
|
6 |
| -#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT... |
7 |
| -#include "cpp11/r_bool.hpp" // for r_bool |
8 |
| -#include "cpp11/r_string.hpp" // for r_string |
9 |
| -#include "cpp11/r_vector.hpp" // for r_vector |
10 |
| -#include "cpp11/sexp.hpp" // for sexp |
| 7 | +#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT... |
| 8 | +#include "cpp11/attribute_proxy.hpp" // for attribute_proxy |
| 9 | +#include "cpp11/r_bool.hpp" // for r_bool |
| 10 | +#include "cpp11/r_string.hpp" // for r_string |
| 11 | +#include "cpp11/r_vector.hpp" // for r_vector |
| 12 | +#include "cpp11/sexp.hpp" // for sexp |
11 | 13 |
|
12 | 14 | namespace cpp11 {
|
13 | 15 |
|
@@ -190,11 +192,49 @@ class matrix : public matrix_slices<S> {
|
190 | 192 |
|
191 | 193 | // operator sexp() { return sexp(vector_); }
|
192 | 194 |
|
193 |
| - sexp attr(const char* name) const { return SEXP(vector_.attr(name)); } |
| 195 | + attribute_proxy<V> attr(const char* name) { return attribute_proxy<V>(vector_, name); } |
194 | 196 |
|
195 |
| - sexp attr(const std::string& name) const { return SEXP(vector_.attr(name)); } |
| 197 | + attribute_proxy<V> attr(const std::string& name) { |
| 198 | + return attribute_proxy<V>(vector_, name.c_str()); |
| 199 | + } |
| 200 | + |
| 201 | + attribute_proxy<V> attr(SEXP name) { return attribute_proxy<V>(vector_, name); } |
| 202 | + |
| 203 | + void attr(const char* name, SEXP value) { vector_.attr(name) = value; } |
| 204 | + |
| 205 | + void attr(const std::string& name, SEXP value) { vector_.attr(name) = value; } |
196 | 206 |
|
197 |
| - sexp attr(SEXP name) const { return SEXP(vector_.attr(name)); } |
| 207 | + void attr(SEXP name, SEXP value) { vector_.attr(name) = value; } |
| 208 | + |
| 209 | + void attr(const char* name, std::initializer_list<SEXP> value) { |
| 210 | + SEXP attr = PROTECT(Rf_allocVector(VECSXP, value.size())); |
| 211 | + int i = 0; |
| 212 | + for (SEXP v : value) { |
| 213 | + SET_VECTOR_ELT(attr, i++, v); |
| 214 | + } |
| 215 | + vector_.attr(name) = attr; |
| 216 | + UNPROTECT(1); |
| 217 | + } |
| 218 | + |
| 219 | + void attr(const std::string& name, std::initializer_list<SEXP> value) { |
| 220 | + SEXP attr = PROTECT(Rf_allocVector(VECSXP, value.size())); |
| 221 | + int i = 0; |
| 222 | + for (SEXP v : value) { |
| 223 | + SET_VECTOR_ELT(attr, i++, v); |
| 224 | + } |
| 225 | + vector_.attr(name) = attr; |
| 226 | + UNPROTECT(1); |
| 227 | + } |
| 228 | + |
| 229 | + void attr(SEXP name, std::initializer_list<SEXP> value) { |
| 230 | + SEXP attr = PROTECT(Rf_allocVector(VECSXP, value.size())); |
| 231 | + int i = 0; |
| 232 | + for (SEXP v : value) { |
| 233 | + SET_VECTOR_ELT(attr, i++, v); |
| 234 | + } |
| 235 | + vector_.attr(name) = attr; |
| 236 | + UNPROTECT(1); |
| 237 | + } |
198 | 238 |
|
199 | 239 | r_vector<r_string> names() const { return r_vector<r_string>(vector_.names()); }
|
200 | 240 |
|
|
0 commit comments