<small id='pRK8l'></small><noframes id='pRK8l'>

    <bdo id='pRK8l'></bdo><ul id='pRK8l'></ul>

  • <i id='pRK8l'><tr id='pRK8l'><dt id='pRK8l'><q id='pRK8l'><span id='pRK8l'><b id='pRK8l'><form id='pRK8l'><ins id='pRK8l'></ins><ul id='pRK8l'></ul><sub id='pRK8l'></sub></form><legend id='pRK8l'></legend><bdo id='pRK8l'><pre id='pRK8l'><center id='pRK8l'></center></pre></bdo></b><th id='pRK8l'></th></span></q></dt></tr></i><div id='pRK8l'><tfoot id='pRK8l'></tfoot><dl id='pRK8l'><fieldset id='pRK8l'></fieldset></dl></div>
    <tfoot id='pRK8l'></tfoot>

      1. <legend id='pRK8l'><style id='pRK8l'><dir id='pRK8l'><q id='pRK8l'></q></dir></style></legend>

        如何使用 Boost.serialize 序列化派生模板类?

        时间:2023-10-06
        1. <legend id='WfDd3'><style id='WfDd3'><dir id='WfDd3'><q id='WfDd3'></q></dir></style></legend>

            1. <i id='WfDd3'><tr id='WfDd3'><dt id='WfDd3'><q id='WfDd3'><span id='WfDd3'><b id='WfDd3'><form id='WfDd3'><ins id='WfDd3'></ins><ul id='WfDd3'></ul><sub id='WfDd3'></sub></form><legend id='WfDd3'></legend><bdo id='WfDd3'><pre id='WfDd3'><center id='WfDd3'></center></pre></bdo></b><th id='WfDd3'></th></span></q></dt></tr></i><div id='WfDd3'><tfoot id='WfDd3'></tfoot><dl id='WfDd3'><fieldset id='WfDd3'></fieldset></dl></div>

              <small id='WfDd3'></small><noframes id='WfDd3'>

              • <tfoot id='WfDd3'></tfoot>
                  <tbody id='WfDd3'></tbody>
                  <bdo id='WfDd3'></bdo><ul id='WfDd3'></ul>
                  本文介绍了如何使用 Boost.serialize 序列化派生模板类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想序列化/反序列化以下类:

                  I'd like to serialize/unserialize following classes:

                  class Feature{
                  ...
                  virtual string str()=0;
                  };
                  
                  template<typename T>
                  class GenericFeature : public Feature{
                  T value;
                  ...
                  virtual string str();
                  };
                  

                  我阅读了 boost.serialize 文档,并说您必须注册类.我可以在构造函数中注册它们.但是加载会出现问题,因为注册是动态的,而不是静态的(据我了解,您必须在序列化/反序列化之前注册类).

                  I read boost.serialize docs and the sayed that you must register classes. I can register them in constructor. But there will be problems with loading, as registration will be dynamic, not static(As I understood, you must register classes prior to serialization/deserialization).

                  如何保存/加载这些类型的类?

                  How to save/load these type of classes?

                  推荐答案

                  首先告诉 boost Feature 是抽象的,并不总是需要:

                  First tell boost that Feature is abstract, is not always needed:

                  BOOST_SERIALIZATION_ASSUME_ABSTRACT(Feature);
                  

                  序列化方法应该或多或少像这样:

                  The serialization method should look more or less like this:

                  template<class Archive> 
                  void Feature::serialize(Archive & ar, const unsigned int version)
                  {
                     ar & BOOST_SERIALIZATION_NVP(some_member);
                  }
                  
                  
                  template<typename T,class Archive> 
                  void GenericFeature<T>::serialize(Archive & ar, const unsigned int version)
                  {
                     ar & boost::serialization::base_object<Feature>(*this);  //serialize base class
                     ar & BOOST_SERIALIZATION_NVP(some_other_member);
                  }
                  

                  现在棘手的一点是在序列化/反序列化中注册类:

                  Now the tricky point is to register class in serialize/deserialize:

                  boost::archive::text_iarchive inputArchive(somesstream);
                  
                  boost::archive::text_oarchive outputArchive(somesstream);
                  
                  //something to serialize
                  Feature* one = new GenericFeature<SomeType1>();
                  Feature* two = new GenericFeature<SomeType2>();
                  Feature* three = new GenericFeature<SomeType3>();
                  
                  //register our class, must be all of posible template specyfication    
                  outputArchive.template register_type< GenericFeature<SomeType1> >();
                  outputArchive.template register_type< GenericFeature<SomeType2> >();
                  outputArchive.template register_type< GenericFeature<SomeType3> >();
                  
                  // now simply serialize ;-]
                  outputArchive << one << two << three;
                  
                  // register class in deserialization
                  // must be the same template specification as in serialize
                  // and in the same correct order or i'm get it wrong ;-D   
                  inputArchive.template register_type< GenericFeature<SomeType1> >();
                  inputArchive.template register_type< GenericFeature<SomeType2> >();
                  inputArchive.template register_type< GenericFeature<SomeType3> >();
                  
                  Feature* another_one;
                  Feature* another_two;
                  Feature* another_three;
                  
                  // and deserialize ;-]
                  inputArchive >> another_one >> another_two >> another_three;
                  

                  如果您需要在某处隐藏显式注册并使其更加自动化,则可以制作特殊的函子模板来注册一个派生类,创建所有可用的并放入一个列表中,类 Feature 的一个静态方法将注册商场.但是问题是您需要注册所有版本的存档,现在我不知道多态存档是否可以完成这项工作.

                  If you need to hide explicit registering somewhere and make it more automatic, there is idea to make special functor template that register one derived class, create all avaible and put in a single list, that one static method of class Feature would register them all. However the problem will be that you need registration for all version of archive, right now i dont know if polymorphic archive will do the job or not.

                  这篇关于如何使用 Boost.serialize 序列化派生模板类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何扩展词法转换以支持枚举类型? 下一篇:使用Boost序列化和发送数据结构?

                  相关文章

                  最新文章

                  <tfoot id='Rifbg'></tfoot>

                  <small id='Rifbg'></small><noframes id='Rifbg'>

                  • <bdo id='Rifbg'></bdo><ul id='Rifbg'></ul>

                      <legend id='Rifbg'><style id='Rifbg'><dir id='Rifbg'><q id='Rifbg'></q></dir></style></legend>

                      <i id='Rifbg'><tr id='Rifbg'><dt id='Rifbg'><q id='Rifbg'><span id='Rifbg'><b id='Rifbg'><form id='Rifbg'><ins id='Rifbg'></ins><ul id='Rifbg'></ul><sub id='Rifbg'></sub></form><legend id='Rifbg'></legend><bdo id='Rifbg'><pre id='Rifbg'><center id='Rifbg'></center></pre></bdo></b><th id='Rifbg'></th></span></q></dt></tr></i><div id='Rifbg'><tfoot id='Rifbg'></tfoot><dl id='Rifbg'><fieldset id='Rifbg'></fieldset></dl></div>