From 852621f99f833e2fc22673490b69a5f048027b37 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Mon, 5 Aug 2024 10:19:41 +0200 Subject: [PATCH] Extended output CDR classes to serialize a std::string_view directly * ACE/ace/CDR_Size.h: * ACE/ace/CDR_Size.inl: * ACE/ace/CDR_Stream.h: * ACE/ace/CDR_Stream.inl: * ACE/tests/CDR_Test.cpp: * TAO/tao/CDR.h: * TAO/tao/CDR.inl: --- ACE/ace/CDR_Size.h | 3 +++ ACE/ace/CDR_Size.inl | 16 ++++++++++++++++ ACE/ace/CDR_Stream.h | 4 ++++ ACE/ace/CDR_Stream.inl | 16 ++++++++++++++++ ACE/tests/CDR_Test.cpp | 11 +++++++++++ TAO/tao/CDR.h | 2 ++ TAO/tao/CDR.inl | 9 +++++++++ 7 files changed, 61 insertions(+) diff --git a/ACE/ace/CDR_Size.h b/ACE/ace/CDR_Size.h index d29a1a3975d2e..1668bba36132e 100644 --- a/ACE/ace/CDR_Size.h +++ b/ACE/ace/CDR_Size.h @@ -79,6 +79,7 @@ class ACE_Export ACE_SizeCDR ACE_CDR::Boolean write_wstring (ACE_CDR::ULong length, const ACE_CDR::WChar *x); ACE_CDR::Boolean write_string (const std::string &x); + ACE_CDR::Boolean write_string_view (const std::string_view &x); #if !defined(ACE_LACKS_STD_WSTRING) ACE_CDR::Boolean write_wstring (const std::wstring &x); #endif @@ -230,6 +231,8 @@ extern ACE_Export ACE_CDR::Boolean operator<< (ACE_SizeCDR &ss, const ACE_CDR::WChar* x); extern ACE_Export ACE_CDR::Boolean operator<< (ACE_SizeCDR &ss, const std::string& x); +extern ACE_Export ACE_CDR::Boolean operator<< (ACE_SizeCDR &ss, + const std::string_view& x); #if !defined(ACE_LACKS_STD_WSTRING) extern ACE_Export ACE_CDR::Boolean operator<< (ACE_SizeCDR &ss, const std::wstring& x); diff --git a/ACE/ace/CDR_Size.inl b/ACE/ace/CDR_Size.inl index 29cc5ac9a7693..d4af1432accf0 100644 --- a/ACE/ace/CDR_Size.inl +++ b/ACE/ace/CDR_Size.inl @@ -152,6 +152,15 @@ ACE_SizeCDR::write_string (const std::string &x) x.empty () ? 0 : x.c_str ()); } +ACE_INLINE ACE_CDR::Boolean +ACE_SizeCDR::write_string_view (const std::string_view &x) +{ + ACE_CDR::ULong len = + static_cast (x.size ()); + return this->write_string (len, + x.empty () ? 0 : x.data ()); +} + #if !defined(ACE_LACKS_STD_WSTRING) ACE_INLINE ACE_CDR::Boolean ACE_SizeCDR::write_wstring (const std::wstring &x) @@ -399,6 +408,13 @@ operator<< (ACE_SizeCDR &ss, const std::string& x) return ss.good_bit (); } +ACE_INLINE ACE_CDR::Boolean +operator<< (ACE_SizeCDR &ss, const std::string_view& x) +{ + ss.write_string_view (x); + return ss.good_bit (); +} + #if !defined(ACE_LACKS_STD_WSTRING) ACE_INLINE ACE_CDR::Boolean operator<< (ACE_SizeCDR &ss, const std::wstring& x) diff --git a/ACE/ace/CDR_Stream.h b/ACE/ace/CDR_Stream.h index c1cd997330301..3249fc46adeb5 100644 --- a/ACE/ace/CDR_Stream.h +++ b/ACE/ace/CDR_Stream.h @@ -57,6 +57,7 @@ #endif /* ACE_HAS_MONITOR_POINTS==1 */ #include +#include ACE_BEGIN_VERSIONED_NAMESPACE_DECL @@ -284,6 +285,7 @@ class ACE_Export ACE_OutputCDR ACE_CDR::Boolean write_wstring (ACE_CDR::ULong length, const ACE_CDR::WChar *x); ACE_CDR::Boolean write_string (const std::string &x); + ACE_CDR::Boolean write_string_view (const std::string_view &x); #if !defined(ACE_LACKS_STD_WSTRING) ACE_CDR::Boolean write_wstring (const std::wstring &x); #endif @@ -1437,6 +1439,8 @@ extern ACE_Export ACE_CDR::Boolean operator<< (ACE_OutputCDR &os, ACE_OutputCDR::from_std_string x); extern ACE_Export ACE_CDR::Boolean operator<< (ACE_OutputCDR &os, const std::string& x); +extern ACE_Export ACE_CDR::Boolean operator<< (ACE_OutputCDR &os, + const std::string_view& x); #if !defined(ACE_LACKS_STD_WSTRING) extern ACE_Export ACE_CDR::Boolean operator<< (ACE_OutputCDR &os, ACE_OutputCDR::from_std_wstring x); diff --git a/ACE/ace/CDR_Stream.inl b/ACE/ace/CDR_Stream.inl index cbfa8203fb647..ab520f7b86951 100644 --- a/ACE/ace/CDR_Stream.inl +++ b/ACE/ace/CDR_Stream.inl @@ -368,6 +368,15 @@ ACE_OutputCDR::write_string (const std::string &x) x.empty () ? 0 : x.c_str ()); } +ACE_INLINE ACE_CDR::Boolean +ACE_OutputCDR::write_string_view (const std::string_view &x) +{ + ACE_CDR::ULong const len = + static_cast (x.size ()); + return this->write_string (len, + x.empty () ? 0 : x.data ()); +} + #if !defined(ACE_LACKS_STD_WSTRING) ACE_INLINE ACE_CDR::Boolean ACE_OutputCDR::write_wstring (const std::wstring &x) @@ -1385,6 +1394,13 @@ operator<< (ACE_OutputCDR &os, const std::string& x) return os.good_bit (); } +ACE_INLINE ACE_CDR::Boolean +operator<< (ACE_OutputCDR &os, const std::string_view& x) +{ + os.write_string_view (x); + return os.good_bit (); +} + #if !defined(ACE_LACKS_STD_WSTRING) ACE_INLINE ACE_CDR::Boolean operator<< (ACE_OutputCDR &os, const std::wstring& x) diff --git a/ACE/tests/CDR_Test.cpp b/ACE/tests/CDR_Test.cpp index ccbf14f55a821..3cdcac82ec491 100644 --- a/ACE/tests/CDR_Test.cpp +++ b/ACE/tests/CDR_Test.cpp @@ -111,6 +111,7 @@ short_stream () ACE_CDR::WChar *wstr = wchar2; ACE_CString str ("Test String"); std::string std_str ("std string"); + std::string_view std_stringview {"std stringview"}; #if !defined(ACE_LACKS_STD_WSTRING) std::wstring std_wstr (L"std wstring"); #endif @@ -136,6 +137,7 @@ short_stream () os << str; os << wstr; os << std_str; + os << std_stringview; #if !defined(ACE_LACKS_STD_WSTRING) os << std_wstr; #endif @@ -158,6 +160,7 @@ short_stream () ss << str; ss << wstr; ss << std_str; + ss << std_stringview; #if !defined(ACE_LACKS_STD_WSTRING) ss << std_wstr; #endif @@ -215,6 +218,7 @@ short_stream () ACE_CDR::WChar *wstr1 = 0; ACE_CString str1; std::string std_str1; + std::string std_stringview1; #if !defined(ACE_LACKS_STD_WSTRING) std::wstring std_wstr1; #endif @@ -246,6 +250,7 @@ short_stream () // std::string, or the like. std::unique_ptr safe_wstr (wstr1); is >> std_str1; + is >> std_stringview1; #if !defined(ACE_LACKS_STD_WSTRING) is >> std_wstr1; #endif @@ -291,6 +296,12 @@ short_stream () ACE_TEXT ("std::string transfer error")), 1); + if (std_stringview1 != std_stringview) + ACE_ERROR_RETURN ((LM_ERROR, + ACE_TEXT ("%p\n"), + ACE_TEXT ("std::string_view transfer error")), + 1); + #if !defined(ACE_LACKS_STD_WSTRING) if (std_wstr1 != std_wstr) ACE_ERROR_RETURN ((LM_ERROR, diff --git a/TAO/tao/CDR.h b/TAO/tao/CDR.h index c6b7d64b17f78..8a11ed08a86b0 100644 --- a/TAO/tao/CDR.h +++ b/TAO/tao/CDR.h @@ -491,6 +491,8 @@ TAO_Export CORBA::Boolean operator<< (TAO_OutputCDR &os, ACE_OutputCDR::from_wstring x); TAO_Export CORBA::Boolean operator<< (TAO_OutputCDR &os, const std::string &x); +TAO_Export CORBA::Boolean operator<< (TAO_OutputCDR &os, + const std::string_view &x); TAO_Export CORBA::Boolean operator<< (TAO_OutputCDR &os, ACE_OutputCDR::from_std_string x); #if !defined(ACE_LACKS_STD_WSTRING) diff --git a/TAO/tao/CDR.inl b/TAO/tao/CDR.inl index 2180ec07e88fb..e881ea29bd074 100644 --- a/TAO/tao/CDR.inl +++ b/TAO/tao/CDR.inl @@ -460,6 +460,15 @@ ACE_INLINE CORBA::Boolean operator<< (TAO_OutputCDR &os, && static_cast (os) << x; } +ACE_INLINE CORBA::Boolean operator<< (TAO_OutputCDR &os, + const std::string_view &x) +{ + return + os.fragment_stream (ACE_CDR::OCTET_ALIGN, + sizeof (char)) + && static_cast (os) << x; +} + ACE_INLINE CORBA::Boolean operator<< (TAO_OutputCDR &os, ACE_OutputCDR::from_std_string x) {