views:

502

answers:

2

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.

+3  A: 

I think you misunderstood the philosophy.

IMHO Google's Protocol Buffers is meant to produce 'messages' class that are distinct from your application classes. Not that this is NOT a serialization library (though it may be used as such) but 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.

Matthieu M.
Ok, I think I understand what you are saying (I suspected as much when posting my classes). However, if you take the "behaviours" (i.e. serialization/deserialization etc) from the classes above, I am sure you will agree that they are simply "containers". The question then degenerates to how can I "port" a C++ class that contains a vector of other C++ classes (which may themselves be nested) to a .proto file?
Stick it to THE MAN
Here is the c++ tutorial: http://code.google.com/intl/fr/apis/protocolbuffers/docs/cpptutorial.html, you can note 2 things: it's perfectly possible to nest messages within one another and the `repeated` keyword allows to emulate the behavior of containers.
Matthieu M.
I'll read the docs again and see if I can get a quick example to work. Incidentally,
Stick it to THE MAN
I created the .pto from my headers and succesfully compiled it, to create the C++ stubs. I peeked in the generated files and saw some google headers e.g.: #include <google/protobuf/stubs/common.h> etc being included. This is not mentioned in the docs. I did a (very quick) google search for the headers (and presumably a link library), but found nothing specific. Care to share some light ... (anyone?)
Stick it to THE MAN
Like the name indicates, this header is relative to `protobuf` itself. If you can generate the headers, you have downloaded the pack, it includes a C++ component that you should include in your build system.
Matthieu M.
I had initially loaded only the compiler (for the Windows platform). I have managed to build succesfully from the full source on my Linux box. Thanks for your help
Stick it to THE MAN
A: 

I think the answer for converting to using protobuf messages is something like this:

message Classroom { 
   required int id = 1; 
   required People teacher = 2;
   repeated People bar = 3; 
}

message People { 
   required int id = 1; 
   required string name = 2; 
} 
Chris Kaminski