Skip to content

Commit

Permalink
Implement proxy::operator T() in r_vector.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Aug 9, 2024
1 parent 032a552 commit ae62cdf
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 52 deletions.
10 changes: 0 additions & 10 deletions inst/include/cpp11/doubles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ inline void r_vector<double>::set_elt(SEXP x, R_xlen_t i,
SET_REAL_ELT(x, i, value);
}

template <>
inline r_vector<double>::proxy::operator double() const {
if (p_ == nullptr) {
// NOPROTECT: likely too costly to unwind protect every elt
return REAL_ELT(data_, index_);
} else {
return *p_;
}
}

template <>
inline r_vector<double>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<double>(safe[Rf_allocVector](REALSXP, il.size())),
Expand Down
10 changes: 0 additions & 10 deletions inst/include/cpp11/integers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ inline void r_vector<int>::set_elt(SEXP x, R_xlen_t i,
SET_INTEGER_ELT(x, i, value);
}

template <>
inline r_vector<int>::proxy::operator int() const {
if (p_ == nullptr) {
// NOPROTECT: likely too costly to unwind protect every elt
return INTEGER_ELT(data_, index_);
} else {
return *p_;
}
}

template <>
inline r_vector<int>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<int>(safe[Rf_allocVector](INTSXP, il.size())),
Expand Down
5 changes: 0 additions & 5 deletions inst/include/cpp11/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ inline void r_vector<SEXP>::set_elt(SEXP x, R_xlen_t i,
SET_VECTOR_ELT(x, i, value);
}

template <>
inline r_vector<SEXP>::proxy::operator SEXP() const {
return VECTOR_ELT(data_, index_);
}

template <>
inline r_vector<SEXP>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<SEXP>(safe[Rf_allocVector](VECSXP, il.size())),
Expand Down
9 changes: 0 additions & 9 deletions inst/include/cpp11/logicals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ inline typename r_vector<r_bool>::proxy& r_vector<r_bool>::proxy::operator=(
return *this;
}

template <>
inline r_vector<r_bool>::proxy::operator r_bool() const {
if (p_ == nullptr) {
return LOGICAL_ELT(data_, index_);
} else {
return *p_;
}
}

inline bool operator==(const r_vector<r_bool>::proxy& lhs, r_bool rhs) {
return static_cast<r_bool>(lhs).operator==(rhs);
}
Expand Down
9 changes: 7 additions & 2 deletions inst/include/cpp11/r_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ class r_vector : public cpp11::r_vector<T> {
public:
proxy(SEXP data, const R_xlen_t index, underlying_type* const p, bool is_altrep);

/// Implemented in specialization
proxy& operator=(const T& rhs);
proxy& operator+=(const T& rhs);
proxy& operator-=(const T& rhs);
Expand All @@ -275,7 +274,6 @@ class r_vector : public cpp11::r_vector<T> {
void operator++();
void operator--();

/// Implemented in specialization
operator T() const;
};

Expand Down Expand Up @@ -1090,6 +1088,13 @@ inline void r_vector<T>::proxy::operator--() {
operator=(static_cast<T>(*this) - 1);
}

template <typename T>
inline r_vector<T>::proxy::operator T() const {
// Handles ALTREP, VECSXP, and STRSXP cases through `get_elt()`
const underlying_type elt = (p_ != nullptr) ? *p_ : r_vector::get_elt(data_, index_);
return static_cast<T>(elt);
}

template <typename T>
r_vector<T>::iterator::iterator(const r_vector& data, R_xlen_t pos)
: r_vector::const_iterator(&data, pos), data_(data) {}
Expand Down
10 changes: 0 additions & 10 deletions inst/include/cpp11/raws.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ inline void r_vector<uint8_t>::set_elt(SEXP x, R_xlen_t i,
#endif
}

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

template <>
inline r_vector<uint8_t>::r_vector(std::initializer_list<named_arg> il)
: cpp11::r_vector<uint8_t>(safe[Rf_allocVector](RAWSXP, il.size())),
Expand Down
6 changes: 0 additions & 6 deletions inst/include/cpp11/strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ inline void r_vector<r_string>::set_elt(SEXP x, R_xlen_t i,
SET_STRING_ELT(x, i, value);
}

template <>
inline r_vector<r_string>::proxy::operator r_string() const {
// NOPROTECT: likely too costly to unwind protect every elt
return STRING_ELT(data_, index_);
}

inline bool operator==(const r_vector<r_string>::proxy& lhs, r_string rhs) {
return static_cast<r_string>(lhs).operator==(static_cast<std::string>(rhs).c_str());
}
Expand Down

0 comments on commit ae62cdf

Please sign in to comment.