增强使用结构中包含的 typedef 定义的本机类型的序列化 [英] boost serialization of native type defined with typedef contained within struct

查看:22
本文介绍了增强使用结构中包含的 typedef 定义的本机类型的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种类型和枚举的 MyFile.hpp 头文件.我如何对给定的示例代码进行序列化/反序列化.

I have a MyFile.hpp header file which contains various types and enums. How do i do serialization/ desrialization of given example code.

//MyFile.hpp

//MyFile.hpp

namespace A { 
   namespace B {

      typedef std::string MyString;
      typedef std::map<std::string,std::string> my_type;
      typedef bool result;

      struct MyTimer
      {
         int time;

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

      enum MODE
      {
          Sleep=1,
          HybridSleep,
          Hybernate
      }

   }
}

我需要在对应的 MyFile.cpp 中实现,但不知道我该怎么做.

I need to do implementation in corresponding MyFile.cpp but don't know how do i go ahead.

谢谢,

推荐答案

地图、字符串等可以通过包含相关标题来序列化:

Maps, strings etc. can just be serialized by including the relevant header:

#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>

枚举 算作原始类型:

当且仅当以下条件之一为真时,类型 T 是可序列化的:

A type T is Serializable if and only if one of the following is true:

  • 它是一种原始类型.

  • it is a primitive type.

原始类型是指 C++ 内置类型,仅是 C++ 内置类型.算术(包括字符)、bool、enum 是原始类型.在下面的序列化特征中,我们为不同的目的以不同的方式定义了一个原始"实现级别.这可能是混淆的根源.

By primitive type we mean a C++ built-in type and ONLY a C++ built-in type. Arithmetic (including characters), bool, enum are primitive types. Below in serialization traits, we define a "primitive" implementation level in a different way for a different purpose. This can be a source of confusion.

  • 一个类成员函数序列化
  • 一个全局函数序列化

对于更棘手的情况,有 BOOST_STRONG_TYPEDEF(参见 文档序列化包装器")

For more tricky cases there is BOOST_STRONG_TYPEDEF (see documentation "Serialization Wrappers")

这篇关于增强使用结构中包含的 typedef 定义的本机类型的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆