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.

boost serialization的更多相关文章

  1. 如何用boost::serialization去序列化派生模板类(续)

    在 如何用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类, 在写測试用例时一直出现编译错误,调了非常久也没跳出来,今天偶然试了一下...竟然调了出来. ...

  2. 最经常使用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    导读 1. 什么是序列化? 2. 为什么要序列化?优点在哪里? 3. C++对象序列化的四种方法 4. 最经常使用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序猿在编写应用程序的时候往往须 ...

  3. 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)

    导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...

  4. boost::serialization 用基类指针转存派生类(错误多多,一波三折)

    boost::serialization 也支持c++的多态,这样我们就能够通过使用基类的指针来转存派生类, 我们接着上一篇( boost::serialization(2)序列化基类 )的样例来看: ...

  5. :“boost/serialization/string.hpp”: No such file or directory 错误

    主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html

  6. Boost的Serialization和SmartPoint搭配使用

    准确来说,这篇博文并不是译文,而是一篇某个网页中代码改写而来.原文章中的代码存在几处严重错误,网页又不提供留言功能(不是没有而是一个没有留言功能的留言板).4年过去了,作者对这些错误不更正让人无法接受 ...

  7. 【boost】使用serialization库序列化子类

    boost.serialization库是一个非常强大又易用的序列化库,用于对象的保存与持久化等. 使用base_object可以在序列化子类的同时也序列化父类,以此获得足够的信息来从文件或网络数据中 ...

  8. C++ | boost库 类的序列化

    是的,这是今年的情人节,一篇还在研究怎么用的文章,文结的时候应该就用成功了. 恩,要有信心 神奇的分割线 不知何时装过boost库的header-only库, 所以ratslam中的boost是可以编 ...

  9. Linux上安装使用boost入门指导

    Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...

随机推荐

  1. Emmet基本使用教程

    转载来自:http://www.iteye.com/news/27580 Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语 ...

  2. 【easyui-combobox】下拉菜单自动补全功能,Ajax获取远程数据源

    这个是针对easyUI的下拉菜单使用的,Ajax获取远程数据源 HTML 页面 <input id="uname" name="uname" class= ...

  3. EZOJ #387字符串

    分析 似乎ttl的模拟赛t3总是折半搜索? 先把所有串转化为每个字母的0/1状态 之后我们将所有字符串分为两半 分别枚举状态 我们发现只有左右两边的字母状态相等才能保证这个集合合法 所以我们在搜左半边 ...

  4. crazyflie四轴飞行器

    源地址:http://www.bitcraze.se/2013/02/pre-order-has-started/ Crazyflie是一个开源的纳米四旋翼 来几张靓照 开发平台是开源的,所以原理图和 ...

  5. CentOS 7命令行安装GNOME、KDE图形界面(成功安装验证)

    来源:cnblogs.com/Amedeo  作者:Amedeo 正文 CentOS 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方 ...

  6. IsAjaxRequest

    具体来说,IsAjaxRequest代码可以分解为以下功能: public static bool IsAjaxRequest(this HttpRequestBase request) { if ( ...

  7. ECMAScript 2015 迭代器协议:实现自定义迭代器

    迭代器协议定义了一种标准的方式来产生一个有限或无限序列的值,并且当所有的值都已经被迭代后,就会有一个默认的返回值. 当一个对象只有满足下述条件才会被认为是一个迭代器:它实现了一个 next() 的方法 ...

  8. HDU 6583 Typewriter 题解

    ——本题来自杭电多校第一场 题意:给定一个字符串,主角需要用打字机将字符串打出来,每次可以: 1.花费p来打出任意一个字符 2.花费q来将已经打出的某一段(子串)复制到后面去 对于这种最优化的问题,我 ...

  9. 爬虫(五)—— 解析库(二)beautiful soup解析库

    目录 解析库--beautiful soup 一.BeautifulSoup简介 二.安装模块 三.Beautiful Soup的基本使用 四.Beautiful Soup查找元素 1.查找文本.属性 ...

  10. nodejs基础-nvm和npm

    nvm npm 更新 npm install npm@latest -g 本地安装 npm install 包名称 require(”包名“) 全局安装 npm install 包名 -g 可以直接作 ...