From f699c8193e83dfdac1bf00361f8b1b189309ad64 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Fri, 24 Jul 2020 12:33:34 +0200 Subject: [PATCH 1/2] allow = in writables::logicals. closes #56 --- inst/include/cpp11/doubles.hpp | 3 ++- inst/include/cpp11/integers.hpp | 3 ++- inst/include/cpp11/list.hpp | 3 ++- inst/include/cpp11/logicals.hpp | 10 +++++++++- inst/include/cpp11/r_vector.hpp | 3 ++- inst/include/cpp11/raws.hpp | 3 ++- inst/include/cpp11/strings.hpp | 3 ++- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 32ab6d12..14138902 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -49,7 +49,8 @@ typedef r_vector doubles; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=( +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=( const double& rhs) { if (is_altrep_) { // NOPROTECT: likely too costly to unwind protect every set elt diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 4b72740d..b8868970 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -51,7 +51,8 @@ typedef r_vector integers; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=(const int& rhs) { +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=(const int& rhs) { if (is_altrep_) { // NOPROTECT: likely too costly to unwind protect every set elt SET_INTEGER_ELT(data_, index_, rhs); diff --git a/inst/include/cpp11/list.hpp b/inst/include/cpp11/list.hpp index 6bfcceb4..4abe8ed8 100644 --- a/inst/include/cpp11/list.hpp +++ b/inst/include/cpp11/list.hpp @@ -55,7 +55,8 @@ typedef r_vector list; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=(const SEXP& rhs) { +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=(const SEXP& rhs) { SET_VECTOR_ELT(data_, index_, rhs); return *this; } diff --git a/inst/include/cpp11/logicals.hpp b/inst/include/cpp11/logicals.hpp index 2f347c9e..393d95a3 100644 --- a/inst/include/cpp11/logicals.hpp +++ b/inst/include/cpp11/logicals.hpp @@ -48,7 +48,8 @@ typedef r_vector logicals; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=( +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=( const Rboolean& rhs) { if (is_altrep_) { SET_LOGICAL_ELT(data_, index_, rhs); @@ -58,6 +59,13 @@ inline typename r_vector::proxy& r_vector::proxy::operator=( return *this; } +template <> +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=( + const bool& rhs) { + return operator=(rhs ? TRUE : FALSE); +} + template <> inline r_vector::proxy::operator Rboolean() const { if (p_ == nullptr) { diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index b1e37882..1222af4a 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -223,7 +223,8 @@ class r_vector : public cpp11::r_vector { public: proxy(SEXP data, const R_xlen_t index, T* const p, bool is_altrep); - proxy& operator=(const T& rhs); + template + proxy& operator=(const U& rhs); proxy& operator+=(const T& rhs); proxy& operator-=(const T& rhs); proxy& operator*=(const T& rhs); diff --git a/inst/include/cpp11/raws.hpp b/inst/include/cpp11/raws.hpp index 67cfd56b..6291034e 100644 --- a/inst/include/cpp11/raws.hpp +++ b/inst/include/cpp11/raws.hpp @@ -52,7 +52,8 @@ typedef r_vector raws; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=( +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=( const uint8_t& rhs) { if (is_altrep_) { // NOPROTECT: likely too costly to unwind protect every set elt diff --git a/inst/include/cpp11/strings.hpp b/inst/include/cpp11/strings.hpp index 1b7c1a53..79ded19a 100644 --- a/inst/include/cpp11/strings.hpp +++ b/inst/include/cpp11/strings.hpp @@ -49,7 +49,8 @@ typedef r_vector strings; namespace writable { template <> -inline typename r_vector::proxy& r_vector::proxy::operator=( +template <> +inline typename r_vector::proxy& r_vector::proxy::operator=( const r_string& rhs) { unwind_protect([&] { SET_STRING_ELT(data_, index_, rhs); }); return *this; From 5156ff06e11db46730b60152b2bd9717b45e61e5 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Mon, 27 Jul 2020 17:29:53 +0200 Subject: [PATCH 2/2] testing writable::logicals::proxy::operator=(bool) --- cpp11test/src/test-logicals.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cpp11test/src/test-logicals.cpp b/cpp11test/src/test-logicals.cpp index 298bb7ca..87a0c70d 100644 --- a/cpp11test/src/test-logicals.cpp +++ b/cpp11test/src/test-logicals.cpp @@ -126,6 +126,14 @@ context("logicals-C++") { UNPROTECT(1); } + test_that("writable::logicals::proxy::operator=(bool)") { + cpp11::writable::logicals y(2); + + y[0] = false; + y[1] = true; + expect_true(y[0] == TRUE); + expect_true(y[1] == FALSE); + } test_that("is_na(Rboolean)") { Rboolean x = TRUE; expect_true(!cpp11::is_na(x));