mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Fixes empty string serialization for debug in VS (#69)
Previously we took the address of the dereferenced begin() iterator on a string to get the pointer to its data; we now just const_cast the pointer that is the result of calling data(). The original reason for using the iterator over something like data() was to avoid the const_cast and to ensure that any copy on write mechanisms were used, but this doesn't seem necessary given that we call resize() immediately prior to this. Valgrind shows no problems with the new method. Also added unit tests for this case to string.
This commit is contained in:
@@ -53,7 +53,7 @@ namespace cereal
|
|||||||
size_type size;
|
size_type size;
|
||||||
ar( make_size_tag( size ) );
|
ar( make_size_tag( size ) );
|
||||||
str.resize(static_cast<std::size_t>(size));
|
str.resize(static_cast<std::size_t>(size));
|
||||||
ar( binary_data( &(*str.begin()), static_cast<std::size_t>(size) * sizeof(CharT) ) );
|
ar( binary_data( const_cast<CharT *>( str.data() ), static_cast<std::size_t>(size) * sizeof( CharT ) ) );
|
||||||
}
|
}
|
||||||
} // namespace cereal
|
} // namespace cereal
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
#include <cereal/types/vector.hpp>
|
#include <cereal/types/vector.hpp>
|
||||||
|
|
||||||
#include <cereal/archives/binary.hpp>
|
#include <cereal/archives/binary.hpp>
|
||||||
//#include <cereal/archives/portable_binary.hpp>
|
#include <cereal/archives/portable_binary.hpp>
|
||||||
#include <cereal/archives/xml.hpp>
|
#include <cereal/archives/xml.hpp>
|
||||||
#include <cereal/archives/json.hpp>
|
#include <cereal/archives/json.hpp>
|
||||||
|
|
||||||
|
|||||||
@@ -1892,24 +1892,29 @@ void test_string_basic()
|
|||||||
|
|
||||||
for(size_t i=0; i<100; ++i)
|
for(size_t i=0; i<100; ++i)
|
||||||
{
|
{
|
||||||
std::basic_string<char> o_string = random_basic_string<char>(gen);
|
std::basic_string<char> o_string = random_basic_string<char>(gen);
|
||||||
|
std::basic_string<char> o_string2 = "";
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
{
|
{
|
||||||
OArchive oar(os);
|
OArchive oar(os);
|
||||||
oar(o_string);
|
oar(o_string);
|
||||||
|
oar(o_string2);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::basic_string<char> i_string;
|
std::basic_string<char> i_string;
|
||||||
|
std::basic_string<char> i_string2;
|
||||||
|
|
||||||
std::istringstream is(os.str());
|
std::istringstream is(os.str());
|
||||||
{
|
{
|
||||||
IArchive iar(is);
|
IArchive iar(is);
|
||||||
|
|
||||||
iar(i_string);
|
iar(i_string);
|
||||||
|
iar(i_string2);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(i_string, o_string);
|
BOOST_CHECK_EQUAL(i_string, o_string);
|
||||||
|
BOOST_CHECK_EQUAL(i_string2, o_string2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user