如何序列化 fusion::vector?

发布于 2024-10-11 09:21:17 字数 54 浏览 4 评论 0原文

告诉我,如何序列化/反序列化 fusion::vector 对象类型?

谢谢。

Tell me, how can I serialize/deserialize the fusion::vector object type?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高速公鹿 2024-10-18 09:21:17

解决了。

像这样: 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;
}

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

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;
}

/***************************************************************************/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文