提升从struct list派生的struct的序列化

时间:2014-04-22 04:38:37

标签: c++ serialization boost boost-serialization

我不知道如何序列化从另一个列表类型结构派生的结构。 以下是我要问的示例代码:

// Nodes.hpp

#include <boost/serialization/serialization.hpp>
#include <boost/serialization/access.hpp> 
#include <boost/serialization/optional.hpp> 
#include <boost/serialization/list.hpp> 

struct Node
{
      std::string firstname;
      std::string lastname;

   private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &ar, const unsigned int version)
      {
         ar & firstname;
         ar & lastname;
      }

};

struct Nodes : public std::list<Node>
{
      std::string address;
      std::string country;

   private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &ar, const unsigned int version)
      {
         ar & address;
         ar & country;
      }

};

BOOST_CLASS_EXPORT_KEY(Node);
BOOST_CLASS_EXPORT_KEY(Nodes);

BOOST_CLASS_EXPORT_IMPLEMENT(Nodes);         
BOOST_CLASS_IMPLEMENTATION(Nodes,boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(Nodes,boost::serialization::track_never);

请查看“public std :: list”行,并告诉我序列化是否正确。

1 个答案:

答案 0 :(得分:2)

我不知道你想问什么。不过我知道

  1. 不建议继承标准容器(它们不是为此而设计的)
  2. 如果您,您仍然必须指定您希望序列化基类:

    struct Nodes : public std::list<Node>
    {
        std::string address;
        std::string country;
    
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive &ar, const unsigned int version)
        {
            ar & boost::serialization::base_object<std::list<Node> >(*this);
            ar & address;
            ar & country;
        }
    
    };
    
  3. 当然,我建议将list 成员改为 base 。不要忘记加入boost/serialization/list.hpp:看到它 Live On Coliru

    #include <boost/serialization/serialization.hpp>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/serialization/export.hpp>
    #include <boost/serialization/access.hpp>
    #include <boost/serialization/base_object.hpp> 
    #include <boost/serialization/optional.hpp> 
    #include <boost/serialization/list.hpp> 
    #include <boost/serialization/shared_ptr.hpp>
    #include <list>
    
    struct Node
    {
        std::string firstname;
        std::string lastname;
    
        private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int version)
            {
                ar & firstname;
                ar & lastname;
            }
    
    };
    
    struct Nodes : public std::list<Node>
    {
        std::string address;
        std::string country;
    
        private:
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int version)
            {
                ar & boost::serialization::base_object<std::list<Node> >(*this);
                ar & address;
                ar & country;
            }
    
    };
    
    BOOST_CLASS_EXPORT_KEY(Node);
    BOOST_CLASS_EXPORT_KEY(Nodes);
    
    BOOST_CLASS_EXPORT_IMPLEMENT(Nodes);
    BOOST_CLASS_IMPLEMENTATION(Nodes    , boost::serialization::object_serializable);
    BOOST_CLASS_TRACKING(Nodes          , boost::serialization::track_never);
    
    int main()
    {
        boost::archive::text_oarchive oa(std::cout);
            Nodes nodes;
            nodes.address = "Mr.";
            nodes.country = "Puerto Mazarique";
            nodes.insert(nodes.end(), Node{"John", "Doe"});
            nodes.insert(nodes.end(), Node{"Jane", "Greenwalt"});
            nodes.insert(nodes.end(), Node{"Morgan", "Cheese"});
            nodes.insert(nodes.end(), Node{"Walton", "Whiz"});
    
            oa << nodes;
    }