| 1 | #include <fstream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/serialization/serialization.hpp>
|
|---|
| 4 |
|
|---|
| 5 | #include <boost/archive/text_oarchive.hpp>
|
|---|
| 6 | #include <boost/archive/text_iarchive.hpp>
|
|---|
| 7 | #include <boost/archive/xml_oarchive.hpp>
|
|---|
| 8 | #include <boost/archive/xml_iarchive.hpp>
|
|---|
| 9 |
|
|---|
| 10 | #include <boost/serialization/string.hpp>
|
|---|
| 11 |
|
|---|
| 12 | #include <boost/serialization/access.hpp>
|
|---|
| 13 | #include <boost/serialization/nvp.hpp>
|
|---|
| 14 |
|
|---|
| 15 | struct MyString {
|
|---|
| 16 | std::string s_;
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | namespace boost {
|
|---|
| 21 | namespace serialization {
|
|---|
| 22 |
|
|---|
| 23 | template<class Archive>
|
|---|
| 24 | void
|
|---|
| 25 | serialize(Archive& ar, MyString& ms, unsigned int const /*version*/)
|
|---|
| 26 | {
|
|---|
| 27 | ar & ms.s_;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | } // namespace serialization
|
|---|
| 31 | } // namespace boost
|
|---|
| 32 |
|
|---|
| 33 | #if 1
|
|---|
| 34 | typedef boost::archive::xml_oarchive oarchive;
|
|---|
| 35 | typedef boost::archive::xml_iarchive iarchive;
|
|---|
| 36 | //BOOST_CLASS_IS_WRAPPER(std::string) // Why is it required?
|
|---|
| 37 | #else
|
|---|
| 38 | typedef boost::archive::text_oarchive oarchive;
|
|---|
| 39 | typedef boost::archive::text_iarchive iarchive;
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | class Model {
|
|---|
| 44 | friend class boost::serialization::access;
|
|---|
| 45 | template<class Archive>
|
|---|
| 46 | void serialize(Archive& ar, unsigned int const /*version*/) {
|
|---|
| 47 | ar & BOOST_SERIALIZATION_NVP(ms_);
|
|---|
| 48 | }
|
|---|
| 49 | MyString ms_;
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | int main()
|
|---|
| 53 | {
|
|---|
| 54 | {
|
|---|
| 55 | Model m;
|
|---|
| 56 | std::ofstream ofs("foo.xml");
|
|---|
| 57 | oarchive oa(ofs);
|
|---|
| 58 | oa << boost::serialization::make_nvp("Model", m);
|
|---|
| 59 | }
|
|---|
| 60 | {
|
|---|
| 61 | Model m;
|
|---|
| 62 | std::ifstream ifs("foo.xml");
|
|---|
| 63 | iarchive ia(ifs);
|
|---|
| 64 | ia >> boost::serialization::make_nvp("Model", m);
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|