boost序列化补充说明
[入库:2005年8月19日] [更新:2007年3月24日]
|
boost序列化补充说明
昨天研究了boost::xml_serialization的序列化的应用部份,特别是在xml_serialization中关于对象序列化的部份还是值得研究一下的,我花了点时间看了看这部份的代码是如何实现的,这里记录一下,做个笔记和朋友们分享
(一)回顾
昨天研究到对象序列化部份可以通过如下的方式做对象的序列化
test* a = null; oa >> boost_serialization_nvp(nvalue); oa >> boost_serialization_nvp(ints); oa >> boost_serialization_nvp(a);
这时序列化回来的时候就会生成test对象
这个部份的实现是跟踪了一下代码是这样实现的
(二)对象创建的过程
a.首先是从operator >>( test* &a )这个操作符开始
b.然后会调用load_override(test* &a , bppst_pfto int ) => basic_xml_iarchive::load_override(test* &a , 0)
c.basic_xml_iarchive::load_override(test* &a , 0)这个函数做几件事情,结点开始load_start( "a"/*结点的名字,缺省是变量的名字*/ ),结点结束load_end("a"),另一个就是核心的load函数了
d.archive::load函数做类型匹配后调用invoke(xml_iarchive , a/*是引用*/ )
e.invoke函数做两件主要的事儿,register_type( xml_iarchive , test* &a ) 和 xml_iarchive.load_pointer
|
register_type函数实现代码的简要说明,这个函数没有什么特别的什么别的功能主要是生成一个cobject_id的结构体把它加到一个数组中,cobject_id需要一个basic_iserializer的结构体,这个结构体保存了两个静态的类型指针xml_iarchive的指针,一个是test*的指针。看看下面的代码就清楚了
template<class t> const basic_pointer_iserializer * register_type(t * t = null){ const basic_pointer_iserializer & bpis = archive::detail::instantiate_pointer_iserializer( static_cast<archive *>(null), static_cast<t *>(null) ); this->this()->register_basic_serializer(bpis.get_basic_serializer()); return & bpis; }
这里保存了这两个指针 |
|
ar.load_pointer函数的简要说明,这个函数是动态生成类实例的功能函数,前面没有什么要跟的都是处理版本对象id什么的代码,值得记录的就是bpis_ptr->load_object_ptr( ar , object_id_vector[ui].address , co.file_version )这个函数是比较关键的函数,具体的代码实现如下
template<class t, class archive> boost_dllexport void pointer_iserializer<t, archive>::load_object_ptr( basic_iarchive & ar, void * & x, const unsigned int file_version ) const { auto_ptr_with_deleter<t> ap(heap_allocator<t>::invoke()); if(null == ap.get()) boost::throw_exception(std::bad_alloc()) ; t * t = ap.get(); x = t;
// this addresses an obscure situtation that occurs when load_constructor // de-serializes something through and a pointer. ar.next_object_pointer(t);
archive & ar_impl = boost::smart_cast_reference<archive &>(ar); boost::serialization::load_construct_data_adl<archive, t>( ar_impl, t, file_version ); ar_impl >> boost::serialization::make_nvp(null, * t); ap.release(); }
红色部份的代码为对象分配内存的代码,其中用到了auto_ptr_with_deleter这个智能指针,和一个heap_allocator<t>堆的分配器,这两个工具类的实现代码很简单,看看他们的代码就清楚,调用完这一句之会就会为test对象分配一块儿堆的内存做为a指针指向的内容,注意这个时候还没有调用到test的构造函数,只是为这个对象分配了一个空间因为在heap_allocator<t>中分配内存使用内存用的是operator new (sizeof(t)),这个操作只是分配了空间并没有调用test的构造函数。
紫色部份的代码是真正的调用构造函数的部份,调试跟踪后就会发现最后通过调用
template<class archive, class t> static void construct(archive & /* ar */, t * t){ // default is inplace invocation of default constructor // note the :: before the placement new. required if the // class doesn't have a class-specific placement new defined. ::new(t)t; } 这个函数来调用test对象的构造函数的,到了这儿test* a这个对象才算真正创建完成了。
|
|
关于::new(t) t操作符的说明:
通过调用::new(t) t这个操作符可以显示调用类的构造函数,想想下面的例子代码:
class test { public: test() { mvalue = 1002; } test( int value ) : mvalue(value) {
} int mvalue; public: template<class archive> void serialize(archive & ar, const unsigned int version) { ar & boost_serialization_nvp(mvalue); }
};
int main(int argc, char* argv[]) {
test* a = new test()
}
这句代码可以看成是下面两句的代码实现的首先为类的对象分配内存然后调用企构造函数
test* a = (test*)malloc( sizeof(test) ); ::new(a) test;
下面这段代码是为了说明显示调用构造函数的
test b; ::new(&b) test(100);
这样调用时第一句会调用一次构造函数这时候b.mvalue = 1002,当执行第二句的时候就是调用test::test( int value )这个构造函数,这时个b.mvalue = 100了 |
到此就完成了xml_serialization的全部的序列化过程......过两天就要回家了,没心情干活了,呵呵,祝各位新年快乐 。 |
本文关键:boost序列化补充说明
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)