Suppose your protobuf messagelooks like this:
message Object
{
... = 1;
... = 2;
... = 3;
}
Then in the same file, enter another 1 message, which is a collection of these Objects.
message Objects
{
repeated Object array = 1;
}
Therefore, when you have many elements, you can just use Objectsand use SerializeAsString()on Objects. This will save your efforts on serializing an individual Objectseparately and place your own handmade limiter. You can serialize everything Objectusing one instance Objects.
With this approach, you delegate all parsing and serialization processing to the Protobuf library as well . I use this in my project and it works like a charm.
, Objects Object. . repeated protobufs ++ 11, for.
, Objects::SerializeAsString() , string, . , . std::fstream :
struct fstreamEncoded : std::fstream
{
void
operator<< (const string& content)
{
static_cast<std::fstream&>(*this) << (content.length() + 1) << "\n" << content << std::endl;
}
string
getline ()
{
char length_[20] = {};
std::istream::getline(length_, sizeof(length_) - 1);
if(*length_ == 0)
return "";
const size_t length = std::atol(length_);
string content(length, 0);
read(&content[0], length);
return content;
}
}
. .