diff --git a/coding/coding_tests/reader_writer_ops_test.cpp b/coding/coding_tests/reader_writer_ops_test.cpp index a9ebc9f4f9..9b2a91191c 100644 --- a/coding/coding_tests/reader_writer_ops_test.cpp +++ b/coding/coding_tests/reader_writer_ops_test.cpp @@ -5,6 +5,8 @@ #include "../../coding/reader_writer_ops.hpp" #include "../../coding/file_reader.hpp" #include "../../coding/file_writer.hpp" +#include "../../coding/read_write_utils.hpp" +#include "../../coding/byte_stream.hpp" #include "../../std/algorithm.hpp" @@ -77,3 +79,40 @@ UNIT_TEST(Reverse_Smoke) FileWriter::DeleteFileX(tmpFile); } } + +namespace +{ + struct ThePOD + { + uint32_t m_i; + double m_d; + }; + + bool operator==(ThePOD const & r1, ThePOD const & r2) + { + return (r1.m_i == r2.m_i && r1.m_d == r2.m_d); + } +} + +UNIT_TEST(ReadWrite_POD) +{ + srand(666); + + size_t const count = 1000; + vector src(1000); + for (size_t i = 0; i < count; ++i) + { + src[i].m_i = rand(); + src[i].m_d = double(rand()) / double(rand()); + } + + vector buffer; + PushBackByteSink > sink(buffer); + rw::WriteVectorOfPOD(sink, src); + + buffer_vector dest; + ArrayByteSource byteSrc(buffer.data()); + rw::ReadVectorOfPOD(byteSrc, dest); + + TEST(equal(src.begin(), src.end(), dest.begin()), ()); +} diff --git a/coding/read_write_utils.hpp b/coding/read_write_utils.hpp index e818809da3..b7bf10a14a 100644 --- a/coding/read_write_utils.hpp +++ b/coding/read_write_utils.hpp @@ -6,6 +6,7 @@ #include "../std/string.hpp" #include "../std/vector.hpp" +//#include "../std/type_traits.hpp" namespace rw @@ -86,27 +87,31 @@ namespace rw } template - void ReadRaw(TSource & src, TCont & v) + void ReadVectorOfPOD(TSource & src, TCont & v) { - STATIC_ASSERT(sizeof(typename TCont::value_type) == 1); + typedef typename TCont::value_type ValueT; + // Not every compiler support this. + //STATIC_ASSERT(boost::is_pod::value); uint32_t const count = ReadVarUint(src); if (count > 0) { v.resize(count); - src.Read(&v[0], count); + src.Read(&v[0], count * sizeof(ValueT)); } } template - void WriteRaw(TSink & sink, TCont const & v) + void WriteVectorOfPOD(TSink & sink, TCont const & v) { - STATIC_ASSERT(sizeof(typename TCont::value_type) == 1); + typedef typename TCont::value_type ValueT; + // Not every compiler support this. + //STATIC_ASSERT(boost::is_pod::value); uint32_t const count = static_cast(v.size()); WriteVarUint(sink, count); if (count > 0) - sink.Write(&v[0], count); + sink.Write(&v[0], count * sizeof(ValueT)); } } diff --git a/indexer/string_file.cpp b/indexer/string_file.cpp index 2283552497..b51532abc3 100644 --- a/indexer/string_file.cpp +++ b/indexer/string_file.cpp @@ -17,7 +17,7 @@ StringsFile::IdT StringsFile::StringT::Write(TWriter & writer) const CHECK_EQUAL(static_cast(pos), writer.Pos(), ()); rw::Write(writer, m_name); - rw::WriteRaw(writer, m_val); + rw::WriteVectorOfPOD(writer, m_val); return pos; } @@ -26,7 +26,7 @@ template void StringsFile::StringT::Read(TReader & src) { rw::Read(src, m_name); - rw::ReadRaw(src, m_val); + rw::ReadVectorOfPOD(src, m_val); } bool StringsFile::StringT::operator < (StringT const & name) const