提升嵌套结构的序列化不起作用

时间:2014-04-15 16:30:30

标签: c++ serialization boost boost-serialization

我试图序列化包含两种或更多其他类型sturct的结构。但似乎序列化不起作用。下面是样本代码:

// SimpleData.hpp

#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp> 
#include <boost/serialization/optional.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/shared_ptr.hpp>

namespace A
{

Struct Name
{
    std::string firstname;
    std::string lastname;

      template<class Archive>
      void serialize(Archive &ar, const unsigned int version)
      {
         ar & firstname;
         ar & lastname;
      } 
};

Struct Address
{
   std::string street;
   std::string city;

      template<class Archive>
      void serialize(Archive &ar, const unsigned int version)
      {
         ar & street;
         ar & city;
      } 

}

Struct FullData 
{

  public :
      FullData();
      FullData(const parameter_strings & parms);

      virtual ~FullData();

      boost::optional<Name> nameinfo;       // instance of struct Name
      boost::optional<Address> addressinfo;    // instance of struct Address

      std::string country;
      std::int pincode;

  private :
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &ar, const unsigned int version)
      {
         ar & nameinfo;       // are these enough to take of serialization of Name and 
         ar & addressinfo;   // Address
         ar & country;;
         ar & pincode;
      } 
};

}

// SimpleData.cpp

#include "SimpleData.hpp"

FullData::FullData()
{

}

FullData::~FullData()
{

}

FullData::~FullData(const parameter_strings & parms): Detail(parms)
{

 // impelmentation
}


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

我不确定上面的代码是否会正确序列化。上面的代码有什么问题。 我是否还需要为SimpleData.cpp文件中的Name和Address结构的序列化添加以下代码

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


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

1 个答案:

答案 0 :(得分:2)

我不知道问题是什么。我/做/知道代码中有错误。

我修复了这些,看哪:工作:

int main()
{
    A::FullData data({ { "key1", "value1" }, { "key2" , "value2" } });
    data.nameinfo    = A::Name    { "firstname", "lastname" };
    data.addressinfo = A::Address { "street", "city" };
    data.pincode     = 99977;
    data.country     = "Gibraltar";

    boost::archive::text_oarchive oa(std::cout);
    oa << data;
}

打印

22 serialization::archive 10 0 0 1 0 0 0 9 firstname 8 lastname 0 0 1 0 0 0 6 street 4 city 9 Gibraltar 99977 0 0 2 0 0 0 4 key1 6 value1 4 key2 6 value2

查看 Live On Coliru

SimpleData.hpp

#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp> 
#include <boost/serialization/optional.hpp> 
#include <boost/serialization/map.hpp> 
#include <boost/serialization/shared_ptr.hpp>

namespace A
{
    typedef std::map<std::string, std::string> parameter_strings;
    struct Name
    {
        std::string firstname;
        std::string lastname;

        template<class Archive>
            void serialize(Archive &ar, const unsigned int version)
            {
                ar & firstname;
                ar & lastname;
            } 
    };

    struct Address
    {
        std::string street;
        std::string city;

        template<class Archive>
            void serialize(Archive &ar, const unsigned int version)
            {
                ar & street;
                ar & city;
            } 

    };

    struct FullData 
    {
        FullData();
        FullData(const parameter_strings & parms);

        virtual ~FullData();

        boost::optional<Name> nameinfo;       // instance of struct Name
        boost::optional<Address> addressinfo; // instance of struct Address
        parameter_strings detail;

        std::string country;
        int pincode;

        private :
        friend class boost::serialization::access;
        template<class Archive>
            void serialize(Archive &ar, const unsigned int version)
            {
                ar & nameinfo;       // are these enough to take of serialization of Name and 
                ar & addressinfo;   // Address
                ar & country;
                ar & pincode;
                ar & detail;
            } 
    };

}

SimpleData.cpp

#include "SimpleData.hpp"
#include <boost/serialization/export.hpp>

namespace A
{
    FullData::FullData()
    {

    }

    FullData::~FullData()
    {

    }

    FullData::FullData(const parameter_strings & parms): detail(parms)
    {

        // impelmentation
    }

}

#include <boost/archive/text_oarchive.hpp>

BOOST_CLASS_EXPORT_IMPLEMENT(A::FullData)
BOOST_CLASS_IMPLEMENTATION(A::FullData    , boost::serialization::object_serializable)
BOOST_CLASS_TRACKING(A::FullData          , boost::serialization::track_never)

int main()
{
    A::FullData data({ { "key1", "value1" }, { "key2" , "value2" } });
    data.nameinfo    = A::Name    { "firstname", "lastname" };
    data.addressinfo = A::Address { "street", "city" };
    data.pincode     = 99977;
    data.country     = "Gibraltar";

    boost::archive::text_oarchive oa(std::cout);
    oa << data;
}