How to serialize fusion::vector?

荒凉一梦 提交于 2019-12-04 17:03:17
niXman

Solved.

Like this: http://liveworkspace.org/code/652cab59dc33d280b6d48470c11cfb91

#include <iostream>
#include <sstream>

#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>
#include <boost/fusion/container/generation/make_vector.hpp>
#include <boost/fusion/include/make_vector.hpp>
#include <boost/fusion/sequence/comparison/equal_to.hpp>
#include <boost/fusion/include/equal_to.hpp>

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>

/***************************************************************************/

template<typename archive_type>
struct item_serializer {
   item_serializer(archive_type& ar):ar(ar) {}

   template<typename T>
   void operator()(const T& o) const {
      ar << o;
   }
   archive_type& ar;
};

template<typename archive_type, typename V>
archive_type& serialize_fusion_vector(archive_type& ar, const V& v) {
   boost::fusion::for_each(v, item_serializer<archive_type>(ar));
   return ar;
}

template<typename archive_type>
struct item_deserializer {
   item_deserializer(archive_type& ar):ar(ar) {}

   template<typename T>
   void operator()(T& o) const {
      ar >> o;
   }
   archive_type& ar;
};

template<typename archive_type, typename V>
archive_type& deserialize_fusion_vector(archive_type& ar, V& v) {
   boost::fusion::for_each(v, item_deserializer<archive_type>(ar));
   return ar;
}

/***************************************************************************/

int main() {
   boost::fusion::vector<int, char, double> src(boost::fusion::make_vector(3, '4', 5.41)), dst;
   std::ostringstream os;
   {  boost::archive::binary_oarchive oa(os);
      serialize_fusion_vector(oa, src);
   }
   std::istringstream is(os.str());
   {  boost::archive::binary_iarchive ia(is);
      deserialize_fusion_vector(ia, dst);
   }

   std::cout << ((src == dst) ? "is equal" : "is not equal") << std::endl;
}

/***************************************************************************/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!