Boost serialization, class including pointer - c++

i'm currently working on boost serialization and I came into a moment when I can't really move forward. I have a class Order which holds pointers to Table and Waiter classes. I would like to add just that serialization for all the other classes works perfectly fine, its just order that makes problems. When I try to serialize Order in my main, I get an exemption:
Unhandled exception at 0x77A03FC8 in ConsoleApplication1.exe: Microsoft C++ exception: boost::archive::archive_exception at memory location 0x0081C42C.
and
+ e {m_buffer=0x0081c6f4 "unregistered class - derived class not registered or exported" code=unregistered_class (2) } const boost::archive::archive_exception &
Have you got an idea how to make this connection work properly in serialization call?
Order.h
#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include "Meal.h"
#include "Table.h"
#include "Waiter.h"
using namespace std;
enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };
class Order
{
private:
friend std::ostream & operator<<(std::ostream &os, const Order &tb);
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int /* file_version */) {
ar & ID & TimeAndDate & status & table & waiter & meals;
};
protected:
string TimeAndDate;
Status status;
Table * table;
Waiter * waiter;
string ID;
public:
typedef Meal * Meal_Pointer;
list<Meal_Pointer> meals;
Order(string,Table*,Waiter*);
Order():table(new Table),waiter(new Waiter) {};
string CurrentDateAndTime();
void UpdateStatus(Status);
void AssignWaiter(Waiter*);
void AssignTable(Table*);
void AddItem(Meal*);
void append(Meal *_bs)
{
meals.insert(meals.end(), _bs);
}
};
Order.cpp
#include "Order.h"
Order::Order(string id, Table* tab, Waiter* wait)
:ID(move(id)), table(tab), waiter(wait), status(ASSIGNED),
std::ostream & operator<<(std::ostream &os, const Order &tb)
{
//std::list<Meal *>::const_iterator it;
// note: we're displaying the pointer to permit verification
// that duplicated pointers are properly restored.
return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << ' ' << tb.table << ' ' << tb.waiter << ' ' ;
/*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}*/
}
template void Order::serialize<boost::archive::text_iarchive>(
boost::archive::text_iarchive & ar,
const unsigned int file_version
);
template void Order::serialize<boost::archive::text_oarchive>(
boost::archive::text_oarchive & ar,
const unsigned int file_ver
Functions
void save_order(const Order &s, const char * filename) {
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
int main ()
{
filename = "order.txt";
save_order(myOrder, filename.c_str());
Order neworder;
restore_order(neworder, filename.c_str());}

You don't show the relevant code.
The code you do show is messy and broken.
Here I've fixed it enough so it can compile:
Live On Coliru
Obviously, no problem.
// Order.h
//#pragma once
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <ctime>
#include <fstream>
#include <iostream>
using namespace std;
struct Table {};
struct Waiter {};
struct Meal {};
namespace boost { namespace serialization {
template <typename Ar> void serialize(Ar&, Table&, unsigned) { }
template <typename Ar> void serialize(Ar&, Waiter&, unsigned) { }
template <typename Ar> void serialize(Ar&, Meal&, unsigned) { }
} }
enum Status { ASSIGNED, PROCESSED, CONFIRMED, SUBMITTED, BEING_PREPARED, READY, SERVED, CANCELED, PAID, FINALIZED };
class Order {
private:
friend std::ostream &operator<<(std::ostream &os, const Order &tb);
friend class boost::serialization::access;
template <class Archive> void serialize(Archive &ar, const unsigned int /* file_version */) {
ar &ID &TimeAndDate &status &table &waiter &meals;
}
protected:
string TimeAndDate;
string ID;
Table *table;
Waiter *waiter;
Status status;
typedef Meal *Meal_Pointer;
list<Meal_Pointer> meals;
public:
Order(string, Table *, Waiter *);
Order() : table(new Table), waiter(new Waiter){};
string CurrentDateAndTime();
void UpdateStatus(Status);
void AssignWaiter(Waiter *);
void AssignTable(Table *);
void AddItem(Meal *);
void append(Meal *_bs) { meals.insert(meals.end(), _bs); }
};
// Order.cpp
//#include "Order.h"
Order::Order(string id, Table *tab, Waiter *wait) : ID(move(id)), table(tab), waiter(wait), status(ASSIGNED) {}
std::ostream &operator<<(std::ostream &os, const Order &tb) {
return os << ' ' << tb.ID << ' ' << tb.TimeAndDate << ' ' << tb.status << ' ' << tb.table << ' ' << tb.waiter
<< ' ';
/*for (it = tb.meals.begin(); it != tb.meals.end(); it++) {
os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
}*/
}
template void Order::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive &ar, const unsigned int file_version);
template void Order::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive &ar, const unsigned int file_ver);
void save_order(const Order &s, const char *filename) {
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
void restore_order(Order &s, const char *filename) {
std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
ia >> s;
}
int main() {
std::string const filename = "order.txt";
{
Order myOrder;
save_order(myOrder, filename.c_str());
}
std::cout << std::ifstream(filename).rdbuf() << "\n";
{
Order neworder;
restore_order(neworder, filename.c_str());
}
}
Hints
What you seem to be doing is serializing polymorphic instances of classes (e.g. derived from Table, Waiter or Meal). In such cases you need to register the derived classes and serialize the base properly:
Common confusions with serializing polymorphic types

Related

boost::serialization of static array / archive exception on restore

The following code compiles and seems to serialize properly (that is, the static is saved only once apparently). However, it has an 'input stream error' exception on restore:
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/array.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <iostream>
#include <fstream>
#include <array>
class SA {
std::array<char, 1024*1024> sbuf;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(sbuf.data(), sbuf.size());
};
};
BOOST_CLASS_IMPLEMENTATION(SA, boost::serialization::object_serializable); // serialization_level
BOOST_CLASS_TRACKING(SA, boost::serialization::track_always); // tracking_level
class Foo {
char buf[1024];
inline static SA sxbuf;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(buf, sizeof(buf));
ar & sxbuf;
};
};
class FooList {
std::array<Foo, 100> fool;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int ver) {
ar & boost::serialization::make_array(fool.data(), fool.size());
};
};
int
main()
{
const std::string filename{"foo.sav"};
FooList x;
std::ofstream out{filename, std::ios::binary};
boost::archive::binary_oarchive oa(out);
oa << x;
std::cout << "Saved\n";
std::ifstream ifs{filename};
boost::archive::binary_iarchive ia(ifs);
if (ifs.fail()) {
std::cerr << "couldn't open input file " << filename << "\n";
return 1;
}
ia >> x; // gives exception
std::cout << "Restored\n";
return 0;
}
The output is as follows:
Saved
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
(I tried this on godbolt too, but -lboost_serialization didn't seem to work, so it wouldn't link properly... possibly my error there).
Any insight much appreciated...
This opens an archive before the writing end is finalized:
std::ofstream out{filename, std::ios::binary};
boost::archive::text_oarchive oa(out);
oa << x;
std::cout << "Saved\n";
std::ifstream ifs{filename};
Also ifs is missing the ios::binary flag.
Other than that, you can
drop make_array,
prefer std::array over T[] and
default the serialization level.
Live On Coliru
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <fstream>
#include <iostream>
class SA {
std::array<char, 1024 * 1024> sbuf;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &sbuf; }
};
BOOST_CLASS_TRACKING(SA, boost::serialization::track_always) // tracking_level
class Foo {
std::array<char, 1024> buf;
inline static SA sxbuf;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &buf &sxbuf; }
};
class FooList {
std::array<Foo, 100> fool;
friend class boost::serialization::access;
template <class Ar> void serialize(Ar &ar, unsigned) { ar &fool; }
};
int main() {
const std::string filename{"foo.sav"};
{
std::ofstream out(filename, std::ios::binary);
boost::archive::binary_oarchive oa(out);
FooList x{};
oa << x;
std::cout << "Saved\n";
}
{
std::ifstream ifs(filename, std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
FooList x{};
ia >> x; // gives exception
std::cout << "Restored\n";
}
}
Prints
Saved
Restored

public inheritance of std::fstream and overloading operator<< for char* not actually writing to the Output file

#include <iostream>
#include <fstream>
#include <string>
class SychronizedFileWriter : public std::fstream {
//std::mutex _mutex;
public:
SychronizedFileWriter(std::string&& filePath);
virtual ~SychronizedFileWriter();
bool is_open() const;
// SychronizedFileWriter& operator<<(const char* s);
// SychronizedFileWriter& operator<<(SychronizedFileWriter& sfw, const signed char* s);
// SychronizedFileWriter& operator<<(SychronizedFileWriter& sfw, const unsigned char* s);
};
SychronizedFileWriter::SychronizedFileWriter(std::string&& filePath)
{
std::fstream::open(filePath, std::ios_base::app);
}
SychronizedFileWriter:: ~SychronizedFileWriter()
{
std::fstream::close();
}
bool
SychronizedFileWriter::is_open() const
{
return std::fstream::is_open();
}
template<typename T, typename S>
T& operator<<(T& fs, const S* s)
{
std::cout << "im here\n";
{
// Take mutex lock
fs.std::fstream::operator<<(s);
}
return fs;
}
int main(int argc, char *argv[])
{
std::ios_base::sync_with_stdio(true);
SychronizedFileWriter sfw(std::string("./test.txt"));
std::cout << "open: " << std::boolalpha << sfw.is_open();
sfw << "Hello, Worked!!!!" << "\n";
exit(0);
}
Code compiled successfully. However, "text.txt" does not contain the string "Hello, Worked!!!!" but it is empty. My purpose is just to use this class as a wrapper to operator<< so that i can use mutex to synchronize writing to the same file by different threads.
Thanks in Advance and highly appreciate your help.

Serializing derived classes from an abstract class using boost::serialization

I am trying to serialize derived classes from an abstract class and I am getting some (obvious) errors.
Here is the code (ran using g++ -std=c++17 XXXX.cpp -Wall -larmadillo -lboost_serialization):
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/unique_ptr.hpp>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <vector>
class Car {
public:
//Car() = default;
//virtual double getInfo() = 0;
virtual char const* type() const = 0;
virtual ~Car() = default;
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {};
};
class Audi : public Car {
public:
char const* type() const override { return "Audi"; }
//double getInfo() override { return 2.2; }
Audi(std::string owner1, int hp1, std::string second_owner1, std::string country1)
{
owner = owner1;
hp = hp1;
second_owner = second_owner1;
country = country1;
}
Audi() = default;
std::string owner;
int hp{};
std::string second_owner;
std::string country;
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & boost::serialization::base_object<Car>(*this); //https://theboostcpplibraries.com/boost.serialization-class-hierarchies
ar & owner;
ar & hp;
ar & second_owner;
ar & country;
}
};
BOOST_CLASS_EXPORT(Audi);
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Car); //Tell Boost that Car is abstract
int main() {
std::string str;
{
std::unique_ptr<Car> audi = std::make_unique<Audi>("Wilma", 3, "Rene", "Argentina");
audi->type();
std::stringstream strs;
boost::archive::binary_oarchive ar(strs);
ar& audi;
str = strs.str();
}
{
std::unique_ptr<Car> audi; //= std::make_unique<Audi>();
std::stringstream strs(str);
boost::archive::binary_iarchive ar(strs);
ar& audi;
//dynamic_cast<Audi>(audi); Tried using casting but am unfamiliar with it -- perhaps this is the issue?
std::cout << "audi: hp=" << audi->hp << " owner=" << audi->owner <<"\n";
}
}
and I am getting the errors:
audi_seri.cpp: In function ‘int main()’:
audi_seri.cpp:84:36: error: cannot dynamic_cast ‘audi’ (of type ‘class std::unique_ptr<Car>’) to type ‘class Audi’ (target is not pointer or reference)
dynamic_cast<Audi>(audi);
^
audi_seri.cpp:86:47: error: ‘class Car’ has no member named ‘hp’
std::cout << "audi: hp=" << audi->hp << " owner=" << audi->owner <<"\n";
^~
audi_seri.cpp:86:72: error: ‘class Car’ has no member named ‘owner’
std::cout << "audi: hp=" << audi->hp << " owner=" << audi->owner <<"\n";
Now, I know why this is happening -- Car doesn't have these variables. But how can I solve this to where the solution can work for many derived classes that could have different members? Perhaps to solve this I have to use casting?
To give more insight as to why I am trying to do this. I initially wanted to have void serialize() in Car as a virtual function but that doesn't work with templates. And hence I came to the above code with the help of https://theboostcpplibraries.com/boost.serialization-class-hierarchies
Here is the old code with with void serialize as a virtual function:
class Car {
public:
virtual double getInfo() = 0;
private:
template <typename Archive> //This is the problem I am trying to solve: templates cannot be virtual
virtual void serialize(Archive& ar, unsigned int version) = 0;
};
class Audi : public Car {
public:
char const* type() const override { return "Audi"; }
//double getInfo() override { return 2.2; }
Audi(std::string owner1, int hp1, std::string second_owner1, std::string country1)
{
owner = owner1;
hp = hp1;
second_owner = second_owner1;
country = country1;
}
Audi() = default;
std::string owner;
int hp{};
std::string second_owner;
std::string country;
private:
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & owner;
ar & hp;
ar & second_owner;
ar & country;
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Car); //Tell Boost that Car is abstract
int main() {
std::string str;
{
std::unique_ptr<Car> audi = std::make_unique<Audi>("Wilma", 3, "Rene", "Argentina");
std::stringstream strs;
boost::archive::binary_oarchive ar(strs);
ar& audi;
str = strs.str();
}
{
std::unique_ptr<Car> audi; //= std::make_unique<Audi>();
std::stringstream strs(str);
boost::archive::binary_iarchive ar(strs);
ar& audi;
//dynamic_cast<Audi>(audi);
std::cout << "audi: hp=" << audi->hp << audi->hp<< " owner=" << "\n";
}
}

Boost Serialization throws exception while reading from binary

I have found this code and trying to modify it for binary object serialization on visual studios. But on doing so it throws exception
"Unhandled exception at 0x00007FFF269954D8 in Boost_Serialization.exe: Microsoft C++ exception: std::length_error at memory location 0x000000829D94F7B0."
Not sure what's wrong.
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>
#include<boost/serialization/binary_object.hpp>
class Info
{
private:
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
//template<class Archive>
//void save(Archive & ar, const unsigned int version) const { ar & filenames;}
template<class Archive>
void load(Archive & ar, const unsigned int version) { ar & filenames; }
BOOST_SERIALIZATION_SPLIT_MEMBER()
std::vector<std::string> filenames;
public:
void AddFilename(const std::string& filename);
void Print() const;
};
void Info::Print() const { std::copy(filenames.begin(),filenames.end(),std::ostream_iterator<std::string>(std::cout, "\n")); }
void Info::AddFilename(const std::string& filename) { filenames.push_back(filename); }
int main(int argc, char** argv)
{
std::vector<Info> infs;
Info info1, info2;
info1.AddFilename("ThisFile.txt");
info2.AddFilename("ThatFile.txt");
info2.AddFilename("OtherFile.txt");
info2.AddFilename("ABC");
info2.AddFilename("123");
info2.AddFilename("XYZ");
infs.push_back(info1);
infs.push_back(info2);
// Save filename data contained in Info object
{
std::ofstream Obj_ofstream("data.dat", std::ios::binary);
boost::archive::binary_oarchive op_archive(Obj_ofstream);
op_archive << boost::serialization::make_binary_object(&infs, sizeof(infs));
//Obj_ofstream.close();
}
// Restore from saved data and print to verify contents
std::vector<Info> restored_info;
{
std::ifstream Obj_ifstream("data.dat", std::ios::binary);
boost::archive::binary_iarchive ip_archive(Obj_ifstream);
ip_archive >> restored_info;
//Obj_ifstream.close();
}
//std::vector<Info>::const_iterator it = restored_info.begin();
//for (; it != restored_info.end(); ++it)
//{
// Info info = *it;
// info.Print();
//}
std::cout << "Testing : " << std::endl;
return 0;
You use BOOST_SERIALIZATION_SPLIT_MEMBER() and do not implement save method. However, you do not need to do that. Remove splitting and implement general void serialize(Archive & ar, const unsigned int version) method (note to include new header for vector serialization). It will look like this and run like a charm:
#include <iostream>
#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>
#include<boost/serialization/binary_object.hpp>
#include<boost/serialization/vector.hpp>
class Info
{
private:
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
//template<class Archive>
//void save(Archive & ar, const unsigned int version) const { ar & filenames;}
template<class Archive>
void serialize(Archive & ar, const unsigned int version) { ar & filenames; }
std::vector<std::string> filenames;
public:
void AddFilename(const std::string& filename);
void Print() const;
};
void Info::Print() const { std::copy(filenames.begin(), filenames.end(), std::ostream_iterator<std::string>(std::cout, "\n")); }
void Info::AddFilename(const std::string& filename) { filenames.push_back(filename); }
int main(int argc, char** argv)
{
std::vector<Info> infs;
Info info1, info2;
info1.AddFilename("ThisFile.txt");
info2.AddFilename("ThatFile.txt");
info2.AddFilename("OtherFile.txt");
info2.AddFilename("ABC");
info2.AddFilename("123");
info2.AddFilename("XYZ");
infs.push_back(info1);
infs.push_back(info2);
// Save filename data contained in Info object
{
std::ofstream Obj_ofstream("data.dat", std::ios::binary);
boost::archive::binary_oarchive op_archive(Obj_ofstream);
op_archive << infs;
//Obj_ofstream.close();
}
// Restore from saved data and print to verify contents
std::vector<Info> restored_info;
{
std::ifstream Obj_ifstream("data.dat", std::ios::binary);
boost::archive::binary_iarchive ip_archive(Obj_ifstream);
ip_archive >> restored_info;
//Obj_ifstream.close();
}
std::vector<Info>::const_iterator it = restored_info.begin();
for (; it != restored_info.end(); ++it)
{
Info info = *it;
info.Print();
}
std::cout << "Testing : " << std::endl;
system("PAUSE");
return 0;
}

looking for deserialization example in boost

I'm really not sure how do i go/start for boost deserialization. Below is sample code for which want to know how do the boost deserialization code would be.
#include <boost/serialization/access.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/smart_ptr/make_shared.hpp>
namespace mydata
{
struct MyInfo
{
std::string info = "extra info";
MyInfo(std::string info) : info(std::move(info)) {}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int /*version*/)
{
ar & info;
}
};
struct MyData
{
std::string name;
std::string type;
boost::shared_ptr<MyInfo> myref;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int /*version*/)
{
ar & name;
ar & type;
ar & myref;
}
};
}
int main()
{
using namespace mydata;
MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };
boost::archive::text_oarchive oa(std::cout);
oa << data;
}
There is also link similar to deserialization but was not clear on
Boost deserialize a derived class to base class pointer .
I really don't know much.. it would really helpful..if i get link on boost deserialization also if there exist.
You were almost there:
int main()
{
using namespace mydata;
MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };
std::ostringstream oss;
{
boost::archive::text_oarchive oa(oss);
oa << data;
}
std::istringstream iss(oss.str());
{
boost::archive::text_iarchive ia(iss);
ia >> data;
}
}
In fact, you could use std::stringstream for both the input and output, but for purity I showed the symmetric approaches (which does redundant copying).
You'll need
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
and for deserialization your classes need to be defaultconstructible:
MyInfo(std::string info = "") : info(std::move(info)) {}
(unrelated warning: do not use std::string info = {} here because that triggers a compiler bug in MSVC)
Here's a fully working sample that shows that the deserialized object has the same data: Live On Coliru
#include <boost/serialization/access.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <sstream>
#include <boost/archive/text_iarchive.hpp>
namespace mydata
{
struct MyInfo
{
std::string info = "extra info";
MyInfo(std::string info = "") : info(std::move(info)) {}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int /*version*/)
{
ar & info;
}
};
struct MyData
{
std::string name;
std::string type;
boost::shared_ptr<MyInfo> myref;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int /*version*/)
{
ar & name;
ar & type;
ar & myref;
}
};
}
int main()
{
using namespace mydata;
std::ostringstream oss;
{
MyData data { "this is a name", "this is a type", boost::make_shared<MyInfo>("this is info") };
boost::archive::text_oarchive oa(oss);
oa << data;
}
MyData cloned;
std::istringstream iss(oss.str());
{
boost::archive::text_iarchive ia(iss);
ia >> cloned;
}
// check equality
{
std::ostringstream oss2;
boost::archive::text_oarchive oa(oss2);
oa << cloned;
std::cout << oss.str() << "\n";
std::cout << oss2.str() << "\n";
}
}
Output:
22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0
0 12 this is info
22 serialization::archive 10 0 0 14 this is a name 14 this is a type 0 1 2 1 0
0 12 this is info