Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c++ - boost serialization of native type defined with typedef contained within struct

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

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
      }

   }
}

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

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

The enum counts as a primitive type:

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

  • it is a primitive type.

    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.

  • It is a class type and one of the following has been declared according to the prototypes detailed below:
    • a class member function serialize
    • a global function serialize
  • it is a pointer to a Serializable type.
  • it is a reference to a Serializable type.
  • it is a native C++ Array of Serializable type.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...