From e8c9dc04aa43b34cf040dd2a4db9010dce3c5db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 17 May 2023 13:07:57 +0200 Subject: [PATCH 01/54] include integers.hpp before doubles.hpp --- cpp11test/src/test-doubles.cpp | 3 ++- cpp11test/src/test-list.cpp | 3 ++- cpp11test/src/test-matrix.cpp | 3 ++- cpp11test/src/test-nas.cpp | 3 ++- inst/include/cpp11.hpp | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 2d847405..22c9ec5f 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -1,7 +1,8 @@ #include +#include "cpp11/integers.hpp" + #include "cpp11/doubles.hpp" #include "cpp11/function.hpp" -#include "cpp11/integers.hpp" #include "cpp11/sexp.hpp" #include "cpp11/strings.hpp" diff --git a/cpp11test/src/test-list.cpp b/cpp11test/src/test-list.cpp index 0abf6002..b9e2b871 100644 --- a/cpp11test/src/test-list.cpp +++ b/cpp11test/src/test-list.cpp @@ -1,5 +1,6 @@ -#include "cpp11/doubles.hpp" #include "cpp11/integers.hpp" + +#include "cpp11/doubles.hpp" #include "cpp11/list.hpp" #include "cpp11/logicals.hpp" #include "cpp11/raws.hpp" diff --git a/cpp11test/src/test-matrix.cpp b/cpp11test/src/test-matrix.cpp index 693d6ec7..e9552e00 100644 --- a/cpp11test/src/test-matrix.cpp +++ b/cpp11test/src/test-matrix.cpp @@ -1,6 +1,7 @@ +#include "cpp11/integers.hpp" + #include "cpp11/doubles.hpp" #include "cpp11/function.hpp" -#include "cpp11/integers.hpp" #include "cpp11/matrix.hpp" #include diff --git a/cpp11test/src/test-nas.cpp b/cpp11test/src/test-nas.cpp index a0ca5848..5c3ef350 100644 --- a/cpp11test/src/test-nas.cpp +++ b/cpp11test/src/test-nas.cpp @@ -1,5 +1,6 @@ -#include "cpp11/doubles.hpp" #include "cpp11/integers.hpp" + +#include "cpp11/doubles.hpp" #include "cpp11/r_bool.hpp" #include "cpp11/r_string.hpp" diff --git a/inst/include/cpp11.hpp b/inst/include/cpp11.hpp index 71e1cf1d..20f8f102 100644 --- a/inst/include/cpp11.hpp +++ b/inst/include/cpp11.hpp @@ -5,11 +5,12 @@ #include "cpp11/as.hpp" #include "cpp11/attribute_proxy.hpp" #include "cpp11/data_frame.hpp" +#include "cpp11/integers.hpp" + #include "cpp11/doubles.hpp" #include "cpp11/environment.hpp" #include "cpp11/external_pointer.hpp" #include "cpp11/function.hpp" -#include "cpp11/integers.hpp" #include "cpp11/list.hpp" #include "cpp11/list_of.hpp" #include "cpp11/logicals.hpp" From 03dc96bd42ed0e33d639af3009de224cfb84e851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 17 May 2023 13:23:05 +0200 Subject: [PATCH 02/54] Revert "include integers.hpp before doubles.hpp" This reverts commit e8c9dc04aa43b34cf040dd2a4db9010dce3c5db4. --- cpp11test/src/test-doubles.cpp | 3 +-- cpp11test/src/test-list.cpp | 3 +-- cpp11test/src/test-matrix.cpp | 3 +-- cpp11test/src/test-nas.cpp | 3 +-- inst/include/cpp11.hpp | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 22c9ec5f..2d847405 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -1,8 +1,7 @@ #include -#include "cpp11/integers.hpp" - #include "cpp11/doubles.hpp" #include "cpp11/function.hpp" +#include "cpp11/integers.hpp" #include "cpp11/sexp.hpp" #include "cpp11/strings.hpp" diff --git a/cpp11test/src/test-list.cpp b/cpp11test/src/test-list.cpp index b9e2b871..0abf6002 100644 --- a/cpp11test/src/test-list.cpp +++ b/cpp11test/src/test-list.cpp @@ -1,6 +1,5 @@ -#include "cpp11/integers.hpp" - #include "cpp11/doubles.hpp" +#include "cpp11/integers.hpp" #include "cpp11/list.hpp" #include "cpp11/logicals.hpp" #include "cpp11/raws.hpp" diff --git a/cpp11test/src/test-matrix.cpp b/cpp11test/src/test-matrix.cpp index e9552e00..693d6ec7 100644 --- a/cpp11test/src/test-matrix.cpp +++ b/cpp11test/src/test-matrix.cpp @@ -1,7 +1,6 @@ -#include "cpp11/integers.hpp" - #include "cpp11/doubles.hpp" #include "cpp11/function.hpp" +#include "cpp11/integers.hpp" #include "cpp11/matrix.hpp" #include diff --git a/cpp11test/src/test-nas.cpp b/cpp11test/src/test-nas.cpp index 5c3ef350..a0ca5848 100644 --- a/cpp11test/src/test-nas.cpp +++ b/cpp11test/src/test-nas.cpp @@ -1,6 +1,5 @@ -#include "cpp11/integers.hpp" - #include "cpp11/doubles.hpp" +#include "cpp11/integers.hpp" #include "cpp11/r_bool.hpp" #include "cpp11/r_string.hpp" diff --git a/inst/include/cpp11.hpp b/inst/include/cpp11.hpp index 20f8f102..71e1cf1d 100644 --- a/inst/include/cpp11.hpp +++ b/inst/include/cpp11.hpp @@ -5,12 +5,11 @@ #include "cpp11/as.hpp" #include "cpp11/attribute_proxy.hpp" #include "cpp11/data_frame.hpp" -#include "cpp11/integers.hpp" - #include "cpp11/doubles.hpp" #include "cpp11/environment.hpp" #include "cpp11/external_pointer.hpp" #include "cpp11/function.hpp" +#include "cpp11/integers.hpp" #include "cpp11/list.hpp" #include "cpp11/list_of.hpp" #include "cpp11/logicals.hpp" From eff355b6a2337d51eb3145908d09ebe254fef91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 17 May 2023 13:24:09 +0200 Subject: [PATCH 03/54] integers.hpp includes doubles.hpp, for is_na --- inst/include/cpp11/integers.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 92b1fb12..50a1d141 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -6,6 +6,7 @@ #include "R_ext/Arith.h" // for NA_INTEGER #include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector +#include "cpp11/doubles.hpp" #include "cpp11/as.hpp" // for as_sexp #include "cpp11/attribute_proxy.hpp" // for attribute_proxy #include "cpp11/named_arg.hpp" // for named_arg From cc6174d2044e4949e3adf8d78231b3658c7667ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 17 May 2023 13:29:06 +0200 Subject: [PATCH 04/54] move doubles.hpp below --- inst/include/cpp11/integers.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 50a1d141..12019a8a 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -6,7 +6,6 @@ #include "R_ext/Arith.h" // for NA_INTEGER #include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_allocVector -#include "cpp11/doubles.hpp" #include "cpp11/as.hpp" // for as_sexp #include "cpp11/attribute_proxy.hpp" // for attribute_proxy #include "cpp11/named_arg.hpp" // for named_arg @@ -14,6 +13,8 @@ #include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy #include "cpp11/sexp.hpp" // for sexp +#include "cpp11/doubles.hpp" + // Specializations for integers namespace cpp11 { From 48269186cfd78fe77d562db2625d7c6b6f6e03ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Mon, 22 May 2023 10:53:35 +0200 Subject: [PATCH 05/54] define is_na in R.hpp --- inst/include/cpp11/R.hpp | 7 +++++++ inst/include/cpp11/doubles.hpp | 5 ----- inst/include/cpp11/integers.hpp | 3 --- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 59054591..2e8ac2ff 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #ifdef R_INTERNALS_H_ #if !(defined(R_NO_REMAP) && defined(STRICT_R_HEADERS)) #error R headers were included before cpp11 headers \ @@ -52,4 +54,9 @@ inline bool is_na(const T& value) { return value == na(); } +template <> +inline bool is_na(const double& x) { + return ISNA(x); +} + } // namespace cpp11 diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index a20bdafa..08cd761b 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -140,11 +140,6 @@ inline double na() { return NA_REAL; } -template <> -inline bool is_na(const double& x) { - return ISNA(x); -} - // forward declarations typedef r_vector integers; diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 12019a8a..e793779b 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -150,9 +150,6 @@ inline int na() { // forward declarations typedef r_vector doubles; -template <> -bool is_na(const double& x); - template <> double r_vector::operator[](const R_xlen_t pos) const; From bbe9e2575f6fe96a106d27067e06a160e2c774aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Mon, 22 May 2023 10:56:46 +0200 Subject: [PATCH 06/54] unnecessary include --- inst/include/cpp11/R.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 2e8ac2ff..f7272a2d 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #ifdef R_INTERNALS_H_ #if !(defined(R_NO_REMAP) && defined(STRICT_R_HEADERS)) #error R headers were included before cpp11 headers \ From d022c62615dff41ba71ac49a73db9ed6007216af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Mon, 22 May 2023 10:59:04 +0200 Subject: [PATCH 07/54] proably not needed --- inst/include/cpp11/integers.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index e793779b..9fa99d33 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -13,8 +13,6 @@ #include "cpp11/r_vector.hpp" // for r_vector, r_vector<>::proxy #include "cpp11/sexp.hpp" // for sexp -#include "cpp11/doubles.hpp" - // Specializations for integers namespace cpp11 { From b2c481b1f82b0e24b142b8b02a97430e6c2f1e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Mon, 22 May 2023 12:44:03 +0200 Subject: [PATCH 08/54] try different approach --- inst/include/cpp11/R.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index f7272a2d..75d9c3f6 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -53,7 +53,7 @@ inline bool is_na(const T& value) { } template <> -inline bool is_na(const double& x) { +inline bool is_na<>(const double& x) { return ISNA(x); } From 162b51cad7283c586fbcf6646a2ed6521c243fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Mon, 22 May 2023 15:17:38 +0200 Subject: [PATCH 09/54] not using references :shurg: --- inst/include/cpp11/R.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 75d9c3f6..bf81949d 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -48,12 +48,12 @@ template inline T na(); template -inline bool is_na(const T& value) { +inline bool is_na(T value) { return value == na(); } template <> -inline bool is_na<>(const double& x) { +inline bool is_na<>(double x) { return ISNA(x); } From e50563c7e79b46efbb35919d036a75d26b70b794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 10:30:40 +0200 Subject: [PATCH 10/54] sfinae for is_na so that implementations double and !double are separate --- inst/include/cpp11/R.hpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index bf81949d..d4116754 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -29,6 +29,7 @@ // clang-format on #include "cpp11/altrep.hpp" +#include namespace cpp11 { namespace literals { @@ -48,13 +49,13 @@ template inline T na(); template -inline bool is_na(T value) { +inline typename std::enable_if::value, bool>::type is_na(T value) { return value == na(); } -template <> -inline bool is_na<>(double x) { - return ISNA(x); +template +inline typename std::enable_if::value, bool>::type is_na(T value) { + return ISNA(value); } } // namespace cpp11 From 25b877ad7a5d666d1c8c43b35a54c0eb07561113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 10:32:44 +0200 Subject: [PATCH 11/54] making it easier to open both cpp11 and cpp11test in separate rstudio --- cpp11test/.Rbuildignore | 2 ++ cpp11test/cpp11test.Rproj | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 cpp11test/.Rbuildignore create mode 100644 cpp11test/cpp11test.Rproj diff --git a/cpp11test/.Rbuildignore b/cpp11test/.Rbuildignore new file mode 100644 index 00000000..91114bf2 --- /dev/null +++ b/cpp11test/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/cpp11test/cpp11test.Rproj b/cpp11test/cpp11test.Rproj new file mode 100644 index 00000000..497f8bfc --- /dev/null +++ b/cpp11test/cpp11test.Rproj @@ -0,0 +1,20 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source From cd441d800794b59cba517e07e60a4df62a3d4c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 10:56:31 +0200 Subject: [PATCH 12/54] make format --- Makefile | 4 ---- inst/include/cpp11/R.hpp | 8 +++++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 91261c66..d99155d2 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,4 @@ clean: clang_format=`which clang-format` format: $(shell find . -name '*.hpp') $(shell find . -name '*.cpp') -ifeq ($(findstring version 10,$(shell ${clang_format} --version 2>/dev/null)),) - @echo "clang-format 10 is required" -else @${clang_format} -i $? -endif diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index d4116754..583ba7e0 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -28,8 +28,8 @@ #endif // clang-format on -#include "cpp11/altrep.hpp" #include +#include "cpp11/altrep.hpp" namespace cpp11 { namespace literals { @@ -49,12 +49,14 @@ template inline T na(); template -inline typename std::enable_if::value, bool>::type is_na(T value) { +inline typename std::enable_if::value, bool>::type is_na( + T value) { return value == na(); } template -inline typename std::enable_if::value, bool>::type is_na(T value) { +inline typename std::enable_if::value, bool>::type is_na( + T value) { return ISNA(value); } From fd6e1e0bd0a626d21752cfc62c90322bfffbf525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 11:16:07 +0200 Subject: [PATCH 13/54] const& --- inst/include/cpp11/R.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 583ba7e0..45cf096b 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -49,14 +49,14 @@ template inline T na(); template -inline typename std::enable_if::value, bool>::type is_na( - T value) { +inline typename std::enable_if::type, double>::value, bool>::type is_na( + const T& value) { return value == na(); } template -inline typename std::enable_if::value, bool>::type is_na( - T value) { +inline typename std::enable_if::type, double>::value, bool>::type is_na( + const T& value) { return ISNA(value); } From 62d6bef59df8a27e9676e92263841aa70b00617f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 11:17:07 +0200 Subject: [PATCH 14/54] Revert "const&" This reverts commit fd6e1e0bd0a626d21752cfc62c90322bfffbf525. --- inst/include/cpp11/R.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 45cf096b..583ba7e0 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -49,14 +49,14 @@ template inline T na(); template -inline typename std::enable_if::type, double>::value, bool>::type is_na( - const T& value) { +inline typename std::enable_if::value, bool>::type is_na( + T value) { return value == na(); } template -inline typename std::enable_if::type, double>::value, bool>::type is_na( - const T& value) { +inline typename std::enable_if::value, bool>::type is_na( + T value) { return ISNA(value); } From f6743fe5b12dc8a0aae81ca00c35d39ba93812e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 11:18:18 +0200 Subject: [PATCH 15/54] const T& --- inst/include/cpp11/R.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 583ba7e0..4c2c4fa3 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -49,14 +49,16 @@ template inline T na(); template -inline typename std::enable_if::value, bool>::type is_na( - T value) { +inline typename std::enable_if::type, double>::value, + bool>::type +is_na(const T& value) { return value == na(); } template -inline typename std::enable_if::value, bool>::type is_na( - T value) { +inline typename std::enable_if::type, double>::value, + bool>::type +is_na(const T& value) { return ISNA(value); } From e96ebb071f4efc47fc692ec142a255adf53ddb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:08:28 +0200 Subject: [PATCH 16/54] try a dispatch based approach --- cpp11test/R/cpp11.R | 104 +++++++++--------- cpp11test/src/cpp11.cpp | 194 ++++++++++++++++----------------- cpp11test/src/test-doubles.cpp | 3 + inst/include/cpp11/R.hpp | 16 +-- 4 files changed, 161 insertions(+), 156 deletions(-) diff --git a/cpp11test/R/cpp11.R b/cpp11test/R/cpp11.R index 3d6b0f5d..4b9a1d9a 100644 --- a/cpp11test/R/cpp11.R +++ b/cpp11test/R/cpp11.R @@ -8,30 +8,6 @@ data_frame_ <- function() { .Call(`_cpp11test_data_frame_`) } -my_stop <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) -} - -my_stop_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_stop_n1`, mystring)) -} - -my_warning <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_warning`, mystring, myarg)) -} - -my_warning_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_warning_n1`, mystring)) -} - -my_message <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_message`, mystring, myarg)) -} - -my_message_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_message_n1`, mystring)) -} - my_stop_n1fmt <- function(mystring) { invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) } @@ -56,6 +32,30 @@ my_message_n2fmt <- function(mystring, myarg) { invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) } +my_stop <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) +} + +my_stop_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_stop_n1`, mystring)) +} + +my_warning <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_warning`, mystring, myarg)) +} + +my_warning_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_warning_n1`, mystring)) +} + +my_message <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_message`, mystring, myarg)) +} + +my_message_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_message_n1`, mystring)) +} + remove_altrep <- function(x) { .Call(`_cpp11test_remove_altrep`, x) } @@ -160,6 +160,34 @@ cpp11_safe_ <- function(x_sxp) { .Call(`_cpp11test_cpp11_safe_`, x_sxp) } +sum_dbl_for_ <- function(x) { + .Call(`_cpp11test_sum_dbl_for_`, x) +} + +sum_dbl_for2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) +} + +sum_dbl_for3_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) +} + +sum_dbl_foreach_ <- function(x) { + .Call(`_cpp11test_sum_dbl_foreach_`, x) +} + +sum_dbl_foreach2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) +} + +sum_dbl_accumulate_ <- function(x) { + .Call(`_cpp11test_sum_dbl_accumulate_`, x) +} + +sum_dbl_accumulate2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) +} + sum_int_for_ <- function(x) { .Call(`_cpp11test_sum_int_for_`, x) } @@ -195,31 +223,3 @@ rcpp_sum_dbl_accumulate_ <- function(x_sxp) { rcpp_grow_ <- function(n_sxp) { .Call(`_cpp11test_rcpp_grow_`, n_sxp) } - -sum_dbl_for_ <- function(x) { - .Call(`_cpp11test_sum_dbl_for_`, x) -} - -sum_dbl_for2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) -} - -sum_dbl_for3_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) -} - -sum_dbl_foreach_ <- function(x) { - .Call(`_cpp11test_sum_dbl_foreach_`, x) -} - -sum_dbl_foreach2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) -} - -sum_dbl_accumulate_ <- function(x) { - .Call(`_cpp11test_sum_dbl_accumulate_`, x) -} - -sum_dbl_accumulate2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) -} diff --git a/cpp11test/src/cpp11.cpp b/cpp11test/src/cpp11.cpp index d38863ee..44472d85 100644 --- a/cpp11test/src/cpp11.cpp +++ b/cpp11test/src/cpp11.cpp @@ -21,54 +21,6 @@ extern "C" SEXP _cpp11test_data_frame_() { return cpp11::as_sexp(data_frame_()); END_CPP11 } -// errors_fmt.cpp -void my_stop(std::string mystring, int myarg); -extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_stop(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_stop_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_stop_n1(SEXP mystring) { - BEGIN_CPP11 - my_stop_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_warning(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_warning(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_warning(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_warning_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_warning_n1(SEXP mystring) { - BEGIN_CPP11 - my_warning_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_message(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_message(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_message(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_message_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { - BEGIN_CPP11 - my_message_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} // errors.cpp void my_stop_n1fmt(std::string mystring); extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { @@ -117,6 +69,54 @@ extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { return R_NilValue; END_CPP11 } +// errors_fmt.cpp +void my_stop(std::string mystring, int myarg); +extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_stop(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_stop_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_stop_n1(SEXP mystring) { + BEGIN_CPP11 + my_stop_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_warning(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_warning(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_warning(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_warning_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_warning_n1(SEXP mystring) { + BEGIN_CPP11 + my_warning_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_message(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_message(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_message(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_message_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { + BEGIN_CPP11 + my_message_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} // find-intervals.cpp SEXP remove_altrep(SEXP x); extern "C" SEXP _cpp11test_remove_altrep(SEXP x) { @@ -310,6 +310,55 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) { return cpp11::as_sexp(cpp11_safe_(cpp11::as_cpp>(x_sxp))); END_CPP11 } +// sum.cpp +double sum_dbl_for_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for3_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} // sum_int.cpp double sum_int_for_(cpp11::integers x); extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) { @@ -373,55 +422,6 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) { return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp>(n_sxp))); END_CPP11 } -// sum.cpp -double sum_dbl_for_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for3_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} extern "C" { /* .Call calls */ diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 2d847405..65324e6f 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -373,6 +373,9 @@ context("doubles-C++") { double y = NA_REAL; expect_true(cpp11::is_na(y)); + + double z = NA_INTEGER; + expect_true(!cpp11::is_na(z)); } test_that("as_doubles(integers)") { diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index 4c2c4fa3..c09abb64 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -49,17 +49,19 @@ template inline T na(); template -inline typename std::enable_if::type, double>::value, - bool>::type -is_na(const T& value) { +inline bool is_na_impl(T value, std::true_type) { + return ISNA(value); +} + +template +inline bool is_na_impl(T value, std::false_type) { return value == na(); } template -inline typename std::enable_if::type, double>::value, - bool>::type -is_na(const T& value) { - return ISNA(value); +inline bool is_na(T value) { + return is_na_impl(value, + typename std::is_same::type, double>::type()); } } // namespace cpp11 From 2db642339076b4492cb75791999f07fb249b4a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:17:09 +0200 Subject: [PATCH 17/54] Revert "try a dispatch based approach" This reverts commit e96ebb071f4efc47fc692ec142a255adf53ddb81. --- cpp11test/R/cpp11.R | 104 +++++++++--------- cpp11test/src/cpp11.cpp | 194 ++++++++++++++++----------------- cpp11test/src/test-doubles.cpp | 3 - inst/include/cpp11/R.hpp | 16 ++- 4 files changed, 156 insertions(+), 161 deletions(-) diff --git a/cpp11test/R/cpp11.R b/cpp11test/R/cpp11.R index 4b9a1d9a..3d6b0f5d 100644 --- a/cpp11test/R/cpp11.R +++ b/cpp11test/R/cpp11.R @@ -8,30 +8,6 @@ data_frame_ <- function() { .Call(`_cpp11test_data_frame_`) } -my_stop_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) -} - -my_stop_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_stop_n2fmt`, mystring, myarg)) -} - -my_warning_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_warning_n1fmt`, mystring)) -} - -my_warning_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_warning_n2fmt`, mystring, myarg)) -} - -my_message_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_message_n1fmt`, mystring)) -} - -my_message_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) -} - my_stop <- function(mystring, myarg) { invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) } @@ -56,6 +32,30 @@ my_message_n1 <- function(mystring) { invisible(.Call(`_cpp11test_my_message_n1`, mystring)) } +my_stop_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) +} + +my_stop_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_stop_n2fmt`, mystring, myarg)) +} + +my_warning_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_warning_n1fmt`, mystring)) +} + +my_warning_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_warning_n2fmt`, mystring, myarg)) +} + +my_message_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_message_n1fmt`, mystring)) +} + +my_message_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) +} + remove_altrep <- function(x) { .Call(`_cpp11test_remove_altrep`, x) } @@ -160,34 +160,6 @@ cpp11_safe_ <- function(x_sxp) { .Call(`_cpp11test_cpp11_safe_`, x_sxp) } -sum_dbl_for_ <- function(x) { - .Call(`_cpp11test_sum_dbl_for_`, x) -} - -sum_dbl_for2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) -} - -sum_dbl_for3_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) -} - -sum_dbl_foreach_ <- function(x) { - .Call(`_cpp11test_sum_dbl_foreach_`, x) -} - -sum_dbl_foreach2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) -} - -sum_dbl_accumulate_ <- function(x) { - .Call(`_cpp11test_sum_dbl_accumulate_`, x) -} - -sum_dbl_accumulate2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) -} - sum_int_for_ <- function(x) { .Call(`_cpp11test_sum_int_for_`, x) } @@ -223,3 +195,31 @@ rcpp_sum_dbl_accumulate_ <- function(x_sxp) { rcpp_grow_ <- function(n_sxp) { .Call(`_cpp11test_rcpp_grow_`, n_sxp) } + +sum_dbl_for_ <- function(x) { + .Call(`_cpp11test_sum_dbl_for_`, x) +} + +sum_dbl_for2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) +} + +sum_dbl_for3_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) +} + +sum_dbl_foreach_ <- function(x) { + .Call(`_cpp11test_sum_dbl_foreach_`, x) +} + +sum_dbl_foreach2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) +} + +sum_dbl_accumulate_ <- function(x) { + .Call(`_cpp11test_sum_dbl_accumulate_`, x) +} + +sum_dbl_accumulate2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) +} diff --git a/cpp11test/src/cpp11.cpp b/cpp11test/src/cpp11.cpp index 44472d85..d38863ee 100644 --- a/cpp11test/src/cpp11.cpp +++ b/cpp11test/src/cpp11.cpp @@ -21,54 +21,6 @@ extern "C" SEXP _cpp11test_data_frame_() { return cpp11::as_sexp(data_frame_()); END_CPP11 } -// errors.cpp -void my_stop_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_stop_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_stop_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_stop_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_stop_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_warning_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_warning_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_warning_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_warning_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_warning_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_warning_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_message_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_message_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_message_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_message_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_message_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} // errors_fmt.cpp void my_stop(std::string mystring, int myarg); extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { @@ -117,6 +69,54 @@ extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { return R_NilValue; END_CPP11 } +// errors.cpp +void my_stop_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_stop_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_stop_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_stop_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_stop_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_warning_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_warning_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_warning_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_warning_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_warning_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_warning_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_message_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_message_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_message_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_message_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_message_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} // find-intervals.cpp SEXP remove_altrep(SEXP x); extern "C" SEXP _cpp11test_remove_altrep(SEXP x) { @@ -310,55 +310,6 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) { return cpp11::as_sexp(cpp11_safe_(cpp11::as_cpp>(x_sxp))); END_CPP11 } -// sum.cpp -double sum_dbl_for_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for3_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} // sum_int.cpp double sum_int_for_(cpp11::integers x); extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) { @@ -422,6 +373,55 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) { return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp>(n_sxp))); END_CPP11 } +// sum.cpp +double sum_dbl_for_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for3_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} extern "C" { /* .Call calls */ diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 65324e6f..2d847405 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -373,9 +373,6 @@ context("doubles-C++") { double y = NA_REAL; expect_true(cpp11::is_na(y)); - - double z = NA_INTEGER; - expect_true(!cpp11::is_na(z)); } test_that("as_doubles(integers)") { diff --git a/inst/include/cpp11/R.hpp b/inst/include/cpp11/R.hpp index c09abb64..4c2c4fa3 100644 --- a/inst/include/cpp11/R.hpp +++ b/inst/include/cpp11/R.hpp @@ -49,19 +49,17 @@ template inline T na(); template -inline bool is_na_impl(T value, std::true_type) { - return ISNA(value); -} - -template -inline bool is_na_impl(T value, std::false_type) { +inline typename std::enable_if::type, double>::value, + bool>::type +is_na(const T& value) { return value == na(); } template -inline bool is_na(T value) { - return is_na_impl(value, - typename std::is_same::type, double>::type()); +inline typename std::enable_if::type, double>::value, + bool>::type +is_na(const T& value) { + return ISNA(value); } } // namespace cpp11 From 91f41c7102805f629be22c193f86cdef7029b4a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:18:17 +0200 Subject: [PATCH 18/54] use explciit is_na<> specializations --- inst/include/cpp11/doubles.hpp | 2 +- inst/include/cpp11/integers.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 08cd761b..ea3b2222 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -158,7 +158,7 @@ inline doubles as_doubles(sexp x) { writable::doubles ret(len); for (R_xlen_t i = 0; i < len; ++i) { int el = xn[i]; - if (is_na(el)) { + if (is_na(el)) { ret[i] = na(); } else { ret[i] = static_cast(el); diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 9fa99d33..3b506036 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -160,7 +160,7 @@ inline integers as_integers(sexp x) { writable::integers ret(len); for (R_xlen_t i = 0; i < len; ++i) { double el = xn[i]; - if (is_na(el)) { + if (is_na(el)) { ret[i] = na(); } else if (is_convertible_without_loss_to_integer(el)) { ret[i] = static_cast(el); From 52000a050b2afedc90afd232711a5f3fc951a941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:29:59 +0200 Subject: [PATCH 19/54] specify is_na on tests --- cpp11test/R/cpp11.R | 104 ++++++++--------- cpp11test/src/cpp11.cpp | 194 ++++++++++++++++---------------- cpp11test/src/test-doubles.cpp | 2 +- cpp11test/src/test-integers.cpp | 2 +- 4 files changed, 151 insertions(+), 151 deletions(-) diff --git a/cpp11test/R/cpp11.R b/cpp11test/R/cpp11.R index 3d6b0f5d..4b9a1d9a 100644 --- a/cpp11test/R/cpp11.R +++ b/cpp11test/R/cpp11.R @@ -8,30 +8,6 @@ data_frame_ <- function() { .Call(`_cpp11test_data_frame_`) } -my_stop <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) -} - -my_stop_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_stop_n1`, mystring)) -} - -my_warning <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_warning`, mystring, myarg)) -} - -my_warning_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_warning_n1`, mystring)) -} - -my_message <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_message`, mystring, myarg)) -} - -my_message_n1 <- function(mystring) { - invisible(.Call(`_cpp11test_my_message_n1`, mystring)) -} - my_stop_n1fmt <- function(mystring) { invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) } @@ -56,6 +32,30 @@ my_message_n2fmt <- function(mystring, myarg) { invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) } +my_stop <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) +} + +my_stop_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_stop_n1`, mystring)) +} + +my_warning <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_warning`, mystring, myarg)) +} + +my_warning_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_warning_n1`, mystring)) +} + +my_message <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_message`, mystring, myarg)) +} + +my_message_n1 <- function(mystring) { + invisible(.Call(`_cpp11test_my_message_n1`, mystring)) +} + remove_altrep <- function(x) { .Call(`_cpp11test_remove_altrep`, x) } @@ -160,6 +160,34 @@ cpp11_safe_ <- function(x_sxp) { .Call(`_cpp11test_cpp11_safe_`, x_sxp) } +sum_dbl_for_ <- function(x) { + .Call(`_cpp11test_sum_dbl_for_`, x) +} + +sum_dbl_for2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) +} + +sum_dbl_for3_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) +} + +sum_dbl_foreach_ <- function(x) { + .Call(`_cpp11test_sum_dbl_foreach_`, x) +} + +sum_dbl_foreach2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) +} + +sum_dbl_accumulate_ <- function(x) { + .Call(`_cpp11test_sum_dbl_accumulate_`, x) +} + +sum_dbl_accumulate2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) +} + sum_int_for_ <- function(x) { .Call(`_cpp11test_sum_int_for_`, x) } @@ -195,31 +223,3 @@ rcpp_sum_dbl_accumulate_ <- function(x_sxp) { rcpp_grow_ <- function(n_sxp) { .Call(`_cpp11test_rcpp_grow_`, n_sxp) } - -sum_dbl_for_ <- function(x) { - .Call(`_cpp11test_sum_dbl_for_`, x) -} - -sum_dbl_for2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) -} - -sum_dbl_for3_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) -} - -sum_dbl_foreach_ <- function(x) { - .Call(`_cpp11test_sum_dbl_foreach_`, x) -} - -sum_dbl_foreach2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) -} - -sum_dbl_accumulate_ <- function(x) { - .Call(`_cpp11test_sum_dbl_accumulate_`, x) -} - -sum_dbl_accumulate2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) -} diff --git a/cpp11test/src/cpp11.cpp b/cpp11test/src/cpp11.cpp index d38863ee..44472d85 100644 --- a/cpp11test/src/cpp11.cpp +++ b/cpp11test/src/cpp11.cpp @@ -21,54 +21,6 @@ extern "C" SEXP _cpp11test_data_frame_() { return cpp11::as_sexp(data_frame_()); END_CPP11 } -// errors_fmt.cpp -void my_stop(std::string mystring, int myarg); -extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_stop(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_stop_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_stop_n1(SEXP mystring) { - BEGIN_CPP11 - my_stop_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_warning(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_warning(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_warning(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_warning_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_warning_n1(SEXP mystring) { - BEGIN_CPP11 - my_warning_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_message(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_message(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_message(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors_fmt.cpp -void my_message_n1(std::string mystring); -extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { - BEGIN_CPP11 - my_message_n1(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} // errors.cpp void my_stop_n1fmt(std::string mystring); extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { @@ -117,6 +69,54 @@ extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { return R_NilValue; END_CPP11 } +// errors_fmt.cpp +void my_stop(std::string mystring, int myarg); +extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_stop(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_stop_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_stop_n1(SEXP mystring) { + BEGIN_CPP11 + my_stop_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_warning(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_warning(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_warning(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_warning_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_warning_n1(SEXP mystring) { + BEGIN_CPP11 + my_warning_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_message(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_message(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_message(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors_fmt.cpp +void my_message_n1(std::string mystring); +extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { + BEGIN_CPP11 + my_message_n1(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} // find-intervals.cpp SEXP remove_altrep(SEXP x); extern "C" SEXP _cpp11test_remove_altrep(SEXP x) { @@ -310,6 +310,55 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) { return cpp11::as_sexp(cpp11_safe_(cpp11::as_cpp>(x_sxp))); END_CPP11 } +// sum.cpp +double sum_dbl_for_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for3_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} // sum_int.cpp double sum_int_for_(cpp11::integers x); extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) { @@ -373,55 +422,6 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) { return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp>(n_sxp))); END_CPP11 } -// sum.cpp -double sum_dbl_for_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for3_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} extern "C" { /* .Call calls */ diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 2d847405..173533cb 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -397,7 +397,7 @@ context("doubles-C++") { cpp11::writable::integers na; na.push_back(cpp11::na()); cpp11::doubles na2(cpp11::as_doubles(na)); - expect_true(cpp11::is_na(na2[0])); + expect_true(cpp11::is_na(na2[0])); } test_that("doubles operator[] and at") { diff --git a/cpp11test/src/test-integers.cpp b/cpp11test/src/test-integers.cpp index f2e6c0f7..99feef73 100644 --- a/cpp11test/src/test-integers.cpp +++ b/cpp11test/src/test-integers.cpp @@ -42,7 +42,7 @@ context("integers-C++") { cpp11::writable::doubles na; na.push_back(cpp11::na()); cpp11::integers na2(cpp11::as_integers(na)); - expect_true(cpp11::is_na(na2[0])); + expect_true(cpp11::is_na(na2[0])); } test_that("integers.push_back()") { From cd873a213ea7a966dc6fc80887d381b7c0a00f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:47:22 +0200 Subject: [PATCH 20/54] + expect_true(ISNA(na2[0])) --- cpp11test/src/test-doubles.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 173533cb..440f24c6 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -397,6 +397,7 @@ context("doubles-C++") { cpp11::writable::integers na; na.push_back(cpp11::na()); cpp11::doubles na2(cpp11::as_doubles(na)); + expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); } From 55e63d9584e56166827bc8213ccc227832b20ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 12:58:36 +0200 Subject: [PATCH 21/54] na.push_back(NA_INTEGER); --- cpp11test/src/test-doubles.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 440f24c6..fde74d5c 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -395,7 +395,8 @@ context("doubles-C++") { expect_error(cpp11::as_doubles(e)); cpp11::writable::integers na; - na.push_back(cpp11::na()); + // na.push_back(cpp11::na()); + na.push_back(NA_INTEGER); cpp11::doubles na2(cpp11::as_doubles(na)); expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); From 36e040758c647a992981fead6f804618a604a65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 13:21:37 +0200 Subject: [PATCH 22/54] test na --- cpp11test/src/test-doubles.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index fde74d5c..6ab25103 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -395,8 +395,10 @@ context("doubles-C++") { expect_error(cpp11::as_doubles(e)); cpp11::writable::integers na; - // na.push_back(cpp11::na()); - na.push_back(NA_INTEGER); + na.push_back(cpp11::na()); + expect_true(cpp11::is_na(na[0])); + expect_true(cpp11::is_na(na[0])); + cpp11::doubles na2(cpp11::as_doubles(na)); expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); From 5067d9b4f9e6864823fa7895d61be7337f85c742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 13:36:05 +0200 Subject: [PATCH 23/54] cast proxy --- cpp11test/src/test-doubles.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 6ab25103..e2aa08b5 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -396,8 +396,7 @@ context("doubles-C++") { cpp11::writable::integers na; na.push_back(cpp11::na()); - expect_true(cpp11::is_na(na[0])); - expect_true(cpp11::is_na(na[0])); + expect_true(cpp11::is_na((int)na[0])); cpp11::doubles na2(cpp11::as_doubles(na)); expect_true(ISNA(na2[0])); From 8a841700c9c265b2c389ee1e36615fae88cccf33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 13:51:47 +0200 Subject: [PATCH 24/54] na{NA_INBTEGER} --- cpp11test/src/test-doubles.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index e2aa08b5..7a29ed57 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -394,9 +394,7 @@ context("doubles-C++") { e.push_back("b"); expect_error(cpp11::as_doubles(e)); - cpp11::writable::integers na; - na.push_back(cpp11::na()); - expect_true(cpp11::is_na((int)na[0])); + cpp11::writable::integers na{NA_INTEGER}; cpp11::doubles na2(cpp11::as_doubles(na)); expect_true(ISNA(na2[0])); From 78ca04367d2cb87daf3e5ee2c955046c4af9aa09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 13:54:01 +0200 Subject: [PATCH 25/54] use NA_REAL and NA_INTEGER in as_doubles() --- inst/include/cpp11/doubles.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index ea3b2222..24e0a5e3 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -158,8 +158,8 @@ inline doubles as_doubles(sexp x) { writable::doubles ret(len); for (R_xlen_t i = 0; i < len; ++i) { int el = xn[i]; - if (is_na(el)) { - ret[i] = na(); + if (el == NA_INTEGER) { + ret[i] = NA_REAL; } else { ret[i] = static_cast(el); } From 09c199c042d30ebf13891200f623eaca4348dc1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 14:07:09 +0200 Subject: [PATCH 26/54] na2 = --- cpp11test/src/test-doubles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 7a29ed57..63be2de6 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -396,7 +396,7 @@ context("doubles-C++") { cpp11::writable::integers na{NA_INTEGER}; - cpp11::doubles na2(cpp11::as_doubles(na)); + cpp11::doubles na2 = cpp11::as_doubles(na); expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); } From a55592e6177f523e39dfdfa2f8992179d5867d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 14:26:49 +0200 Subject: [PATCH 27/54] as_cpp(ret) --- inst/include/cpp11/doubles.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 24e0a5e3..3619580d 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -164,7 +164,7 @@ inline doubles as_doubles(sexp x) { ret[i] = static_cast(el); } } - return ret; + return as_cpp(ret); } throw type_error(REALSXP, TYPEOF(x)); From 4e16ae0e44d0b7d26079e4fc763728aad181c324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 14:48:38 +0200 Subject: [PATCH 28/54] r_vector(r_vector&& rhs) --- cpp11test/src/test-doubles.cpp | 2 +- inst/include/cpp11/doubles.hpp | 2 +- inst/include/cpp11/r_vector.hpp | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 63be2de6..7a29ed57 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -396,7 +396,7 @@ context("doubles-C++") { cpp11::writable::integers na{NA_INTEGER}; - cpp11::doubles na2 = cpp11::as_doubles(na); + cpp11::doubles na2(cpp11::as_doubles(na)); expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); } diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 3619580d..24e0a5e3 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -164,7 +164,7 @@ inline doubles as_doubles(sexp x) { ret[i] = static_cast(el); } } - return as_cpp(ret); + return ret; } throw type_error(REALSXP, TYPEOF(x)); diff --git a/inst/include/cpp11/r_vector.hpp b/inst/include/cpp11/r_vector.hpp index 4831c2f5..d0af40ef 100644 --- a/inst/include/cpp11/r_vector.hpp +++ b/inst/include/cpp11/r_vector.hpp @@ -104,6 +104,17 @@ class r_vector { preserved.release(old_protect); }; + r_vector(r_vector&& rhs) { + protect_ = rhs.protect_; + data_ = rhs.data_; + is_altrep_ = rhs.is_altrep_; + data_p_ = rhs.data_p_; + length_ = rhs.length_; + + // so that the destructor for rhs is no-op + rhs.protect_ = R_NilValue; + } + r_vector(const writable::r_vector& rhs) : r_vector(static_cast(rhs)) {} r_vector(named_arg) = delete; From a316393afd99be6bcdbe5512abd15d9afe060294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 15:13:24 +0200 Subject: [PATCH 29/54] sexp& operator=(sexp&&) --- Makefile | 1 + inst/include/cpp11/sexp.hpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/Makefile b/Makefile index d99155d2..4b85c9f1 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ test: all clean: @Rscript -e 'devtools::clean_dll()' + @Rscript -e 'devtools::clean_dll("cpp11test")' clang_format=`which clang-format` format: $(shell find . -name '*.hpp') $(shell find . -name '*.cpp') diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index 1517c5af..cb538e1d 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -49,6 +49,17 @@ class sexp { return *this; } + sexp& operator=(sexp&& rhs) { + data_ = rhs.data_; + preserve_token_ = rhs.preserve_token_; + + rhs.data_ = R_NilValue; + rhs.preserve_token_ = R_NilValue; + + // REprintf("moved %x : %i\n", rhs.data_, protect_head_size()); + return *this; + } + // void swap(sexp& rhs) { // sexp tmp(rhs); // rhs = *this; From 6abb94a7e777de2bf5968be537819e5aff486f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 15:35:32 +0200 Subject: [PATCH 30/54] sexp& operator=(sexp&& rhs) --- inst/include/cpp11/sexp.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index cb538e1d..6cf0204b 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -50,6 +50,8 @@ class sexp { } sexp& operator=(sexp&& rhs) { + preserved.release(preserve_token_); + data_ = rhs.data_; preserve_token_ = rhs.preserve_token_; From a2b05655e05082e89856b2f953667c2a9aeef4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 15:39:08 +0200 Subject: [PATCH 31/54] avoid hoops --- cpp11test/src/test-doubles.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 7a29ed57..0226e6f3 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -394,9 +394,12 @@ context("doubles-C++") { e.push_back("b"); expect_error(cpp11::as_doubles(e)); + /* cpp11::writable::integers na{NA_INTEGER}; - cpp11::doubles na2(cpp11::as_doubles(na)); + */ + cpp11::doubles na2(Rf_ScalarReal(NA_REAL)); + expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); } From ff2093099b13815093068346a382995c2ca9fcac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 19:48:43 +0200 Subject: [PATCH 32/54] both versions --- cpp11test/src/test-doubles.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 0226e6f3..cb445876 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -394,14 +394,14 @@ context("doubles-C++") { e.push_back("b"); expect_error(cpp11::as_doubles(e)); - /* - cpp11::writable::integers na{NA_INTEGER}; - cpp11::doubles na2(cpp11::as_doubles(na)); - */ cpp11::doubles na2(Rf_ScalarReal(NA_REAL)); - expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); + + cpp11::writable::integers na{NA_INTEGER}; + cpp11::doubles na3(cpp11::as_doubles(na)); + expect_true(ISNA(na3[0])); + expect_true(cpp11::is_na(na3[0])); } test_that("doubles operator[] and at") { From 99099674cda4c4ae177c512be315b8e374edd5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 20:30:21 +0200 Subject: [PATCH 33/54] is_na() --- cpp11test/src/test-doubles.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index cb445876..3d2bec4e 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -397,11 +397,13 @@ context("doubles-C++") { cpp11::doubles na2(Rf_ScalarReal(NA_REAL)); expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); + expect_true(cpp11::is_na(na2[0])); cpp11::writable::integers na{NA_INTEGER}; cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(ISNA(na3[0])); expect_true(cpp11::is_na(na3[0])); + expect_true(cpp11::is_na(na3[0])); } test_that("doubles operator[] and at") { From 3947b262641e69ec6dc92e1ea2bd43966ea70ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 21:00:39 +0200 Subject: [PATCH 34/54] cpp11::integers na4 = cpp11::as_cpp(na); --- cpp11test/src/test-doubles.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 3d2bec4e..b677a308 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -400,6 +400,9 @@ context("doubles-C++") { expect_true(cpp11::is_na(na2[0])); cpp11::writable::integers na{NA_INTEGER}; + cpp11::integers na4 = cpp11::as_cpp(na); + expect_true(cpp11::is_na(na4[0])); + cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(ISNA(na3[0])); expect_true(cpp11::is_na(na3[0])); From 9a9a2c26e018373a90a54a46ff7a8e89fd76bcb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 21:11:52 +0200 Subject: [PATCH 35/54] expect_true(na3.size() == 1); --- cpp11test/src/test-doubles.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index b677a308..d6a56cd5 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -401,9 +401,11 @@ context("doubles-C++") { cpp11::writable::integers na{NA_INTEGER}; cpp11::integers na4 = cpp11::as_cpp(na); + expect_true(na4.size() == 1); expect_true(cpp11::is_na(na4[0])); cpp11::doubles na3(cpp11::as_doubles(na)); + expect_true(na3.size() == 1); expect_true(ISNA(na3[0])); expect_true(cpp11::is_na(na3[0])); expect_true(cpp11::is_na(na3[0])); From cb25d63920b3937a96ca1b5219bdae6d87c5fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 21:47:36 +0200 Subject: [PATCH 36/54] expect_true(ISNA(na5[0])); --- cpp11test/src/test-doubles.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index d6a56cd5..23d147f4 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -401,8 +401,17 @@ context("doubles-C++") { cpp11::writable::integers na{NA_INTEGER}; cpp11::integers na4 = cpp11::as_cpp(na); - expect_true(na4.size() == 1); - expect_true(cpp11::is_na(na4[0])); + R_xlen_t len = na4.size(); + cpp11::writable::doubles na5(len); + for (R_xlen_t i = 0; i < len; ++i) { + int el = na4[i]; + if (el == NA_INTEGER) { + na5[i] = NA_REAL; + } else { + na5[i] = static_cast(el); + } + } + expect_true(ISNA(na5[0])); cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); From ba11c0d02cf5b799a676aec70e27723e7c4f784f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 21:57:20 +0200 Subject: [PATCH 37/54] expect_true(el == NA_INTEGER); --- cpp11test/src/test-doubles.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 23d147f4..5085af56 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -405,6 +405,7 @@ context("doubles-C++") { cpp11::writable::doubles na5(len); for (R_xlen_t i = 0; i < len; ++i) { int el = na4[i]; + expect_true(el == NA_INTEGER); if (el == NA_INTEGER) { na5[i] = NA_REAL; } else { From 2e7e8fe5796c621f5e42cf2cd182c1232e9e58e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Tue, 23 May 2023 22:00:19 +0200 Subject: [PATCH 38/54] expect_true(ISNA(na6[0])); --- cpp11test/src/test-doubles.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 5085af56..e39c1670 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -414,6 +414,9 @@ context("doubles-C++") { } expect_true(ISNA(na5[0])); + cpp11::doubles na6(na5); + expect_true(ISNA(na6[0])); + cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); expect_true(ISNA(na3[0])); From 94cf73188162f8cffadf954a551fa143fe76a7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 05:47:42 +0200 Subject: [PATCH 39/54] cpp11::sexp na(Rf_ScalarReal(NA_INTEGER)); --- cpp11test/src/test-doubles.cpp | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index e39c1670..2d6f5b88 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -399,24 +399,8 @@ context("doubles-C++") { expect_true(cpp11::is_na(na2[0])); expect_true(cpp11::is_na(na2[0])); - cpp11::writable::integers na{NA_INTEGER}; - cpp11::integers na4 = cpp11::as_cpp(na); - R_xlen_t len = na4.size(); - cpp11::writable::doubles na5(len); - for (R_xlen_t i = 0; i < len; ++i) { - int el = na4[i]; - expect_true(el == NA_INTEGER); - if (el == NA_INTEGER) { - na5[i] = NA_REAL; - } else { - na5[i] = static_cast(el); - } - } - expect_true(ISNA(na5[0])); - - cpp11::doubles na6(na5); - expect_true(ISNA(na6[0])); - + // cpp11::writable::integers na{NA_INTEGER}; + cpp11::sexp na(Rf_ScalarReal(NA_INTEGER)); cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); expect_true(ISNA(na3[0])); From 0d4b92ca6cbc5087e07d800d3d25474d2105d1d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 05:51:04 +0200 Subject: [PATCH 40/54] cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); --- cpp11test/src/test-doubles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 2d6f5b88..dda3ac9d 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -400,7 +400,7 @@ context("doubles-C++") { expect_true(cpp11::is_na(na2[0])); // cpp11::writable::integers na{NA_INTEGER}; - cpp11::sexp na(Rf_ScalarReal(NA_INTEGER)); + cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); cpp11::doubles na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); expect_true(ISNA(na3[0])); From 8c4714893ab5fdff671161296b8d8e323ac3e553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 06:01:55 +0200 Subject: [PATCH 41/54] ... --- cpp11test/src/test-doubles.cpp | 8 +++++--- inst/include/cpp11/doubles.hpp | 11 ++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index dda3ac9d..8c82b523 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -376,6 +376,7 @@ context("doubles-C++") { } test_that("as_doubles(integers)") { + /* cpp11::writable::integers y; y.push_back(10); y.push_back(13616); @@ -398,13 +399,14 @@ context("doubles-C++") { expect_true(ISNA(na2[0])); expect_true(cpp11::is_na(na2[0])); expect_true(cpp11::is_na(na2[0])); + */ // cpp11::writable::integers na{NA_INTEGER}; cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); - cpp11::doubles na3(cpp11::as_doubles(na)); + cpp11::integers na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); - expect_true(ISNA(na3[0])); - expect_true(cpp11::is_na(na3[0])); + // expect_true(ISNA(na3[0])); + // expect_true(cpp11::is_na(na3[0])); expect_true(cpp11::is_na(na3[0])); } diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 24e0a5e3..37292d52 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -149,11 +149,15 @@ int na(); template <> int r_vector::operator[](const R_xlen_t pos) const; -inline doubles as_doubles(sexp x) { - if (TYPEOF(x) == REALSXP) { +inline integers as_doubles(sexp x) { + /*if (TYPEOF(x) == REALSXP) { return as_cpp(x); - } else if (TYPEOF(x) == INTSXP) { + } else + */ + if (TYPEOF(x) == INTSXP) { integers xn = as_cpp(x); + return xn; + /* R_xlen_t len = xn.size(); writable::doubles ret(len); for (R_xlen_t i = 0; i < len; ++i) { @@ -165,6 +169,7 @@ inline doubles as_doubles(sexp x) { } } return ret; + */ } throw type_error(REALSXP, TYPEOF(x)); From b671935d24244e3aec3c4ab6b961aaca2f843454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 06:23:34 +0200 Subject: [PATCH 42/54] typeof --- cpp11test/src/test-doubles.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 8c82b523..592531eb 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -403,6 +403,8 @@ context("doubles-C++") { // cpp11::writable::integers na{NA_INTEGER}; cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); + expect_true(TYPEOF(na) == INTSXP); + cpp11::integers na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); // expect_true(ISNA(na3[0])); From dc9d6013561c43cea7ab913d9bc32ffae2b04639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 06:39:28 +0200 Subject: [PATCH 43/54] - --- cpp11test/src/test-doubles.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 592531eb..7417dfbe 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -403,7 +403,6 @@ context("doubles-C++") { // cpp11::writable::integers na{NA_INTEGER}; cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); - expect_true(TYPEOF(na) == INTSXP); cpp11::integers na3(cpp11::as_doubles(na)); expect_true(na3.size() == 1); From 5eade3e81645024d8867c3552e1accde2ba8b130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 06:44:28 +0200 Subject: [PATCH 44/54] order of preserve/release --- inst/include/cpp11/sexp.hpp | 39 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index 6cf0204b..37625558 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -23,17 +23,12 @@ class sexp { // REprintf("created %x %x : %i\n", data_, preserve_token_, protect_head_size()); } - sexp(const sexp& rhs) { - data_ = rhs.data_; - preserve_token_ = preserved.insert(data_); + sexp(const sexp& rhs) : data_(rhs.data_), preserve_token_(preserved.insert(rhs.data_)) { // REprintf("copied %x new protect %x : %i\n", rhs.data_, preserve_token_, // protect_head_size()); } - sexp(sexp&& rhs) { - data_ = rhs.data_; - preserve_token_ = rhs.preserve_token_; - + sexp(sexp&& rhs) : data_(rhs.data_), preserve_token_(rhs.preserve_token_) { rhs.data_ = R_NilValue; rhs.preserve_token_ = R_NilValue; @@ -41,22 +36,30 @@ class sexp { } sexp& operator=(const sexp& rhs) { - preserved.release(preserve_token_); + if (data_ != rhs.data_) { + SEXP old_preserve_token = preserve_token_; + + data_ = rhs.data_; + preserve_token_ = preserved.insert(data_); + + preserved.release(old_preserve_token); + } - data_ = rhs.data_; - preserve_token_ = preserved.insert(data_); // REprintf("assigned %x : %i\n", rhs.data_, protect_head_size()); return *this; } sexp& operator=(sexp&& rhs) { - preserved.release(preserve_token_); + if (data_ != rhs.data_) { + SEXP old_preserve_token = preserve_token_; - data_ = rhs.data_; - preserve_token_ = rhs.preserve_token_; + data_ = rhs.data_; + preserve_token_ = rhs.preserve_token_; - rhs.data_ = R_NilValue; - rhs.preserve_token_ = R_NilValue; + rhs.data_ = R_NilValue; + rhs.preserve_token_ = R_NilValue; + preserved.release(old_preserve_token); + } // REprintf("moved %x : %i\n", rhs.data_, protect_head_size()); return *this; @@ -68,7 +71,9 @@ class sexp { //*this = tmp; //} - ~sexp() { preserved.release(preserve_token_); } + ~sexp() { + preserved.release(preserve_token_); + } attribute_proxy attr(const char* name) const { return attribute_proxy(*this, name); @@ -87,9 +92,11 @@ class sexp { } operator SEXP() const { return data_; } + /* operator double() const { return REAL_ELT(data_, 0); } operator size_t() const { return REAL_ELT(data_, 0); } operator bool() const { return LOGICAL_ELT(data_, 0); } + */ SEXP data() const { return data_; } }; From 7abcceaea857907588c4c913978b766d0afc6914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 07:00:47 +0200 Subject: [PATCH 45/54] ... --- cpp11test/src/test-doubles.cpp | 29 ++--------------------------- inst/include/cpp11/doubles.hpp | 2 +- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 7417dfbe..be6fc06f 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -376,35 +376,10 @@ context("doubles-C++") { } test_that("as_doubles(integers)") { - /* - cpp11::writable::integers y; - y.push_back(10); - y.push_back(13616); - y.push_back(124); - y.push_back(899); - cpp11::doubles i(cpp11::as_doubles(y)); - - expect_true(i[0] == 10); - expect_true(i[1] == 13616); - expect_true(i[2] == 124); - expect_true(i[3] == 899); - expect_true(TYPEOF(i) == REALSXP); - - cpp11::writable::strings e; - e.push_back("a"); - e.push_back("b"); - expect_error(cpp11::as_doubles(e)); - - cpp11::doubles na2(Rf_ScalarReal(NA_REAL)); - expect_true(ISNA(na2[0])); - expect_true(cpp11::is_na(na2[0])); - expect_true(cpp11::is_na(na2[0])); - */ - // cpp11::writable::integers na{NA_INTEGER}; - cpp11::sexp na(Rf_ScalarInteger(NA_INTEGER)); + // cpp11::sexp na(); - cpp11::integers na3(cpp11::as_doubles(na)); + cpp11::integers na3(cpp11::as_doubles(Rf_ScalarInteger(NA_INTEGER))); expect_true(na3.size() == 1); // expect_true(ISNA(na3[0])); // expect_true(cpp11::is_na(na3[0])); diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 37292d52..3c1bb4bc 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -149,7 +149,7 @@ int na(); template <> int r_vector::operator[](const R_xlen_t pos) const; -inline integers as_doubles(sexp x) { +inline integers as_doubles(SEXP x) { /*if (TYPEOF(x) == REALSXP) { return as_cpp(x); } else From 7082f3691d5423fcd9a73672f4e31d273bfdb651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 08:16:55 +0200 Subject: [PATCH 46/54] cpp11::integers na3(Rf_ScalarInteger(NA_INTEGER)); --- cpp11test/src/test-doubles.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index be6fc06f..f7dd7acc 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -379,7 +379,7 @@ context("doubles-C++") { // cpp11::writable::integers na{NA_INTEGER}; // cpp11::sexp na(); - cpp11::integers na3(cpp11::as_doubles(Rf_ScalarInteger(NA_INTEGER))); + cpp11::integers na3(Rf_ScalarInteger(NA_INTEGER)); expect_true(na3.size() == 1); // expect_true(ISNA(na3[0])); // expect_true(cpp11::is_na(na3[0])); From c6a14abfdeedeae8875747051b37d3d9ee163002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 08:48:15 +0200 Subject: [PATCH 47/54] move as_doubles to test file ??? --- cpp11test/src/test-doubles.cpp | 32 +++++++++++++++++++++++++++++++- inst/include/cpp11/doubles.hpp | 26 -------------------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index f7dd7acc..6e09fd9e 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -7,6 +7,36 @@ #include +namespace cpp11 { + +inline integers as_doubles(SEXP x) { + /*if (TYPEOF(x) == REALSXP) { + return as_cpp(x); + } else + */ + if (TYPEOF(x) == INTSXP) { + integers xn = as_cpp(x); + return xn; + /* + R_xlen_t len = xn.size(); + writable::doubles ret(len); + for (R_xlen_t i = 0; i < len; ++i) { + int el = xn[i]; + if (el == NA_INTEGER) { + ret[i] = NA_REAL; + } else { + ret[i] = static_cast(el); + } + } + return ret; + */ + } + + throw type_error(REALSXP, TYPEOF(x)); +} + +} + context("doubles-C++") { test_that("doubles::r_vector(SEXP)") { cpp11::doubles x(Rf_allocVector(REALSXP, 2)); @@ -379,7 +409,7 @@ context("doubles-C++") { // cpp11::writable::integers na{NA_INTEGER}; // cpp11::sexp na(); - cpp11::integers na3(Rf_ScalarInteger(NA_INTEGER)); + cpp11::integers na3(cpp11::as_doubles(Rf_ScalarInteger(NA_INTEGER))); expect_true(na3.size() == 1); // expect_true(ISNA(na3[0])); // expect_true(cpp11::is_na(na3[0])); diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index 3c1bb4bc..f96112b5 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -149,30 +149,4 @@ int na(); template <> int r_vector::operator[](const R_xlen_t pos) const; -inline integers as_doubles(SEXP x) { - /*if (TYPEOF(x) == REALSXP) { - return as_cpp(x); - } else - */ - if (TYPEOF(x) == INTSXP) { - integers xn = as_cpp(x); - return xn; - /* - R_xlen_t len = xn.size(); - writable::doubles ret(len); - for (R_xlen_t i = 0; i < len; ++i) { - int el = xn[i]; - if (el == NA_INTEGER) { - ret[i] = NA_REAL; - } else { - ret[i] = static_cast(el); - } - } - return ret; - */ - } - - throw type_error(REALSXP, TYPEOF(x)); -} - } // namespace cpp11 From 1bb6e3efef67e34ba542fb811fab9dacbff10db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 09:12:55 +0200 Subject: [PATCH 48/54] +++ --- cpp11test/src/test-doubles.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 6e09fd9e..4d5c562c 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -9,27 +9,22 @@ namespace cpp11 { -inline integers as_doubles(SEXP x) { - /*if (TYPEOF(x) == REALSXP) { - return as_cpp(x); - } else - */ - if (TYPEOF(x) == INTSXP) { +inline doubles as_doubles(SEXP x) { + if (TYPEOF(x) == REALSXP) { + return as_cpp(x); + } else if (TYPEOF(x) == INTSXP) { integers xn = as_cpp(x); - return xn; - /* R_xlen_t len = xn.size(); writable::doubles ret(len); for (R_xlen_t i = 0; i < len; ++i) { - int el = xn[i]; - if (el == NA_INTEGER) { - ret[i] = NA_REAL; - } else { - ret[i] = static_cast(el); - } + int el = xn[i]; + if (el == NA_INTEGER) { + ret[i] = NA_REAL; + } else { + ret[i] = static_cast(el); + } } return ret; - */ } throw type_error(REALSXP, TYPEOF(x)); @@ -409,7 +404,7 @@ context("doubles-C++") { // cpp11::writable::integers na{NA_INTEGER}; // cpp11::sexp na(); - cpp11::integers na3(cpp11::as_doubles(Rf_ScalarInteger(NA_INTEGER))); + cpp11::doubles na3(cpp11::as_doubles(Rf_ScalarInteger(NA_INTEGER))); expect_true(na3.size() == 1); // expect_true(ISNA(na3[0])); // expect_true(cpp11::is_na(na3[0])); From 8b7f075c0cc1b29dda0900c64252ce0db951633c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 09:43:52 +0200 Subject: [PATCH 49/54] Revert "avoid need for R_NilValue checks in protect code (#285)" This reverts commit dae1b6aba0da12593f4485b39df33b1cbc919f43. --- inst/include/cpp11/protect.hpp | 56 ++++++++++++++++------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/inst/include/cpp11/protect.hpp b/inst/include/cpp11/protect.hpp index acab13d6..672bf177 100644 --- a/inst/include/cpp11/protect.hpp +++ b/inst/include/cpp11/protect.hpp @@ -328,18 +328,16 @@ static struct { static SEXP list_ = get_preserve_list(); - // Get references to head, tail of the precious list. - SEXP head = list_; - SEXP tail = CDR(list_); + // Add a new cell that points to the previous end. + SEXP cell = PROTECT(Rf_cons(list_, CDR(list_))); - // Add a new cell that points to the current head + tail. - SEXP cell = PROTECT(Rf_cons(head, tail)); SET_TAG(cell, obj); - // Update the head + tail to point at the newly-created cell, - // effectively inserting that cell between the current head + tail. - SETCDR(head, cell); - SETCAR(tail, cell); + SETCDR(list_, cell); + + if (CDR(cell) != R_NilValue) { + SETCAR(CDR(cell), cell); + } UNPROTECT(2); @@ -367,25 +365,29 @@ static struct { #endif } - void release(SEXP cell) { - if (cell == R_NilValue) { + void release(SEXP token) { + if (token == R_NilValue) { return; } #ifdef CPP11_USE_PRESERVE_OBJECT - R_ReleaseObject(cell); + R_ReleaseObject(token); return; #endif - // Get a reference to the cells before and after the token. - SEXP lhs = CAR(cell); - SEXP rhs = CDR(cell); + SEXP before = CAR(token); + + SEXP after = CDR(token); + + if (before == R_NilValue && after == R_NilValue) { + Rf_error("should never happen"); + } + + SETCDR(before, after); - // Remove the cell from the precious list -- effectively, we do this - // by updating the 'lhs' and 'rhs' references to point at each-other, - // effectively removing any references to the cell in the pairlist. - SETCDR(lhs, rhs); - SETCAR(rhs, lhs); + if (after != R_NilValue) { + SETCAR(after, before); + } } private: @@ -425,24 +427,18 @@ static struct { static SEXP get_preserve_list() { static SEXP preserve_list = R_NilValue; + if (TYPEOF(preserve_list) != LISTSXP) { preserve_list = get_preserve_xptr_addr(); if (TYPEOF(preserve_list) != LISTSXP) { - preserve_list = Rf_cons(R_NilValue, Rf_cons(R_NilValue, R_NilValue)); + preserve_list = Rf_cons(R_NilValue, R_NilValue); R_PreserveObject(preserve_list); set_preserve_xptr(preserve_list); } - - // NOTE: Because older versions of cpp11 (<= 0.4.2) initialized the - // precious_list with a single cell, we might need to detect and update - // an existing empty precious list so that we have a second cell following. - if (CDR(preserve_list) == R_NilValue) - SETCDR(preserve_list, Rf_cons(R_NilValue, R_NilValue)); } return preserve_list; } - -} preserved; - +} // namespace cpp11 +preserved; } // namespace cpp11 From b59982f665e57c94e288d88231420a644ee9ab19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 10:04:10 +0200 Subject: [PATCH 50/54] as_integers() --- inst/include/cpp11/integers.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 3b506036..0244c858 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -160,8 +160,8 @@ inline integers as_integers(sexp x) { writable::integers ret(len); for (R_xlen_t i = 0; i < len; ++i) { double el = xn[i]; - if (is_na(el)) { - ret[i] = na(); + if (ISNA(el)) { + ret[i] = NA_INTEGER; } else if (is_convertible_without_loss_to_integer(el)) { ret[i] = static_cast(el); } else { From b614853ed8cc07ac79ccb811555aa29a38a27d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 10:22:27 +0200 Subject: [PATCH 51/54] ... --- cpp11test/src/test-doubles.cpp | 25 ------------------------- cpp11test/src/test-integers.cpp | 9 +++++---- inst/include/cpp11/doubles.hpp | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/cpp11test/src/test-doubles.cpp b/cpp11test/src/test-doubles.cpp index 4d5c562c..95842c71 100644 --- a/cpp11test/src/test-doubles.cpp +++ b/cpp11test/src/test-doubles.cpp @@ -7,31 +7,6 @@ #include -namespace cpp11 { - -inline doubles as_doubles(SEXP x) { - if (TYPEOF(x) == REALSXP) { - return as_cpp(x); - } else if (TYPEOF(x) == INTSXP) { - integers xn = as_cpp(x); - R_xlen_t len = xn.size(); - writable::doubles ret(len); - for (R_xlen_t i = 0; i < len; ++i) { - int el = xn[i]; - if (el == NA_INTEGER) { - ret[i] = NA_REAL; - } else { - ret[i] = static_cast(el); - } - } - return ret; - } - - throw type_error(REALSXP, TYPEOF(x)); -} - -} - context("doubles-C++") { test_that("doubles::r_vector(SEXP)") { cpp11::doubles x(Rf_allocVector(REALSXP, 2)); diff --git a/cpp11test/src/test-integers.cpp b/cpp11test/src/test-integers.cpp index 99feef73..719f327a 100644 --- a/cpp11test/src/test-integers.cpp +++ b/cpp11test/src/test-integers.cpp @@ -39,10 +39,11 @@ context("integers-C++") { expect_true(t[3] == 100000); expect_true(TYPEOF(t) == INTSXP); - cpp11::writable::doubles na; - na.push_back(cpp11::na()); - cpp11::integers na2(cpp11::as_integers(na)); - expect_true(cpp11::is_na(na2[0])); + cpp11::doubles na3(Rf_ScalarReal(NA_REAL)); + expect_true(na3.size() == 1); + + cpp11::integers na2(cpp11::as_integers(na3)); + expect_true(cpp11::is_na(na2[0])); } test_that("integers.push_back()") { diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index f96112b5..d582ba28 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -149,4 +149,26 @@ int na(); template <> int r_vector::operator[](const R_xlen_t pos) const; +inline doubles as_doubles(SEXP x) { + if (TYPEOF(x) == REALSXP) { + return as_cpp(x); + } else if (TYPEOF(x) == INTSXP) { + integers xn = as_cpp(x); + R_xlen_t len = xn.size(); + writable::doubles ret(len); + for (R_xlen_t i = 0; i < len; ++i) { + int el = xn[i]; + if (el == NA_INTEGER) { + ret[i] = NA_REAL; + } else { + ret[i] = static_cast(el); + } + } + return ret; + } + + throw type_error(REALSXP, TYPEOF(x)); +} + + } // namespace cpp11 From b67c145db5c44e70a01afd20b2816297740a81ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 11:09:45 +0200 Subject: [PATCH 52/54] -as_cpp --- inst/include/cpp11/doubles.hpp | 4 ++-- inst/include/cpp11/integers.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index d582ba28..cef15835 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -151,9 +151,9 @@ int r_vector::operator[](const R_xlen_t pos) const; inline doubles as_doubles(SEXP x) { if (TYPEOF(x) == REALSXP) { - return as_cpp(x); + return doubles(x); } else if (TYPEOF(x) == INTSXP) { - integers xn = as_cpp(x); + integers xn(x); R_xlen_t len = xn.size(); writable::doubles ret(len); for (R_xlen_t i = 0; i < len; ++i) { diff --git a/inst/include/cpp11/integers.hpp b/inst/include/cpp11/integers.hpp index 0244c858..f6d80e7f 100644 --- a/inst/include/cpp11/integers.hpp +++ b/inst/include/cpp11/integers.hpp @@ -151,7 +151,7 @@ typedef r_vector doubles; template <> double r_vector::operator[](const R_xlen_t pos) const; -inline integers as_integers(sexp x) { +inline integers as_integers(SEXP x) { if (TYPEOF(x) == INTSXP) { return as_cpp(x); } else if (TYPEOF(x) == REALSXP) { From 137322645425bb9f1e401c20afecf937f1f9c737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 11:18:25 +0200 Subject: [PATCH 53/54] conversion ops back --- inst/include/cpp11/sexp.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/inst/include/cpp11/sexp.hpp b/inst/include/cpp11/sexp.hpp index 37625558..e94b11a3 100644 --- a/inst/include/cpp11/sexp.hpp +++ b/inst/include/cpp11/sexp.hpp @@ -92,11 +92,9 @@ class sexp { } operator SEXP() const { return data_; } - /* operator double() const { return REAL_ELT(data_, 0); } operator size_t() const { return REAL_ELT(data_, 0); } operator bool() const { return LOGICAL_ELT(data_, 0); } - */ SEXP data() const { return data_; } }; From 9b0a9542655795723b1636ebb7c89a78ada030b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Fran=C3=A7ois?= Date: Wed, 24 May 2023 11:42:01 +0200 Subject: [PATCH 54/54] SEXP as_doubles(SEXP x) --- inst/include/cpp11/doubles.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/cpp11/doubles.hpp b/inst/include/cpp11/doubles.hpp index cef15835..ba537f21 100644 --- a/inst/include/cpp11/doubles.hpp +++ b/inst/include/cpp11/doubles.hpp @@ -149,7 +149,7 @@ int na(); template <> int r_vector::operator[](const R_xlen_t pos) const; -inline doubles as_doubles(SEXP x) { +inline SEXP as_doubles(SEXP x) { if (TYPEOF(x) == REALSXP) { return doubles(x); } else if (TYPEOF(x) == INTSXP) {