Move implementation of ReadProto/WriteProto to .cc (#835)

master
Alexander Belyaev 2018-01-22 17:53:31 +01:00 committed by GitHub
parent 0c227097e7
commit 6d4649857a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 16 deletions

View File

@ -47,8 +47,6 @@ ProtoStreamWriter::ProtoStreamWriter(const std::string& filename)
WriteSizeAsLittleEndian(kMagic, &out_);
}
ProtoStreamWriter::~ProtoStreamWriter() {}
void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
std::string compressed_data;
common::FastGzipString(uncompressed_data, &compressed_data);
@ -56,6 +54,12 @@ void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
out_.write(compressed_data.data(), compressed_data.size());
}
void ProtoStreamWriter::WriteProto(const google::protobuf::Message& proto) {
std::string uncompressed_data;
proto.SerializeToString(&uncompressed_data);
Write(uncompressed_data);
}
bool ProtoStreamWriter::Close() {
out_.close();
return !out_.fail();
@ -69,8 +73,6 @@ ProtoStreamReader::ProtoStreamReader(const std::string& filename)
}
}
ProtoStreamReader::~ProtoStreamReader() {}
bool ProtoStreamReader::Read(std::string* decompressed_data) {
uint64 compressed_size;
if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
@ -84,6 +86,11 @@ bool ProtoStreamReader::Read(std::string* decompressed_data) {
return true;
}
bool ProtoStreamReader::ReadProto(google::protobuf::Message* proto) {
std::string decompressed_data;
return Read(&decompressed_data) && proto->ParseFromString(decompressed_data);
}
bool ProtoStreamReader::eof() const { return in_.eof(); }
} // namespace io

View File

@ -34,17 +34,13 @@ namespace io {
class ProtoStreamWriter {
public:
ProtoStreamWriter(const std::string& filename);
~ProtoStreamWriter();
~ProtoStreamWriter() = default;
ProtoStreamWriter(const ProtoStreamWriter&) = delete;
ProtoStreamWriter& operator=(const ProtoStreamWriter&) = delete;
// Serializes, compressed and writes the 'proto' to the file.
void WriteProto(const google::protobuf::Message& proto) {
std::string uncompressed_data;
proto.SerializeToString(&uncompressed_data);
Write(uncompressed_data);
}
void WriteProto(const google::protobuf::Message& proto);
// This should be called to check whether writing was successful.
bool Close();
@ -59,16 +55,12 @@ class ProtoStreamWriter {
class ProtoStreamReader {
public:
ProtoStreamReader(const std::string& filename);
~ProtoStreamReader();
~ProtoStreamReader() = default;
ProtoStreamReader(const ProtoStreamReader&) = delete;
ProtoStreamReader& operator=(const ProtoStreamReader&) = delete;
bool ReadProto(google::protobuf::Message* proto) {
std::string decompressed_data;
return Read(&decompressed_data) &&
proto->ParseFromString(decompressed_data);
}
bool ReadProto(google::protobuf::Message* proto);
bool eof() const;