Skip to content

Commit

Permalink
Implementations for std::pmr::string (boostorg#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
schorsch1976 committed Oct 14, 2022
1 parent a97328d commit 1a0ab95
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 2 deletions.
12 changes: 12 additions & 0 deletions include/boost/archive/basic_binary_iprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ namespace std{
} // namespace std
#endif

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#include <memory_resource>
#endif

#include <boost/cstdint.hpp>
#include <boost/serialization/throw_exception.hpp>
#include <boost/integer.hpp>
Expand Down Expand Up @@ -107,9 +111,17 @@ class BOOST_SYMBOL_VISIBLE basic_binary_iprimitive {
}
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
load(std::string &s);
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
load(std::pmr::string &s);
#endif
#ifndef BOOST_NO_STD_WSTRING
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
load(std::wstring &ws);
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
load(std::pmr::wstring& ws);
#endif
#endif
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
load(char * t);
Expand Down
14 changes: 14 additions & 0 deletions include/boost/archive/basic_binary_oprimitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace std{
} // namespace std
#endif

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#include <memory_resource>
#endif

#include <boost/cstdint.hpp>
#include <boost/integer.hpp>
#include <boost/integer_traits.hpp>
Expand Down Expand Up @@ -101,9 +105,19 @@ class BOOST_SYMBOL_VISIBLE basic_binary_oprimitive {
}
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
save(const std::string &s);

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
save(const std::pmr::string &s);
#endif

#ifndef BOOST_NO_STD_WSTRING
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
save(const std::wstring &ws);
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
save(const std::pmr::wstring& ws);
#endif
#endif
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
save(const char * t);
Expand Down
34 changes: 34 additions & 0 deletions include/boost/archive/impl/basic_binary_iprimitive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
load_binary(&(*s.begin()), l);
}

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive, class Elem, class Tr>
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
basic_binary_iprimitive<Archive, Elem, Tr>::load(std::pmr::string & s)
{
std::size_t l;
this->This()->load(l);
// borland de-allocator fixup
#if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
if(NULL != s.data())
#endif
s.resize(l);
// note breaking a rule here - could be a problem on some platform
if(0 < l)
load_binary(&(*s.begin()), l);
}
#endif

template<class Archive, class Elem, class Tr>
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
basic_binary_iprimitive<Archive, Elem, Tr>::load(char * s)
Expand All @@ -139,6 +157,22 @@ basic_binary_iprimitive<Archive, Elem, Tr>::load(std::wstring & ws)
// note breaking a rule here - is could be a problem on some platform
load_binary(const_cast<wchar_t *>(ws.data()), l * sizeof(wchar_t) / sizeof(char));
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive, class Elem, class Tr>
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
basic_binary_iprimitive<Archive, Elem, Tr>::load(std::pmr::wstring& ws)
{
std::size_t l;
this->This()->load(l);
// borland de-allocator fixup
#if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
if (NULL != ws.data())
#endif
ws.resize(l);
// note breaking a rule here - is could be a problem on some platform
load_binary(const_cast<wchar_t*>(ws.data()), l * sizeof(wchar_t) / sizeof(char));
}
#endif
#endif

template<class Archive, class Elem, class Tr>
Expand Down
25 changes: 25 additions & 0 deletions include/boost/archive/impl/basic_binary_oprimitive.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace std{
} // namespace std
#endif

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#include <memory_resource>
#endif

#ifndef BOOST_NO_CWCHAR
#include <cwchar>
#ifdef BOOST_NO_STDC_NAMESPACE
Expand Down Expand Up @@ -70,6 +74,17 @@ basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::string &s)
save_binary(s.data(), l);
}

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive, class Elem, class Tr>
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::pmr::string &s)
{
std::size_t l = static_cast<std::size_t>(s.size());
this->This()->save(l);
save_binary(s.data(), l);
}
#endif

#ifndef BOOST_NO_CWCHAR
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<class Archive, class Elem, class Tr>
Expand All @@ -91,6 +106,16 @@ basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::wstring &ws)
this->This()->save(l);
save_binary(ws.data(), l * sizeof(wchar_t) / sizeof(char));
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive, class Elem, class Tr>
BOOST_ARCHIVE_OR_WARCHIVE_DECL void
basic_binary_oprimitive<Archive, Elem, Tr>::save(const std::pmr::wstring& ws)
{
std::size_t l = ws.size();
this->This()->save(l);
save_binary(ws.data(), l * sizeof(wchar_t) / sizeof(char));
}
#endif
#endif
#endif // BOOST_NO_CWCHAR

Expand Down
10 changes: 10 additions & 0 deletions include/boost/archive/impl/basic_xml_grammar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/version.hpp>

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#include <memory_resource>
#endif

namespace boost {
namespace archive {

Expand All @@ -74,6 +78,9 @@ class BOOST_SYMBOL_VISIBLE basic_xml_grammar {
private:
typedef typename std::basic_istream<CharType> IStream;
typedef typename std::basic_string<CharType> StringType;
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
typedef typename std::pmr::basic_string<CharType> PmrStringType;
#endif
typedef typename boost::spirit::classic::chset<CharType> chset_t;
typedef typename boost::spirit::classic::chlit<CharType> chlit_t;
typedef typename boost::spirit::classic::scanner<
Expand Down Expand Up @@ -162,6 +169,9 @@ class BOOST_SYMBOL_VISIBLE basic_xml_grammar {
bool parse_start_tag(IStream & is) /*const*/;
bool parse_end_tag(IStream & is) const;
bool parse_string(IStream & is, StringType & s) /*const*/;
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
bool parse_string(IStream & is, PmrStringType & s) /*const*/;
#endif
void init(IStream & is);
bool windup(IStream & is);
basic_xml_grammar();
Expand Down
36 changes: 35 additions & 1 deletion include/boost/archive/impl/text_iarchive_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ text_iarchive_impl<Archive>::load(std::string &s)
if(0 < size)
is.read(&(*s.begin()), size);
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
text_iarchive_impl<Archive>::load(std::pmr::string& s)
{
std::size_t size;
*this->This() >> size;
// skip separating space
is.get();
// borland de-allocator fixup
#if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
if (NULL != s.data())
#endif
s.resize(size);
if (0 < size)
is.read(&(*s.begin()), size);
}
#endif

#ifndef BOOST_NO_CWCHAR
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
Expand Down Expand Up @@ -88,7 +106,23 @@ text_iarchive_impl<Archive>::load(std::wstring &ws)
is.get();
is.read((char *)ws.data(), size * sizeof(wchar_t)/sizeof(char));
}

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
text_iarchive_impl<Archive>::load(std::pmr::wstring& ws)
{
std::size_t size;
*this->This() >> size;
// borland de-allocator fixup
#if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
if (NULL != ws.data())
#endif
ws.resize(size);
// skip separating space
is.get();
is.read((char*)ws.data(), size * sizeof(wchar_t) / sizeof(char));
}
#endif // BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#endif // BOOST_NO_STD_WSTRING
#endif // BOOST_NO_CWCHAR

Expand Down
22 changes: 22 additions & 0 deletions include/boost/archive/impl/text_oarchive_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ text_oarchive_impl<Archive>::save(const std::string &s)
this->This()->newtoken();
os << s;
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
text_oarchive_impl<Archive>::save(const std::pmr::string& s)
{
const std::size_t size = s.size();
*this->This() << size;
this->This()->newtoken();
os << s;
}
#endif

#ifndef BOOST_NO_CWCHAR
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
Expand All @@ -78,6 +89,17 @@ text_oarchive_impl<Archive>::save(const std::wstring &ws)
this->This()->newtoken();
os.write((const char *)(ws.data()), l * sizeof(wchar_t)/sizeof(char));
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
text_oarchive_impl<Archive>::save(const std::pmr::wstring& ws)
{
const std::size_t l = ws.size();
*this->This() << l;
this->This()->newtoken();
os.write((const char*)(ws.data()), l * sizeof(wchar_t) / sizeof(char));
}
#endif
#endif
#endif // BOOST_NO_CWCHAR

Expand Down
46 changes: 46 additions & 0 deletions include/boost/archive/impl/xml_iarchive_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,40 @@ xml_iarchive_impl<Archive>::load(std::wstring &ws){
ws += wc;
}
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
xml_iarchive_impl<Archive>::load(std::pmr::wstring& ws) {
std::string s;
bool result = gimpl->parse_string(is, s);
if (!result)
boost::serialization::throw_exception(
xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
);

#if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
if (NULL != ws.data())
#endif
ws.resize(0);
std::mbstate_t mbs = std::mbstate_t();
const char* start = s.data();
const char* end = start + s.size();
while (start < end) {
wchar_t wc;
std::size_t count = std::mbrtowc(&wc, start, end - start, &mbs);
if (count == static_cast<std::size_t>(-1))
boost::serialization::throw_exception(
iterators::dataflow_exception(
iterators::dataflow_exception::invalid_conversion
)
);
if (count == static_cast<std::size_t>(-2))
continue;
start += count;
ws += wc;
}
}
#endif // BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#endif // BOOST_NO_STD_WSTRING

#ifndef BOOST_NO_INTRINSIC_WCHAR_T
Expand Down Expand Up @@ -134,6 +168,18 @@ xml_iarchive_impl<Archive>::load(std::string &s){
);
}

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
xml_iarchive_impl<Archive>::load(std::pmr::string &s){
bool result = gimpl->parse_string(is, s);
if(! result)
boost::serialization::throw_exception(
xml_archive_exception(xml_archive_exception::xml_archive_parsing_error)
);
}
#endif

template<class Archive>
BOOST_ARCHIVE_DECL void
xml_iarchive_impl<Archive>::load(char * s){
Expand Down
26 changes: 26 additions & 0 deletions include/boost/archive/impl/xml_oarchive_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ xml_oarchive_impl<Archive>::save(const std::wstring & ws){
// save_iterator(os, ws.data(), ws.data() + std::wcslen(ws.data()));
save_iterator(os, ws.data(), ws.data() + ws.size());
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
xml_oarchive_impl<Archive>::save(const std::pmr::wstring& ws) {
// at least one library doesn't typedef value_type for strings
// so rather than using string directly make a pointer iterator out of it
// save_iterator(os, ws.data(), ws.data() + std::wcslen(ws.data()));
save_iterator(os, ws.data(), ws.data() + ws.size());
}
#endif
#endif

#ifndef BOOST_NO_INTRINSIC_WCHAR_T
Expand All @@ -84,6 +94,22 @@ xml_oarchive_impl<Archive>::save(const std::string & s){
boost::archive::iterators::ostream_iterator<char>(os)
);
}
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
template<class Archive>
BOOST_ARCHIVE_DECL void
xml_oarchive_impl<Archive>::save(const std::pmr::string& s) {
// at least one library doesn't typedef value_type for strings
// so rather than using string directly make a pointer iterator out of it
typedef boost::archive::iterators::xml_escape<
const char*
> xml_escape_translator;
std::copy(
xml_escape_translator(s.data()),
xml_escape_translator(s.data() + s.size()),
boost::archive::iterators::ostream_iterator<char>(os)
);
}
#endif

template<class Archive>
BOOST_ARCHIVE_DECL void
Expand Down
12 changes: 12 additions & 0 deletions include/boost/archive/text_iarchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <boost/archive/detail/register_archive.hpp>
#include <boost/serialization/item_version_type.hpp>

#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
#include <memory_resource>
#endif

#include <boost/archive/detail/abi_prefix.hpp> // must be the last header

#ifdef BOOST_MSVC
Expand Down Expand Up @@ -73,9 +77,17 @@ class BOOST_SYMBOL_VISIBLE text_iarchive_impl :
#endif
BOOST_ARCHIVE_DECL void
load(std::string &s);
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_DECL void
load(std::pmr::string& s);
#endif
#ifndef BOOST_NO_STD_WSTRING
BOOST_ARCHIVE_DECL void
load(std::wstring &ws);
#ifndef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
BOOST_ARCHIVE_DECL void
load(std::pmr::wstring& ws);
#endif
#endif
template<class T>
void load_override(T & t){
Expand Down
Loading

0 comments on commit 1a0ab95

Please sign in to comment.