Skip to content

std::string_view support in Rcpp::wrap() #1356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2025-01-31 Lev Kandel <lmakhlis@google.com>

* inst/include/Rcpp/internal/wrap.h: Add support for std::string_view
* inst/include/Rcpp/traits/r_type_traits.h: Idem
* inst/include/Rcpp/traits/wrap_type_traits.h: Idem
* inst/include/RcppCommon.h: Include <string_view>
* inst/tinytest/cpp/wrap.cpp: Add unit test for wrap(std::string_view)
* inst/tinytest/test_wrap.R: Idem

2025-01-26 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Roll micro version and date
6 changes: 6 additions & 0 deletions inst/include/Rcpp/internal/wrap.h
Original file line number Diff line number Diff line change
@@ -66,6 +66,12 @@ namespace Rcpp {
return make_charsexp__impl__cstring(st.c_str());
}

#if __cplusplus >= 201703L
inline SEXP make_charsexp__impl__cstring(std::string_view st) {
return Rf_mkCharLen(st.data(), st.size());
}
#endif

template <typename T>
inline SEXP make_charsexp__impl(const T& s, Rcpp::traits::true_type) {
return make_charsexp__impl__wstring(s);
3 changes: 3 additions & 0 deletions inst/include/Rcpp/traits/r_type_traits.h
Original file line number Diff line number Diff line change
@@ -141,6 +141,9 @@ template<> struct r_type_traits<Rcomplex>{ typedef r_type_primitive_tag r_catego
template<> struct r_type_traits<bool>{ typedef r_type_primitive_tag r_category ; } ;
template<> struct r_type_traits<std::string>{ typedef r_type_string_tag r_category ; } ;
template<> struct r_type_traits<std::wstring>{ typedef r_type_string_tag r_category ; } ;
#if __cplusplus >= 201703L
template<> struct r_type_traits<std::string_view>{ typedef r_type_string_tag r_category ; } ;
#endif
template<> struct r_type_traits<char>{ typedef r_type_string_tag r_category ; } ;
template<> struct r_type_traits<wchar_t>{ typedef r_type_string_tag r_category ; } ;

3 changes: 3 additions & 0 deletions inst/include/Rcpp/traits/wrap_type_traits.h
Original file line number Diff line number Diff line change
@@ -82,6 +82,9 @@ template <> struct wrap_type_traits<unsigned int> { typedef wrap_type_primitive_
template <> struct wrap_type_traits<bool> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<std::string> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<std::wstring> { typedef wrap_type_primitive_tag wrap_category; } ;
#if __cplusplus >= 201703L
template <> struct wrap_type_traits<std::string_view> { typedef wrap_type_primitive_tag wrap_category; } ;
#endif
template <> struct wrap_type_traits<Rcpp::String> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<char> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<wchar_t> { typedef wrap_type_primitive_tag wrap_category; } ;
3 changes: 3 additions & 0 deletions inst/include/RcppCommon.h
Original file line number Diff line number Diff line change
@@ -54,6 +54,9 @@ namespace Rcpp {
#include <iomanip>
#include <sstream>
#include <string>
#if __cplusplus >= 201703L
#include <string_view>
#endif
#include <list>
#include <map>
#include <set>
7 changes: 7 additions & 0 deletions inst/tinytest/cpp/wrap.cpp
Original file line number Diff line number Diff line change
@@ -304,3 +304,10 @@ SEXP vector_Foo(){
vec[1] = Foo( 3 ) ;
return wrap(vec) ;
}

// [[Rcpp::plugins(cpp17)]]
// [[Rcpp::export]]
SEXP test_wrap_string_view(){
std::string_view sv = "test string value" ;
return wrap(sv) ;
}
3 changes: 3 additions & 0 deletions inst/tinytest/test_wrap.R
Original file line number Diff line number Diff line change
@@ -132,3 +132,6 @@ expect_equal(sapply( vector_Foo(), function(.) .$get() ), c(2, 3),

# test.wrap.custom.class <- function() {
expect_equal(test_wrap_custom_class(), 42)

# test.wrap.string_view <- function() {
expect_equal(test_wrap_string_view(), "test string value")