用基类的指针去转存派生类时除了上一篇boost::serialization
用基类指针转存派生类(错误多多,一波三折)
之外。还有还有一种更简单的方法:

用BOOST_CLASS_EXPORT宏。

以下我们来分析如何用BOOST_CLASS_EXPORT来实现

用基类的指针去转存派生类。

首先来看前面实例的一段代码:

void save()
{
std::ofstream ofs("t7.xml");
boost::archive::xml_oarchive oa(ofs);
student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);
oa << BOOST_SERIALIZATION_NVP(sdinfo);//#1
delete sdinfo;
}

当程序运行到#1时就会抛出异常:boost::archive::archive_exception at memory location 0x0017eb30...

boost文档解释是派生类没有实例化(这里是个人理解。。

。“实例化”究竟什么意思也不太理解。。。)。

当我们在#1前面加上注冊的代码时

oa.template register_type<middle_student>(NULL);

实际上就相当于“实例化”。看看register_type的实现代码:

 template<class T>
const basic_pointer_oserializer *
register_type(const T * = NULL){
const basic_pointer_oserializer & bpos =
boost::serialization::singleton<
pointer_oserializer<Archive, T>
>::get_const_instance();
this->This()->register_basic_serializer(bpos.get_basic_serializer());
return & bpos;
}

代码大概就是用单件模式申请一个对象的const指针,预计这个实例化就是为T申请内存。

然后看看BOOST_CLASS_EXPORT宏

#define BOOST_CLASS_EXPORT(T)                   \
BOOST_CLASS_EXPORT_GUID( \
T, \
BOOST_PP_STRINGIZE(T) \
) \

实际上是BOOST_CLASS_EXPORT_GUID宏的定义。继续看看这个宏

#define BOOST_CLASS_EXPORT_GUID(T, K)                                  \
BOOST_CLASS_EXPORT_KEY2(T, K) \
BOOST_CLASS_EXPORT_IMPLEMENT(T) \

原来这个宏展开式两个宏的定义。先看BOOST_CLASS_EXPORT_KEY2(T,K)这个宏

#define BOOST_CLASS_EXPORT_KEY2(T, K)          \
namespace boost { \
namespace serialization { \
template<> \
struct guid_defined< T > : boost::mpl::true_ {}; \
template<> \
inline const char * guid< T >(){ \
return K; \
} \
} /* serialization */ \
} /* boost */ \

这个宏实际上做了一件这种事:返回了一个唯一标记T的const char*字符串。

接下看看看BOOST_CLASS_EXPORT_IMPLEMENT(T)这个宏:

#define BOOST_CLASS_EXPORT_IMPLEMENT(T)                      \
namespace boost { \
namespace archive { \
namespace detail { \
namespace extra_detail { \
template<> \
struct init_guid< T > { \
static guid_initializer< T > const & g; \
}; \
guid_initializer< T > const & init_guid< T >::g = \
::boost::serialization::singleton< \
guid_initializer< T > \
>::get_mutable_instance().export_guid(); \
}}}} \

看看这段代码是不是和register_type实现的代码非常类似:用单件模式返回一个指针。

这就验证了“实例化”事实上就是申请T的内存。

至此我们能够看出BOOST_CLASS_EXPORT和register_type具有类似的功能:(boost 文档)

  • Instantiates code which is not otherwise referred to.
  • Associates an external identifier with the class to be serialized. The fact that the class isn't explicitly referred to implies this requirement.
  • 实例化未被引用的代码。
  • 用一个外部的标识符关联被序列化的类。

    其实该类未被显式引用即暗示这一要求。

好。转回来。如今看看详细如何用BOOST_CLASS_EXPORT这个宏来实现

1.基类文件:student_info.h

class student_info
{
public:
student_info() {}
virtual ~student_info() {}
student_info(const std::string& sn, const std::string& snm, const std::string& sg);
virtual void print_info() const;
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& ar, const unsigned int version);
private:
std::string name_;
std::string number_;
std::string grade_;
};

2派生类文件middle_student.h

class middle_student : public student_info
{
public:
middle_student() {}
virtual ~middle_student() {}
middle_student(const std::string& sn, const std::string& snm, const std::string& sg, int age);
virtual void print_info();
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& ar, const unsigned int version);
private:
int age_;
};

3.main.cpp

#include <fstream>
#include <boost\archive\text_iarchive.hpp>
#include <boost\archive\text_oarchive.hpp>
#include <boost\serialization\export.hpp> #include "student_info.h" #include "middle_student.h"
BOOST_CLASS_EXPORT(middle_student)
//#1
//#2 void save()
{
std::ofstream ofs("t7.xml");
boost::archive::xml_oarchive oa(ofs);
student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);
oa << BOOST_SERIALIZATION_NVP(sdinfo);
delete sdinfo;
}
void load()
{
std::ifstream ifs("t7.xml");
boost::archive::xml_iarchive ia(ifs);
student_info* sdinfo = NULL;
ia >> BOOST_SERIALIZATION_NVP(sdinfo);
middle_student* mds = dynamic_cast<middle_student*>(sdinfo);
mds->print_info();
} int main()
{
save();
load();
return 0;
}

看见红色字体的两行没有,就是这样用BOOST_CLASS_EXPORT宏,这样就不用注冊派生类。

假如又有一个student_info的派生类xxx_student,且头文件为"xxx_student.h",你仅仅须要

在#1加入#include "xxx_student.h"

在#2加入BOOST_CLASS_EXPORT(xxx_student)

这样你就也能使用基类student_info的指针来转存派生了xxx_student。

BOOST_CLASS_EXPORT的更多相关文章

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

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

  2. boost serialization

    Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...

随机推荐

  1. 列表的初识,列表的索引切片,列表的增删改查,列表的嵌套,元组的初识,range

    1 内容总览 列表的初识 列表的索引切片 列表的增删改查 列表的嵌套 元组的初识(了解) 元组的简单应用(了解) range 2 具体内容 列表的初识 why: str: 存储少量的数据.切片出来全都 ...

  2. HDU 6051 If the starlight never fade(原根+推式子)

    题目大意: 设\(f(i)\)为使\((x+y)^i \equiv x^i (mod\ p)\)成立的(x,y)的对数.其中\(1 \leq x \leq p-1 , 1\leq y\leq m\), ...

  3. C语言Huffman压缩和解压

    符号表结构体: struct node { // 字符串形式存储的Huffman编码 char code[MAX_CODE_LENGTH]; // 这个字符在文件中出现的次数 long count; ...

  4. CAD二次开发(01)-绘制直线

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONObject异常的解决办法

    解决办法:你可以尝试添加一个jar包,因为我加入了一个jar包后错误问题成功解决. 将所需要的jettison-1.2.jar包复制到lib文件夹里面,重启项目,问题搞定.

  6. new 对象和Class的getInstance()方法的差别?

    创建对象时的差别 1.new 对象包含2步, 1)载入类: 2)而且实例化. 2.Class的对象.getInstance(),只不过实例化. 也就是说.在运行 Class的对象.getInstanc ...

  7. 4、python序列对比

    1.列表:[ ],杂(什么都可以放进去),有序,可变 2.元组:(),有序,不可变 3.字典:{ },键值对,无序,可变 4.集合:{ },不可重复,无序,可变

  8. 1、初始JAVA

    一.    语言的翻译分两种: 编译型语言:例如c语言.c++语言  优点:速度快  缺点:不能跨平台                编译器编译——>特定平台的目标文件obj——>特定平台 ...

  9. vim-YCM插件安装

    这两天开始使用vim来学习C++语言,中间少不了要进行编译.才刚刚写了两个小例子就开始发现,每次都要退出vim来进行编译,实在太麻烦了.这时候才想到之前有在一本关于vim的书籍上看到quickfix的 ...

  10. java9新特性-15-全新的HTTP 客户端API

    1.官方Feature 110: HTTP 2 Client 2.使用说明 HTTP,用于传输网页的协议,早在1997年就被采用在目前的1.1版本中.直到2015年,HTTP2才成为标准.     H ...