boost serialization
Archive
An archive is a sequence of bytes that represented serialized C++ objects. Objects can be added to an archive to serialize them and then later loaded from the archive.
1. boost::archive::text_iarchive
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <fstream> using namespace boost::archive; void save()
{
std::ofstream file("archive.txt");
text_oarchive oa{file};
int i = ;
oa << i;
} void load()
{
std::ifstream file("archive.txt");
text_iarchive ia{file};
int i = ;
ia >> i;
std::cout << i << std::endl;
} int main()
{
save();
load();
return ;
}
The class boost::archive::text_oarchive serializes data as a text stream, and the class boost::archive::text_iarchive restores data from such a text stream. Constructors of archives expect an input or output stream as a parameter. The stream is used to serialize or restore data.
2. serializing with a stringstream
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive; std::stringstream ss; void save()
{
text_oarchive oa{ss};
int i = ;
oa << i;
} void load()
{
text_iarchive ia{ss};
int i = ;
ia >> i;
std::cout << i << std::endl;
} int main()
{
save();
load();
return ;
}
output: 1
3. user-defined types with a member function
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive; std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
int legs() const { return legs_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; void save()
{
text_oarchive oa(ss);
animal a{};
oa << a;
} void load()
{
text_iarchive ia(ss);
animal a;
ia >> a;
std::cout << a.legs() << std::endl;
} int main()
{
save();
load();
return ;
}
In order to serialize objects of user-defined types, you must define the member function serialize(). This function is called when the object is serialized to or restored from a byte stream.
serialize() is automatically called any time an object is serialized or restored.
4. serializing with a free-standing function and serializing strings
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <utility> using namespace boost::archive; std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs, std::string name) :
legs_{legs}, name_{std::move(name)} {}
int legs() const { return legs_; }
const std::string &name() const { return name_; } private:
friend class boost::serialization::access; template <typename Archive>
friend void serialize(Archive &ar, animal &a, const unsigned int version); int legs_;
std::string name_;
}; template <typename Archive>
void serialize(Archive &ar, animal &a, const unsigned int version)
{
ar & a.legs_;
ar & a.name_;
} void save()
{
text_oarchive oa{ss};
animal a{, "cat"};
oa << a;
} void load()
{
text_iarchive ia{ss};
animal a;
ia >> a;
std::cout << a.legs() << std::endl;
std::cout << a.name() << std::endl;
} int main()
{
save();
load();
return ;
}
a member variable of type std::string, in order to serialize this member variable. the header file boost/serialization/string.hpp must be included to provide the appropriate free-standing function serialize().
Pointers and References
1. serializing pointers
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream> std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_{legs} {}
int legs() const { return legs_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; void save()
{
boost::archive::text_oarchive oa(ss);
animal *a = new animal();
oa << a;
std::cout << std::hex << a << std::endl;
delete a;
} void load()
{
boost::archive::text_iarchive ia(ss);
animal *a;
ia >> a;
std::cout << std::hex << a << std::endl;
std::cout << std::dec << a->legs() << std::endl;
delete a;
} int main()
{
save();
load();
return ;
}
Boost.Serialization automatically serializes the object referenced by a and not the address of the object.
If the archive is restored, a will not necessarily contain the same address. A new object is created and its address is assigned to a instead. Boost.Serialization only guarantees that the object is the same as the one serialized, not that its address is the same.
2 serializing smart pointers
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive; std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
int legs() const { return legs_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; void save()
{
text_oarchive oa(ss);
boost::scoped_ptr<animal> a(new animal(4));
oa << a;
} void load()
{
text_iarchive ia(ss);
boost::scoped_ptr<animal> a;
ia >> a;
std::cout << a->legs() << std::endl;
} int main()
{
save();
load();
return ;
}
uses the smart pointer boost::scoped_ptr
to manage a dynamically allocated object of type animal
. Include the header file boost/serialization/scoped_ptr.hpp
to serialize such a pointer. To serialize a smart pointer of type boost::shared_ptr
, use the header file boost/serialization/shared_ptr.hpp
.
3. serializing references
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive; std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
int legs() const { return legs_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; void save()
{
text_oarchive oa(ss);
animal a();
animal &r = a;
oa << r;
} void load()
{
text_iarchive ia(ss);
animal a;
animal &r = a;
ia >> r;
std::cout << r.legs() << std::endl;
} int main()
{
save();
load();
}
Serialization of Class Hierarchy Objects
Derived classes must access the function boost::serialization::base_object() inside the member function serialize() to serialize objects based on class hierarchies. This function guarantees that inherited member variables of base classes are correctly serialized.
1. serializing derived classes correctly
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive;
std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
int legs() const { return legs_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; class bird : public animal
{
public:
bird() = default;
bird(int legs, bool can_fly) :
animal(legs), can_fly_{can_fly} {}
bool can_fly() const { return can_fly_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<animal>(*this);
ar & can_fly_;
} bool can_fly_;
}; void save()
{
text_oarchive oa(ss);
bird penguin(, false);
oa << penguin;
} void load()
{
text_iarchive ia(ss);
bird penguin;
ia >> penguin;
std::cout << penguin.legs() << '\n';
std::cout << std::boolalpha << penguin.can_fly() << '\n';
} int main()
{
save();
load();
return ;
}
Inherited member variables are serialized by accessing the base class inside the member function serialize()
of the derived class and calling boost::serialization::base_object()
. You must use this function rather than, for example, static_cast
because only boost::serialization::base_object()
ensures correct serialization
2. registering derived classes statically with BOOST_CLASS_EXPORT
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <iostream>
#include <sstream> using namespace boost::archive; std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
virtual int legs() const { return legs_; }
virtual ~animal() = default; private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; class bird : public animal
{
public:
bird() = default;
bird(int legs, bool can_fly) :
animal{legs}, can_fly_(can_fly) {}
bool can_fly() const { return can_fly_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<animal>(*this);
ar & can_fly_;
} bool can_fly_;
}; BOOST_CLASS_EXPORT(bird) void save()
{
text_oarchive oa(ss);
animal *a = new bird(, false);
oa << a;
delete a;
} void load()
{
text_iarchive ia(ss);
animal *a;
ia >> a;
std::cout << a->legs() << '\n';
delete a;
} int main()
{
save();
load();
return ;
}
To have Boost.Serialization recognize that an object of type bird
must be serialized, even though the pointer is of type animal*
, the class bird
needs to be declared. This is done using the macro BOOST_CLASS_EXPORT
, which is defined in boost/serialization/export.hpp
. Because the type bird
does not appear in the pointer definition, Boost.Serialization cannot serialize an object of type bird
correctly without the macro.
The macro BOOST_CLASS_EXPORT
must be used if objects of derived classes are to be serialized using a pointer to their corresponding base class. A disadvantage of BOOST_CLASS_EXPORT
is that, because of static registration, classes can be registered that may not be used for serialization at all.
3. register_type()
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <iostream>
#include <sstream> std::stringstream ss; class animal
{
public:
animal() = default;
animal(int legs) : legs_(legs) {}
virtual int legs() const { return legs_; }
virtual ~animal() = default; private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; } int legs_;
}; class bird : public animal
{
public:
bird() = default;
bird(int legs, bool can_fly) :
animal{legs}, can_fly_(can_fly) {}
bool can_fly() const { return can_fly_; } private:
friend class boost::serialization::access; template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<animal>(*this);
ar & can_fly_;
} bool can_fly_;
}; void save()
{
boost::archive::text_oarchive oa(ss);
oa.register_type<bird>();
animal *a = new bird(, false);
oa << a;
delete a;
} void load()
{
boost::archive::text_iarchive ia(ss);
ia.register_type<bird>();
animal *a;
ia >> a;
std::cout << a->legs() << std::endl;
delete a;
} int main()
{
save();
load();
return ;
}
The type to be registered is passed as a template parameter. Note that register_type()
must be called both in save()
and load()
.
The advantage of register_type()
is that only classes used for serialization must be registered. For example, when developing a library, one does not know which classes a developer may use for serialization later. While the macro BOOST_CLASS_EXPORT
makes this easy, it may register types that are not going to be used for serialization.
最新文章
- Linux初学:(二)Shell环境与命令基础
- wininet异步InternetReadFile和超时相关问题
- SpringMVC结合easyUI中datagird实现分页
- json的中括号和大括号的使用?
- quick-cocos2d-x之testlua之mainMenu.lua
- 数据类型转换的三种方式 Convert,parse和TryParse的解析
- Metadata Lock原理6
- 1-1 spring基础
- JDBC详解(汇总)
- 12.JavaScript字符串方法
- Multi-Projector Based Display Code ---- ModelViewer
- go日常问题记录
- 学习笔记DL008:概率论,随机变量,概率分布,边缘概率,条件概率,期望、方差、协方差
- 【下一代核心技术DevOps】:(七)持续集成Jenkins的应用(Aliyun Pipiline持续构建)
- Struts上传文件
- UVa 11542 Square (高斯消元)
- i=i+1,i+=1,i++哪个执行效率最高?为什么?
- 洛谷P4054 [JSOI2009]计数问题(二维树状数组)
- java学习记录--ThreadLocal使用案例
- 【差分约束系统/SPFA】POJ3169-Layout
热门文章
- margin/padding百分比值的计算
- 新建 SecondFragment 实现类
- linux ( CentOS 7)下Tengine(nginx)的安装与配置
- 123、TensorFlow的Job
- java切分查询数据库表
- Jenkins持续集成_01_Mac安装配置
- spring-第十一篇之SpEL表达式
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 (Treap+单调队列)
- 用php实现一个简单的爬虫,抓取电影网站的视频下载地址
- 最长公共上升子序列 (LIS+LCS+记录)