Created
December 8, 2021 10:12
-
-
Save nthery/e7cc8452d1fa0d4e94862166189d2081 to your computer and use it in GitHub Desktop.
Show how to serialize object to binary blob and restore it using Boost.Serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> | |
#include <iostream> | |
#include <boost/archive/binary_oarchive.hpp> | |
#include <boost/archive/binary_iarchive.hpp> | |
struct Foo { | |
int the_answer = 0; | |
double pi = 0.0; | |
template<class Archive> | |
void serialize(Archive &ar, const unsigned int /*version*/) { | |
(ar & the_answer); | |
(ar & pi); | |
} | |
}; | |
int main() { | |
// Serialize object into binary blob. | |
const Foo input_foo = { 42, 3.14 }; | |
std::ostringstream oss; | |
boost::archive::binary_oarchive oar(oss); | |
oar << input_foo; | |
const auto blob = oss.str(); | |
// Restore object saved into binary blob. | |
Foo output_foo; | |
std::istringstream iss(blob); | |
boost::archive::binary_iarchive iar(iss); | |
iar >> output_foo; | |
std::cout << output_foo.the_answer << ' ' << output_foo.pi << '\n'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment