Skip to content

Commit

Permalink
Unify proxy assignment operators (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan authored Aug 9, 2024
1 parent 5167fa4 commit 6548717
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 49 deletions.
12 changes: 0 additions & 12 deletions inst/include/cpp11/doubles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ inline void r_vector<double>::set_elt(
SET_REAL_ELT(x, i, value);
}

template <>
inline typename r_vector<double>::proxy& r_vector<double>::proxy::operator=(
const double& rhs) {
if (is_altrep_) {
// NOPROTECT: likely too costly to unwind protect every set elt
SET_REAL_ELT(data_, index_, rhs);
} else {
*p_ = rhs;
}
return *this;
}

template <>
inline r_vector<double>::proxy::operator double() const {
if (p_ == nullptr) {
Expand Down
11 changes: 0 additions & 11 deletions inst/include/cpp11/integers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ inline void r_vector<int>::set_elt(
SET_INTEGER_ELT(x, i, value);
}

template <>
inline typename r_vector<int>::proxy& r_vector<int>::proxy::operator=(const int& rhs) {
if (is_altrep_) {
// NOPROTECT: likely too costly to unwind protect every set elt
SET_INTEGER_ELT(data_, index_, rhs);
} else {
*p_ = rhs;
}
return *this;
}

template <>
inline r_vector<int>::proxy::operator int() const {
if (p_ == nullptr) {
Expand Down
6 changes: 0 additions & 6 deletions inst/include/cpp11/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ inline void r_vector<SEXP>::set_elt(
SET_VECTOR_ELT(x, i, value);
}

template <>
inline typename r_vector<SEXP>::proxy& r_vector<SEXP>::proxy::operator=(const SEXP& rhs) {
SET_VECTOR_ELT(data_, index_, rhs);
return *this;
}

template <>
inline r_vector<SEXP>::proxy::operator SEXP() const {
return VECTOR_ELT(data_, index_);
Expand Down
15 changes: 15 additions & 0 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,21 @@ r_vector<T>::proxy::proxy(SEXP data, const R_xlen_t index,
typename r_vector::underlying_type* const p, bool is_altrep)
: data_(data), index_(index), p_(p), is_altrep_(is_altrep) {}

template <typename T>
inline typename r_vector<T>::proxy& r_vector<T>::proxy::operator=(const T& rhs) {
underlying_type elt = static_cast<underlying_type>(rhs);

if (p_ != nullptr) {
*p_ = elt;
} else {
// Handles ALTREP, VECSXP, and STRSXP cases.
// NOPROTECT: Likely too costly to unwind protect every set elt.
r_vector<T>::set_elt(data_, index_, elt);
}

return *this;
}

template <typename T>
inline typename r_vector<T>::proxy& r_vector<T>::proxy::operator+=(const T& rhs) {
operator=(static_cast<T>(*this) + rhs);
Expand Down
12 changes: 0 additions & 12 deletions inst/include/cpp11/raws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@ inline void r_vector<uint8_t>::set_elt(
#endif
}

template <>
inline typename r_vector<uint8_t>::proxy& r_vector<uint8_t>::proxy::operator=(
const uint8_t& rhs) {
if (is_altrep_) {
// NOPROTECT: likely too costly to unwind protect every set elt
RAW(data_)[index_] = rhs;
} else {
*p_ = rhs;
}
return *this;
}

template <>
inline r_vector<uint8_t>::proxy::operator uint8_t() const {
if (p_ == nullptr) {
Expand Down
8 changes: 0 additions & 8 deletions inst/include/cpp11/strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ inline void r_vector<r_string>::set_elt(
SET_STRING_ELT(x, i, value);
}

template <>
inline typename r_vector<r_string>::proxy& r_vector<r_string>::proxy::operator=(
const r_string& rhs) {
// NOPROTECT: likely too costly to unwind protect every elt
SET_STRING_ELT(data_, index_, rhs);
return *this;
}

template <>
inline r_vector<r_string>::proxy::operator r_string() const {
// NOPROTECT: likely too costly to unwind protect every elt
Expand Down

0 comments on commit 6548717

Please sign in to comment.