I have just come accross Google’s Protocol buffers. It seems to be the solution for a C++ backend application I am writing. Problem is I cant seem to find anything regarding vector types. The documentation mentions repeated_types, but I cant seem to find anything.
Supposing I have these set of classes:
class UnifiedBinaryHeader
{
public:
UnifiedBinaryHeader();
void Serialize(std::ostream& output) const;
void Deserialize(std::istream& input);
private:
wxString m_name;
wxDateTime m_time;
};
struct UnifiedBinaryRow
{
wxDateTime date;
float value;
UnifiedBinaryRow()
{
value= 0;
}
void Serialize(std::ostream& output) const;
void Deserialize(std::istream& input);
};
class UnifiedBinaryRowCollection
{
private:
typedef std::vector<UnifiedBinaryRow> UnifiedBinaryRowVector;
public:
typedef UnifiedBinaryRowVector::iterator iterator;
typedef UnifiedBinaryRowVector::const_iterator const_iterator;
UnifiedBinaryRowCollection();
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
UnifiedBinaryRowCollection& AddRow(const UnifiedBinaryRow& row);
size_t NumRows() const;
private:
UnifiedBinaryRowVector m_rows;
};
class UnifiedBinaryFormat
{
public:
UnifiedBinaryFormat();
UnifiedBinaryHeader& Header();
const UnifiedBinaryHeader& Header() const;
UnifiedBinaryFormat& Header(UnifiedBinaryHeader& header);
UnifiedBinaryRowCollection& Rows();
const UnifiedBinaryRowCollection& Rows() const;
UnifiedBinaryFormat& Rows(const UnifiedBinaryRowCollection& rows);
void Serialize(std::ostream& output) const;
void Deserialize(std::istream& input);
private:
UnifiedBinaryHeader m_header;
UnifiedBinaryRowCollection m_rows;
};
How may I write a .proto file for these classes, seeing as I am using a lot of members that are vectors. Any help in “porting” these classes to a .proto file I can use will be much appreciated.
I think you misunderstood the philosophy.
IMHO Google’s Protocol Buffers is meant to produce ‘messages’ class that are distinct from your application classes. Note that Protobuf is NOT a serialization library (though it may be used as such). It is a messaging library, which allows to exchange messages between different subsystems / languages.
So, proto will generate messages class, and then for your serialization/deserialization it will convert the stream to an object of its own class and your job is to convert the proto object to your actual object, or group of objects.
It’s a nice way to decouple the actual implementation of the class from the serialization / deserialization mechanism imo.