|
// Copyright This program is released into the public domain. |
|
|
|
#include <string> |
|
#include <vector> |
|
#include <iostream> |
|
#include <msgpack.hpp> |
|
#include "./msgio.h" |
|
|
|
class MyClass { |
|
private: |
|
std::string m_string; |
|
std::vector<int> m_int_vec; |
|
std::vector<std::string> m_string_vec; |
|
|
|
public: |
|
MyClass() { |
|
} |
|
|
|
MyClass(std::string a, |
|
const std::vector<int>& b, |
|
const std::vector<std::string>& c) { |
|
m_string = a; |
|
|
|
for (auto it = b.begin(); it != b.end(); ++it) { |
|
m_int_vec.push_back(*it); |
|
} |
|
|
|
for (auto it = c.begin(); it != c.end(); ++it) { |
|
m_string_vec.push_back(*it); |
|
} |
|
} |
|
|
|
friend std::ostream& operator<< (std::ostream& os, const MyClass& obj) { |
|
os << "MyClass(m_string: " << obj.m_string << ", m_int_vec: ["; |
|
|
|
for (auto it = obj.m_int_vec.begin(); it != obj.m_int_vec.end(); ++it) { |
|
if (it != obj.m_int_vec.begin()) { |
|
os << ", "; |
|
} |
|
|
|
os << *it; |
|
} |
|
|
|
os << "], m_string_vec: ["; |
|
|
|
for (auto it = obj.m_string_vec.begin(); |
|
it != obj.m_string_vec.end(); ++it) { |
|
if (it != obj.m_string_vec.begin()) { |
|
os << ", "; |
|
} |
|
|
|
os << *it; |
|
} |
|
|
|
os << "])"; |
|
|
|
return os; |
|
} |
|
|
|
MSGPACK_DEFINE(m_string, m_int_vec, m_string_vec); |
|
}; |
|
|
|
int main() { |
|
std::vector<int> int_vec; |
|
std::vector<std::string> string_vec; |
|
|
|
// MyClass obj1 |
|
int_vec.push_back(2); |
|
int_vec.push_back(4); |
|
string_vec.push_back("foo"); |
|
string_vec.push_back("bar"); |
|
MyClass obj1("obj1", int_vec, string_vec); |
|
|
|
// MyClass obj2 |
|
int_vec.push_back(8); |
|
string_vec.push_back("baz"); |
|
MyClass obj2("obj2", int_vec, string_vec); |
|
|
|
// MyClass vector: [obj1, obj2] |
|
std::vector<MyClass> my_class_vec; |
|
my_class_vec.push_back(obj1); |
|
my_class_vec.push_back(obj2); |
|
|
|
// pack |
|
msgpack::sbuffer buffer; |
|
msgpack::pack(buffer, my_class_vec); |
|
|
|
// save |
|
save_msgpack_sbuffer(buffer, __FILE__".msgpack"); |
|
|
|
// unpack |
|
msgpack::unpacked msg; |
|
msgpack::unpack(&msg, buffer.data(), buffer.size()); |
|
|
|
// convert |
|
msgpack::object obj = msg.get(); |
|
std::vector<MyClass> result_vec; |
|
obj.convert(&result_vec); |
|
|
|
// print |
|
for (auto it = result_vec.begin(); it != result_vec.end(); ++it) { |
|
std::cout << *it << std::endl; |
|
} |
|
|
|
return 0; |
|
} |