1
votes

I want to serialize a hash_multimap, does protocal buffers support it ? I have tried boost serializaitn but it has header file confusion about hash_multimap so I want to try google protocol buffers.

1
I would recommend you to get the header file confusion sorted for boost serialization :) rather than going for protocol buffer unless performance is very important. - Arunmu

1 Answers

2
votes

There's a fair bit of boilerplate coding that goes with using protocol buffers, and you need to run their protoc compiler to generate the actual C++ from the .proto files, but aside from that they're great.

Here's an example of how you'd serialise and parse a std::hash_multimap

my_hash_multimap.proto

message MyHashMultimap {
  message Pair {
    required int64 key = 1;
    required bytes value = 2;
  }
  repeated Pair pair = 1;
}

main.cpp

#include <algorithm>
#include <hash_map>
#include <iostream>
#include <string>
#include <utility>

#include "my_hash_multimap.pb.h"

int main() {
  std::hash_multimap<int, std::string> hm;
  hm.insert(std::make_pair(3, "three"));
  hm.insert(std::make_pair(2, "two"));
  hm.insert(std::make_pair(1, "one"));

  // convert std::hash_multimap to a protobuf MyHashMultimap
  MyHashMultimap proto_hm;
  std::for_each(hm.begin(),
                hm.end(),
                [&proto_hm](std::pair<int, std::string> p) {
    std::cout << p.first << "   " << p.second << std::endl;
    // add new Pair to proto_hm
    MyHashMultimap::Pair* proto_pair(proto_hm.add_pair());
    // set this Pair's values
    proto_pair->set_key(p.first);
    proto_pair->set_value(p.second);
  });

  // serialise proto_hm to a std::string
  std::string serialised_hm(proto_hm.SerializeAsString());

  // parse from this string to a new MyHashMultimap
  MyHashMultimap parsed_proto_hm;
  if (!parsed_proto_hm.ParseFromString(serialised_hm))
    return -1;
  std::cout << std::endl << parsed_proto_hm.DebugString() << std::endl << std::endl;

  // convert protobuf MyHashMultimap to a std::hash_multimap
  std::hash_multimap<int, std::string> parsed_hm;
  for (int i(0); i != parsed_proto_hm.pair_size(); ++i) {
    // check required fields are populated correctly
    if (parsed_proto_hm.pair(i).IsInitialized()) {
      // add the Pair to parsed_hm
      parsed_hm.insert(std::make_pair(parsed_proto_hm.pair(i).key(),
                                      parsed_proto_hm.pair(i).value()));
    }
  }

  std::for_each(parsed_hm.begin(),
                parsed_hm.end(),
                [](std::pair<int, std::string> p) {
    std::cout << p.first << "   " << p.second << std::endl;
  });

  return 0;
}