[zeromq-dev] How to send/receive a custom C++ object via zeromq sockets?

Bjorn Reese breese at mail1.stofanet.dk
Wed Sep 18 12:19:44 CEST 2013


On 09/17/2013 06:27 PM, Riskybiz wrote:

> I have the notion that I’d like to try encapsulating each line of the
> data to be sent in a custom class object object instance; then serialize
> each object, pass it through the REQ-REP sockets and deserialise to
> reconstruct the object at the client end.  The simple class would be in
> the format like so; only comprising data members.

Boost.Serialization [1] does this for you. You can serialize into
various wire formats. If you feel comfortable with templates, you can
add your own format (or even Protocol Buffer, although contrary to the
other respondents, I am not a big fan of it). I have added several
formats, including JSON [2].

In you own class, all you need to do is add the serialize() member
function as shown below.

   class Instruction
   {
   public:

   template <typename Archive>
   void serialize(Archive& archive, const unsigned int)
   {
      archive & IntData;
      archive & DblData;
      archive & StrData;
   }

   //Data Members
   const unsigned int IntData;
   const double DblData;
   const std::string StrData;
};

[1] http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/index.html
[2] http://protoc.sourceforge.net/




More information about the zeromq-dev mailing list