From cd0f41aceb732f9c148a9a9ae6a5aa1f385fccdf Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Sun, 7 Jul 2024 20:33:11 +0800 Subject: [PATCH 1/5] use read-only variants of {STRING/VECTOR}_PTR --- ChangeLog | 8 +++++ inst/include/Rcpp/internal/SEXP_Iterator.h | 2 +- inst/include/Rcpp/r/compat.h | 37 ++++++++++++++++++++++ inst/include/Rcpp/vector/Subsetter.h | 4 +-- inst/include/RcppCommon.h | 1 + src/barrier.cpp | 13 +++++--- 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 inst/include/Rcpp/r/compat.h diff --git a/ChangeLog b/ChangeLog index 2b1261d9c..88d7cd29f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2024-07-07 Kevin Ushey + + * inst/include/Rcpp/internal/SEXP_Iterator.h: Avoid using VECTOR_PTR + * inst/include/Rcpp/vector/Subsetter.h: Avoid using STRING_PTR + * inst/include/RcppCommon.h: Include compatibility defines + * src/barrier.cpp: Avoid using {STRING/VECTOR}_PTR + * inst/include/Rcpp/r/compat.h: Include compatibility defines + 2024-06-22 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Roll micro version diff --git a/inst/include/Rcpp/internal/SEXP_Iterator.h b/inst/include/Rcpp/internal/SEXP_Iterator.h index 62e604d23..fbb8e2ad4 100644 --- a/inst/include/Rcpp/internal/SEXP_Iterator.h +++ b/inst/include/Rcpp/internal/SEXP_Iterator.h @@ -37,7 +37,7 @@ class SEXP_Iterator { SEXP_Iterator( ): ptr(){} ; SEXP_Iterator( const SEXP_Iterator& other) : ptr(other.ptr){} ; - SEXP_Iterator( const VECTOR& vec ) : ptr( get_vector_ptr(vec) ){} ; + SEXP_Iterator( const VECTOR& vec ) : ptr( RCPP_VECTOR_PTR(vec) ){} ; SEXP_Iterator& operator=(const SEXP_Iterator& other){ ptr = other.ptr ; return *this ;} diff --git a/inst/include/Rcpp/r/compat.h b/inst/include/Rcpp/r/compat.h new file mode 100644 index 000000000..de61383e4 --- /dev/null +++ b/inst/include/Rcpp/r/compat.h @@ -0,0 +1,37 @@ + +// +// compat.h: Rcpp R/C++ interface class library -- compatibility defines +// +// Copyright (C) 2024 Dirk Eddelbuettel, Kevin Ushey +// +// This file is part of Rcpp. +// +// Rcpp is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 2 of the License, or +// (at your option) any later version. +// +// Rcpp is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Rcpp. If not, see . + +#ifndef RCPP_R_COMPAT_H +#define RCPP_R_COMPAT_H + +#if defined(STRING_PTR_RO) +# define RCPP_STRING_PTR STRING_PTR_RO +#else +# define RCPP_STRING_PTR STRING_PTR +#endif + +#if defined(VECTOR_PTR_RO) +# define RCPP_VECTOR_PTR VECTOR_PTR_RO +#else +# define RCPP_VECTOR_PTR VECTOR_PTR +#endif + +#endif /* RCPP_R_COMPAT_H */ diff --git a/inst/include/Rcpp/vector/Subsetter.h b/inst/include/Rcpp/vector/Subsetter.h index 339d2000d..5210a5e54 100644 --- a/inst/include/Rcpp/vector/Subsetter.h +++ b/inst/include/Rcpp/vector/Subsetter.h @@ -174,8 +174,8 @@ class SubsetProxy { indices.reserve(rhs_n); SEXP names = Rf_getAttrib(lhs, R_NamesSymbol); if (Rf_isNull(names)) stop("names is null"); - SEXP* namesPtr = STRING_PTR(names); - SEXP* rhsPtr = STRING_PTR(rhs); + const SEXP* namesPtr = RCPP_STRING_PTR(names); + const SEXP* rhsPtr = RCPP_STRING_PTR(rhs); for (R_xlen_t i = 0; i < rhs_n; ++i) { SEXP* match = std::find(namesPtr, namesPtr + lhs_n, *(rhsPtr + i)); if (match == namesPtr + lhs_n) diff --git a/inst/include/RcppCommon.h b/inst/include/RcppCommon.h index a2760ec7a..5cbe895b5 100644 --- a/inst/include/RcppCommon.h +++ b/inst/include/RcppCommon.h @@ -28,6 +28,7 @@ // #define RCPP_DEBUG_MODULE_LEVEL 1 #include +#include /** * \brief Rcpp API diff --git a/src/barrier.cpp b/src/barrier.cpp index 374870ef2..9d0fdf7ed 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -22,11 +22,15 @@ #define COMPILING_RCPP #define USE_RINTERNALS + +#include #include + #include -#include "internal.h" -#include #include +#include + +#include "internal.h" // [[Rcpp::register]] SEXP get_string_elt(SEXP x, R_xlen_t i) { // #nocov start @@ -50,7 +54,7 @@ void char_set_string_elt(SEXP x, R_xlen_t i, const char* value) { // [[Rcpp::register]] SEXP* get_string_ptr(SEXP x) { - return STRING_PTR(x); + return RCPP_STRING_PTR(x); } // [[Rcpp::register]] @@ -65,7 +69,8 @@ void set_vector_elt(SEXP x, R_xlen_t i, SEXP value) { // [[Rcpp::register]] SEXP* get_vector_ptr(SEXP x) { - return VECTOR_PTR(x); // #nocov end + // TODO: should we deprecate this? + return const_cast(RCPP_VECTOR_PTR(x)); // #nocov end } // [[Rcpp::register]] From 1cb485b59a452b08e703f05df94e939c024bfb02 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Sun, 7 Jul 2024 20:38:26 +0800 Subject: [PATCH 2/5] another const_cast --- src/barrier.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/barrier.cpp b/src/barrier.cpp index 9d0fdf7ed..c4e417920 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -54,7 +54,8 @@ void char_set_string_elt(SEXP x, R_xlen_t i, const char* value) { // [[Rcpp::register]] SEXP* get_string_ptr(SEXP x) { - return RCPP_STRING_PTR(x); + // TODO: should we deprecate this? + return const_cast(RCPP_STRING_PTR(x)); } // [[Rcpp::register]] From edcf98f8db68bcb548934f104af1530ef881fc7a Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Sun, 7 Jul 2024 20:45:57 +0800 Subject: [PATCH 3/5] one more const --- inst/include/Rcpp/vector/Subsetter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/include/Rcpp/vector/Subsetter.h b/inst/include/Rcpp/vector/Subsetter.h index 5210a5e54..085179171 100644 --- a/inst/include/Rcpp/vector/Subsetter.h +++ b/inst/include/Rcpp/vector/Subsetter.h @@ -177,7 +177,7 @@ class SubsetProxy { const SEXP* namesPtr = RCPP_STRING_PTR(names); const SEXP* rhsPtr = RCPP_STRING_PTR(rhs); for (R_xlen_t i = 0; i < rhs_n; ++i) { - SEXP* match = std::find(namesPtr, namesPtr + lhs_n, *(rhsPtr + i)); + const SEXP* match = std::find(namesPtr, namesPtr + lhs_n, *(rhsPtr + i)); if (match == namesPtr + lhs_n) stop("not found"); indices.push_back(match - namesPtr); From df6171595ea44c0a9ecb20272ab51e994be6d0df Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 9 Jul 2024 10:48:15 +0800 Subject: [PATCH 4/5] fix version checks --- inst/include/Rcpp/r/compat.h | 6 ++++-- src/api.cpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/inst/include/Rcpp/r/compat.h b/inst/include/Rcpp/r/compat.h index de61383e4..9e73612e5 100644 --- a/inst/include/Rcpp/r/compat.h +++ b/inst/include/Rcpp/r/compat.h @@ -22,13 +22,15 @@ #ifndef RCPP_R_COMPAT_H #define RCPP_R_COMPAT_H -#if defined(STRING_PTR_RO) +#include + +#if R_VERSION >= R_Version(4, 4, 0) # define RCPP_STRING_PTR STRING_PTR_RO #else # define RCPP_STRING_PTR STRING_PTR #endif -#if defined(VECTOR_PTR_RO) +#if R_VERSION >= R_Version(4, 4, 0) # define RCPP_VECTOR_PTR VECTOR_PTR_RO #else # define RCPP_VECTOR_PTR VECTOR_PTR diff --git a/src/api.cpp b/src/api.cpp index 7d3d729ba..aaf29ef6d 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -112,7 +112,7 @@ namespace Rcpp { case BCODESXP: return "BCODESXP"; case EXTPTRSXP: return "EXTPTRSXP"; case WEAKREFSXP: return "WEAKREFSXP"; -#if R_Version >= R_Version(4,4,0) // replaces S4SXP in R 4.4.0 +#if R_VERSION >= R_Version(4,4,0) // replaces S4SXP in R 4.4.0 case OBJSXP: return Rf_isS4(x) ? "S4SXP" : "OBJSXP"; // cf src/main/inspect.c #else case S4SXP: return "S4SXP"; From 3fd386e102e537979a3bd613066fbf7b841959ac Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 9 Jul 2024 10:53:52 +0800 Subject: [PATCH 5/5] require newer R --- inst/include/Rcpp/r/compat.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/include/Rcpp/r/compat.h b/inst/include/Rcpp/r/compat.h index 9e73612e5..218f3d7bc 100644 --- a/inst/include/Rcpp/r/compat.h +++ b/inst/include/Rcpp/r/compat.h @@ -24,13 +24,13 @@ #include -#if R_VERSION >= R_Version(4, 4, 0) +#if R_VERSION >= R_Version(4, 4, 2) # define RCPP_STRING_PTR STRING_PTR_RO #else # define RCPP_STRING_PTR STRING_PTR #endif -#if R_VERSION >= R_Version(4, 4, 0) +#if R_VERSION >= R_Version(4, 4, 2) # define RCPP_VECTOR_PTR VECTOR_PTR_RO #else # define RCPP_VECTOR_PTR VECTOR_PTR