Why msgpack-c++ do not support vector<float> or vector<double> in my case?

2.2k views Asked by At

See my code below:(you can compile the code with: g++-4.7 demo.cpp -std=c++11 -lmsgpack)

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <msgpack.hpp>
using namespace std;

template<class T>
void pack(T &t, string &str)
{
  using namespace msgpack;
  sbuffer buff;
  pack(buff, t); 
  ostringstream is; 
  is << buff.size() << buff.data();
  str = string(is.str());
}

template<class T>
T unpack(string &str)
{
  using namespace msgpack;
  unpacked result;
  istringstream ss(str);
  int len;
  string buff;
  ss >> len >> buff;
  unpack(&result, buff.c_str(), len);

  auto obj = result.get();
  return obj.as<T>();
}

int main(int argc, char *argv[]) {
  vector<float> t = {1., 2., 3., 4., 5.};
  string s;
  pack(t, s); 
  auto r = unpack<vector<float> >(s);
  for(auto & v : r)
    std::cout << v << std::endl;
  // vector<int> is right
  /* 
  vector<int> t = {1, 2, 3, 4, 5};
  string s;
  pack(t, s); 
  auto r = unpack<vector<int> >(s);
  for(auto & v : r)
    std::cout << v << std::endl;
  */
  return 0;
}

There is a strange bug that 'vector', 'vector', 'int', 'double' can work in the above encapsulation, while 'vector', 'vector' can not.

runtime error shows below:

terminate called after throwing an instance of 'msgpack::type_error'
what():  std::bad_cast

but in the below encapsulation, any type can work well:

template<class T>
void pack(T &t, msgpack::sbuffer sbuf) {
  pack(&sbuf, t);
}

template<class T>
T unpack(const msgpack::sbuffer & sbuf) {
  msgpack::unpacked msg;
  msgpack::unpack(&msg, sbuf.data(), sbuf.size());
  auto obj = msg.get();
  return obj.as<t>();
}

What's is the problem in my first code? Thanks!

1

There are 1 answers

0
xyz On

msgpack binary output can include null bytes, like it happens in your case. is […] << buff.data(); will only output first 4 bytes in your case.

To fix the error replace is << buff.size() << buff.data(); with is << buff.size() << string(buff.data(), buff.size());.