增强使用结构中包含的 typedef 定义的本机类型的序列化

2023-07-18C/C++开发问题
0

本文介绍了增强使用结构中包含的 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 定义的本机类型的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6