A C++ 17 msgpack implementation
A simple C++17 (only) compatible msgpack implementation.
vectors
, maps
, tuple
and std::string
, double
, int
, char
) packed in 6.56 seconds on an i7-7700HQ
Compile and run test.cpp to run a sample benchmark ~ 100 - 250 MB, or run the hello world below.
#include <iostream>
#include <tuple>
#include <map>
#include <vector>
#include <string>
#include "msgpack.hpp"
int main() {
msgpack_byte::container dest; // byte container holding the packed bytes
// data generation
std::tuple <char, unsigned int, double, string,
std::vector<unsigned int>, std::map<std::string, uint64_t> > original, unpacked; // an example container to pack
std::vector<unsigned int> vec{ 1, 2, 3, 4, 5 };
std::string abc = "test string";
std::map<string, uint64_t> cde;
cde.insert(make_pair(string("abc"), 4142342342342343));
cde.insert(make_pair(string("cde"), 5));
cde.insert(make_pair(string("def"), 11231233));
original = std::make_tuple('a', 10, 0.333333333333333, abc, vec, cde);
msgpack::pack(original, dest); // pass the container to be packed and the byte container
msgpack::unpack(unpacked, dest); // pass container to parse into and the byte container
std::cout << msgpack_byte::to_stringstream(dest).str() << std::endl; // return a stringstream of the packed data (in hex)
std::cout << msgpack_byte::to_string(dest) << std::endl; // return a string
std::cout << "Packed size: " << dest.size() << std::endl;
return 0;
}
Compile with different #define values to change performance
#define lenient_size
an integer value after which garbage collection trims extra memory for msgpack_byte::container
default 1000
#define compression_percent
a float value to with which memory preallocation is adjust (to accomodate msgpack’s formatting) default 1.1
#define doubling_strategy
define this without value to opt for doubling of byte container instead of growing by factor of 1.1