Skip to content

Commit

Permalink
Merge pull request #2272 from jwillemsen/jwi-stringview
Browse files Browse the repository at this point in the history
Extended output CDR classes to serialize a std::string_view directly
  • Loading branch information
jwillemsen authored Aug 5, 2024
2 parents b982a45 + 852621f commit 484ff9d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ACE/ace/CDR_Size.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions ACE/ace/CDR_Size.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<ACE_CDR::ULong> (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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions ACE/ace/CDR_Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#endif /* ACE_HAS_MONITOR_POINTS==1 */

#include <string>
#include <string_view>

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions ACE/ace/CDR_Stream.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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<ACE_CDR::ULong> (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)
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions ACE/tests/CDR_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -246,6 +250,7 @@ short_stream ()
// std::string, or the like.
std::unique_ptr<ACE_CDR::WChar[]> safe_wstr (wstr1);
is >> std_str1;
is >> std_stringview1;
#if !defined(ACE_LACKS_STD_WSTRING)
is >> std_wstr1;
#endif
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions TAO/tao/CDR.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions TAO/tao/CDR.inl
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ ACE_INLINE CORBA::Boolean operator<< (TAO_OutputCDR &os,
&& static_cast<ACE_OutputCDR &> (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<ACE_OutputCDR &> (os) << x;
}

ACE_INLINE CORBA::Boolean operator<< (TAO_OutputCDR &os,
ACE_OutputCDR::from_std_string x)
{
Expand Down

0 comments on commit 484ff9d

Please sign in to comment.