Move implementation of ReadProto/WriteProto to .cc (#835)
parent
0c227097e7
commit
6d4649857a
|
@ -47,8 +47,6 @@ ProtoStreamWriter::ProtoStreamWriter(const std::string& filename)
|
||||||
WriteSizeAsLittleEndian(kMagic, &out_);
|
WriteSizeAsLittleEndian(kMagic, &out_);
|
||||||
}
|
}
|
||||||
|
|
||||||
ProtoStreamWriter::~ProtoStreamWriter() {}
|
|
||||||
|
|
||||||
void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
|
void ProtoStreamWriter::Write(const std::string& uncompressed_data) {
|
||||||
std::string compressed_data;
|
std::string compressed_data;
|
||||||
common::FastGzipString(uncompressed_data, &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());
|
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() {
|
bool ProtoStreamWriter::Close() {
|
||||||
out_.close();
|
out_.close();
|
||||||
return !out_.fail();
|
return !out_.fail();
|
||||||
|
@ -69,8 +73,6 @@ ProtoStreamReader::ProtoStreamReader(const std::string& filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ProtoStreamReader::~ProtoStreamReader() {}
|
|
||||||
|
|
||||||
bool ProtoStreamReader::Read(std::string* decompressed_data) {
|
bool ProtoStreamReader::Read(std::string* decompressed_data) {
|
||||||
uint64 compressed_size;
|
uint64 compressed_size;
|
||||||
if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
|
if (!ReadSizeAsLittleEndian(&in_, &compressed_size)) {
|
||||||
|
@ -84,6 +86,11 @@ bool ProtoStreamReader::Read(std::string* decompressed_data) {
|
||||||
return true;
|
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(); }
|
bool ProtoStreamReader::eof() const { return in_.eof(); }
|
||||||
|
|
||||||
} // namespace io
|
} // namespace io
|
||||||
|
|
|
@ -34,17 +34,13 @@ namespace io {
|
||||||
class ProtoStreamWriter {
|
class ProtoStreamWriter {
|
||||||
public:
|
public:
|
||||||
ProtoStreamWriter(const std::string& filename);
|
ProtoStreamWriter(const std::string& filename);
|
||||||
~ProtoStreamWriter();
|
~ProtoStreamWriter() = default;
|
||||||
|
|
||||||
ProtoStreamWriter(const ProtoStreamWriter&) = delete;
|
ProtoStreamWriter(const ProtoStreamWriter&) = delete;
|
||||||
ProtoStreamWriter& operator=(const ProtoStreamWriter&) = delete;
|
ProtoStreamWriter& operator=(const ProtoStreamWriter&) = delete;
|
||||||
|
|
||||||
// Serializes, compressed and writes the 'proto' to the file.
|
// Serializes, compressed and writes the 'proto' to the file.
|
||||||
void WriteProto(const google::protobuf::Message& proto) {
|
void WriteProto(const google::protobuf::Message& proto);
|
||||||
std::string uncompressed_data;
|
|
||||||
proto.SerializeToString(&uncompressed_data);
|
|
||||||
Write(uncompressed_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This should be called to check whether writing was successful.
|
// This should be called to check whether writing was successful.
|
||||||
bool Close();
|
bool Close();
|
||||||
|
@ -59,16 +55,12 @@ class ProtoStreamWriter {
|
||||||
class ProtoStreamReader {
|
class ProtoStreamReader {
|
||||||
public:
|
public:
|
||||||
ProtoStreamReader(const std::string& filename);
|
ProtoStreamReader(const std::string& filename);
|
||||||
~ProtoStreamReader();
|
~ProtoStreamReader() = default;
|
||||||
|
|
||||||
ProtoStreamReader(const ProtoStreamReader&) = delete;
|
ProtoStreamReader(const ProtoStreamReader&) = delete;
|
||||||
ProtoStreamReader& operator=(const ProtoStreamReader&) = delete;
|
ProtoStreamReader& operator=(const ProtoStreamReader&) = delete;
|
||||||
|
|
||||||
bool ReadProto(google::protobuf::Message* proto) {
|
bool ReadProto(google::protobuf::Message* proto);
|
||||||
std::string decompressed_data;
|
|
||||||
return Read(&decompressed_data) &&
|
|
||||||
proto->ParseFromString(decompressed_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool eof() const;
|
bool eof() const;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue