#include <iostream> #include <Windows.h> #include <cereal/types/memory.hpp> #include <cereal/archives/binary.hpp> #include <cereal/archives/xml.hpp> #include <cereal/archives/json.hpp> #include <fstream>
using namespace std;
class A { public: int value1; string value2; float value3; double value4; char value5[20];
A(int v1,string v2,float v3,double v4,string v5) {; value1 = v1; value2 = v2; value3 = v3; value4 = v4; strcpy(value5, v5.c_str()); } A(){ value1 = 0; value2 = ""; value3 = 0; value4 = 0; strcpy(value5, ""); }
template<class Archive> void serialize(Archive& archive) { archive(value1, value2, value3, value4, value5); } };
int main() {
{ std::ofstream os_xml("my.xml"); cereal::XMLOutputArchive archive_xml_1(os_xml); A a_xml_1(1, "2", 3, 4, "5"); archive_xml_1( cereal::make_nvp("a_xml.v1", a_xml_1.value1), cereal::make_nvp("a_xml.v2", a_xml_1.value2), cereal::make_nvp("a_xml.v3", a_xml_1.value3), cereal::make_nvp("a_xml.v4", a_xml_1.value4), cereal::make_nvp("a_xml.v5", a_xml_1.value5) ); }
{ std::ifstream is_xml("my.xml"); cereal::XMLInputArchive archive_xml_2(is_xml); A a_xml_2; archive_xml_2(a_xml_2.value1, a_xml_2.value2, a_xml_2.value3, a_xml_2.value4, a_xml_2.value5); cout << "a_xml.value1 = " << a_xml_2.value1 << "\n" << "a_xml.value2 = " << a_xml_2.value2 << "\n" << "a_xml.value3 = " << a_xml_2.value3 << "\n" << "a_xml.value4 = " << a_xml_2.value4 << "\n" << "a_xml.value5 = " << a_xml_2.value5 << endl; }
{ std::ofstream os_json("my.json"); cereal::JSONOutputArchive archive_json_1(os_json); A a_json_1(1, "2", 3, 4, "5"); archive_json_1( cereal::make_nvp("a_json.v1", a_json_1.value1), cereal::make_nvp("a_json.v2", a_json_1.value2), cereal::make_nvp("a_json.v3", a_json_1.value3), cereal::make_nvp("a_json.v4", a_json_1.value4), cereal::make_nvp("a_json.v5", a_json_1.value5) ); }
{ std::ifstream is_json("my.json"); cereal::JSONInputArchive archive_json_2(is_json); A a_json_2; archive_json_2(a_json_2.value1, a_json_2.value2, a_json_2.value3, a_json_2.value4, a_json_2.value5); cout << "a_json.value1 = " << a_json_2.value1 << "\n" << "a_json.value2 = " << a_json_2.value2 << "\n" << "a_json.value3 = " << a_json_2.value3 << "\n" << "a_json.value4 = " << a_json_2.value4 << "\n" << "a_json.value5 = " << a_json_2.value5 << endl; }
{ std::ofstream os_bit("my.binary", std::ios::binary); cereal::BinaryOutputArchive archive_bit_1(os_bit); A a_bit_1(1, "2", 3, 4, "5"); archive_bit_1( cereal::make_nvp("a_bit.v1", a_bit_1.value1), cereal::make_nvp("a_bit.v2", a_bit_1.value2), cereal::make_nvp("a_bit.v3", a_bit_1.value3), cereal::make_nvp("a_bit.v4", a_bit_1.value4), cereal::make_nvp("a_bit.v5", a_bit_1.value5) ); }
{ std::ifstream is_bit("my.binary", std::ios::binary); cereal::BinaryInputArchive archive_bit_2(is_bit); A a_bit_2; archive_bit_2(a_bit_2.value1, a_bit_2.value2, a_bit_2.value3, a_bit_2.value4, a_bit_2.value5); cout << "a_bit.value1 = " << a_bit_2.value1 << "\n" << "a_bit.value2 = " << a_bit_2.value2 << "\n" << "a_bit.value3 = " << a_bit_2.value3 << "\n" << "a_bit.value4 = " << a_bit_2.value4 << "\n" << "a_bit.value5 = " << a_bit_2.value5 << endl; }
getchar(); return 0; }
|