Skip to content

Commit 6548717

Browse files
authored
Unify proxy assignment operators (#377)
1 parent 5167fa4 commit 6548717

File tree

6 files changed

+15
-49
lines changed

6 files changed

+15
-49
lines changed

inst/include/cpp11/doubles.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ inline void r_vector<double>::set_elt(
7171
SET_REAL_ELT(x, i, value);
7272
}
7373

74-
template <>
75-
inline typename r_vector<double>::proxy& r_vector<double>::proxy::operator=(
76-
const double& rhs) {
77-
if (is_altrep_) {
78-
// NOPROTECT: likely too costly to unwind protect every set elt
79-
SET_REAL_ELT(data_, index_, rhs);
80-
} else {
81-
*p_ = rhs;
82-
}
83-
return *this;
84-
}
85-
8674
template <>
8775
inline r_vector<double>::proxy::operator double() const {
8876
if (p_ == nullptr) {

inst/include/cpp11/integers.hpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ inline void r_vector<int>::set_elt(
7272
SET_INTEGER_ELT(x, i, value);
7373
}
7474

75-
template <>
76-
inline typename r_vector<int>::proxy& r_vector<int>::proxy::operator=(const int& rhs) {
77-
if (is_altrep_) {
78-
// NOPROTECT: likely too costly to unwind protect every set elt
79-
SET_INTEGER_ELT(data_, index_, rhs);
80-
} else {
81-
*p_ = rhs;
82-
}
83-
return *this;
84-
}
85-
8675
template <>
8776
inline r_vector<int>::proxy::operator int() const {
8877
if (p_ == nullptr) {

inst/include/cpp11/list.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ inline void r_vector<SEXP>::set_elt(
8181
SET_VECTOR_ELT(x, i, value);
8282
}
8383

84-
template <>
85-
inline typename r_vector<SEXP>::proxy& r_vector<SEXP>::proxy::operator=(const SEXP& rhs) {
86-
SET_VECTOR_ELT(data_, index_, rhs);
87-
return *this;
88-
}
89-
9084
template <>
9185
inline r_vector<SEXP>::proxy::operator SEXP() const {
9286
return VECTOR_ELT(data_, index_);

inst/include/cpp11/r_vector.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,21 @@ r_vector<T>::proxy::proxy(SEXP data, const R_xlen_t index,
10211021
typename r_vector::underlying_type* const p, bool is_altrep)
10221022
: data_(data), index_(index), p_(p), is_altrep_(is_altrep) {}
10231023

1024+
template <typename T>
1025+
inline typename r_vector<T>::proxy& r_vector<T>::proxy::operator=(const T& rhs) {
1026+
underlying_type elt = static_cast<underlying_type>(rhs);
1027+
1028+
if (p_ != nullptr) {
1029+
*p_ = elt;
1030+
} else {
1031+
// Handles ALTREP, VECSXP, and STRSXP cases.
1032+
// NOPROTECT: Likely too costly to unwind protect every set elt.
1033+
r_vector<T>::set_elt(data_, index_, elt);
1034+
}
1035+
1036+
return *this;
1037+
}
1038+
10241039
template <typename T>
10251040
inline typename r_vector<T>::proxy& r_vector<T>::proxy::operator+=(const T& rhs) {
10261041
operator=(static_cast<T>(*this) + rhs);

inst/include/cpp11/raws.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,6 @@ inline void r_vector<uint8_t>::set_elt(
8383
#endif
8484
}
8585

86-
template <>
87-
inline typename r_vector<uint8_t>::proxy& r_vector<uint8_t>::proxy::operator=(
88-
const uint8_t& rhs) {
89-
if (is_altrep_) {
90-
// NOPROTECT: likely too costly to unwind protect every set elt
91-
RAW(data_)[index_] = rhs;
92-
} else {
93-
*p_ = rhs;
94-
}
95-
return *this;
96-
}
97-
9886
template <>
9987
inline r_vector<uint8_t>::proxy::operator uint8_t() const {
10088
if (p_ == nullptr) {

inst/include/cpp11/strings.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ inline void r_vector<r_string>::set_elt(
7171
SET_STRING_ELT(x, i, value);
7272
}
7373

74-
template <>
75-
inline typename r_vector<r_string>::proxy& r_vector<r_string>::proxy::operator=(
76-
const r_string& rhs) {
77-
// NOPROTECT: likely too costly to unwind protect every elt
78-
SET_STRING_ELT(data_, index_, rhs);
79-
return *this;
80-
}
81-
8274
template <>
8375
inline r_vector<r_string>::proxy::operator r_string() const {
8476
// NOPROTECT: likely too costly to unwind protect every elt

0 commit comments

Comments
 (0)